Search code examples
ruby-on-railscucumber

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


Solution

  • 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.