Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber: fill in a field with double quote in it

I have some rails app, a view with a field, lets say its called 'some_field' I want to fill in in the field '"SOME_STRING_WITH_QUOTES"'

How can I do this in cucumber?

When I fill in "some_field" with ""SOME_STRING""

When I fill in "some_field" with "\"SOME_STRING\""

dont work (the cucumber parser doesnt seem to approve) What to do? Its not some sort of matcher, so a regex wont work neither

like image 525
Len Avatar asked Dec 17 '22 06:12

Len


2 Answers

When you write a step in your cucumber scenario, Cucumber suggests you the standard regex to match text inside the double quotes (regular expression [^"]* means a sequence of 0 or more characters of any kind, except " character). So for When I fill in "some_field" with: "SOME_STRING" it will suggest the following step definition:

When /^I fill in "([^"]*)" with: "([^"]*)"$/ do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
end 

But you are not forced to use this regex and free to write any matcher you want. For example the following matcher will match any string that follows after colon and ends at the line end (and this string may include quotes):

When /^I fill in "([^"]*)" with: (.*)$/ do |field, value|
    pending # express the regexp above with the code you wish you had
end

Then your scenario step will look like this:

When I fill in "some_field" with: "all " this ' text will ' be matched.

Update

You can also use Cucumber's Multiline String in your Scenario step:

When I fill in "some_field" with: 
  """
  your text " with double quotes
  """

And then you won't need to change step definition generated by Cucumber:

When /^I fill in "([^"]*)" with:$/ do |arg1, string|
  pending # express the regexp above with the code you wish you had
end

Your string with quotes will be passed as a last argument. In my opinion this approach looks heavier than the first one.

like image 174
Aliaksei Kliuchnikau Avatar answered Dec 29 '22 22:12

Aliaksei Kliuchnikau


While google'ing I just found an alternative solution which is (imho) more easy to understand (or reunderstand ;)). I'm using Java but I'm sure the same idea could be reused in your case..

Instead of using this step definition:

@Then("parameter \"(.*?)\" should contain \"(.*?)\"$")

I use another escape char (from " to /):

@Then("parameter \"(.*?)\" should contain /(.*?)/$")

So I can write the feature using double quotes:

Then parameter "getUsers" should contain /"name":"robert","status":"ACTIVE"/
like image 34
boly38 Avatar answered Dec 29 '22 21:12

boly38