Cucumber: fill in a field with double quote in it

ぃ、小莉子 提交于 2020-12-31 04:43:45

问题


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


回答1:


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.




回答2:


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"/


来源:https://stackoverflow.com/questions/8533943/cucumber-fill-in-a-field-with-double-quote-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!