Round Brackets () and Curly Brackets {} have special meaning in Cucumber Expressions. You can not use them directly. If you ever need to match () or {}, you can escape the opening ( or { with a backslash.
Cucumber Expressions won't allow us to use any other special character. For that, we have to use regular expressions.
Table of Contents
- Escaping Square and Curly Brackets
- Escaping Single Special Character
- Escape Multiple Special Characters
- Video Tutorial
Escaping Square and Curly Brackets
Steps
Then I should see 5 item(s)
Then I should see 10 {what}
Step Definitions
@Then("I should see {int} item\\(s)")
public void teststep1(int i)
{
System.out.println("value is:"+i);
}
@Then("I should see {int} \\{what}")
public void teststep2(int i)
{
System.out.println("value 2 is:"+i);
}
Cucumber Escaping Single Special Character
Here we will remove $ and % Symbol from step.
Step
Then total of cart is $100.50
Then progress is 100%
Step Definition
@Then("total of cart is ${double}")
public void total_of_cart_is_$(Double double1)
{
System.out.println(double1);
}
@Then("progress is {int}%")
public void progress_is(Integer int1)
{
System.out.println(int1);
}
Cucumber Escaping Multiple Special Character
Cucumber Expression is fine for escaping one or two characters or matching simple text, but what if we have to escape many similar characters or have complex patterns in common steps. In that case, We have to use Regular Expressions.
Step Definition will remove $ and € Symbol from Step.
Step
Then total of cart is $100.50
Then total of cart is €100.50
Step Definition
Escape $ and €
@Then("^total of cart is [$€]([0-9.]*)$")
public void teststep4(float i)
{
System.out.println("value 3 is:"+i);
}
Escape any character before number
@Then("^total of cart is .([0-9.]*)$")
public void total_of_cart_is_$(Double double1) {
System.out.println(double1);
}
Here,
- 0-9 is for digits
- dot . inside [] is for decimal and outside () is for any character
- * means 0 or more
- [] Match character in bracket
- () is to capture value or group
- Log in to post comments