While asserting some text on a webpage, sometimes you will find that part of the text contains a single quote or double quote.
Then I should see "Hello "World"" text
The step which Cucumber Auto generates for you will escape the text but will not accept the text given in double-quotes, and if you try to take this parameter in your method, you will get an error.
Step Generated By Cucumber
@Then("I should see \"Hello \"World\"\" text")
public void i_should_see_hello_world_text() {
}
This will not read "Hello "World""
You will get "The gherkin step has 0 arguments Error" if you will try to pass parameter to your method like this:
@Then("I should see \"Hello \"World\"\" text")
public void i_should_see_hello_world_text(String s) {
}
and if you will try to replace Hello World with parameter {string}, Cucumber will not detect your step definition.
Escaping of quotation marks in Cucumber steps
One way to handle this is to add a backslash (\) before double quotes or single quotes in your step.
Steps
Then I should see "Hello \'World\'" text
Then I should see "Hello \"World\"" text
Step Definition
Below step definition will work fine for above 2 steps.
@Then("I should see {string} text")
public void i_should_see1(String string) {
System.out.println("Test:"+string);
}
Capturing Quotes in Step Definition
Then I should see "Hello "World"" text
Then I should see "Hello 'World'" text
Handling Double Quotes
@Then("^I should see \"([\\w\\s\"]*)\" text$")
public void teststep3(String s)
{
System.out.println("value is:"+s);
}
or
@Then("^I should see \"([a-zA-Z \"]*)\" text")
public void i_should_see_text(String string) {
System.out.println(string);
}
Handling Single Quotes
@Then("^I should see \"([\\w\\s']*)\" text$")
public void teststep4(String s)
{
System.out.println("value is:"+s);
}
Handling Single and Double Quotes together
@Then("^I should see \"([\\w\\s\"']*)\" text$")
public void teststep3(String s)
{
System.out.println("value is:"+s);
}
Here,
- () : Capturing Group
- [] : Match Character in Bracket
- \\w : word Character. You can also use a-zA-Z
- \\s : Whitespace. You can also use space
- \" : For Double Quotes
- ' : For Single Quotes
- * : 0 or More
- Log in to post comments