Its obvious that many Steps has certain pattern, In that case we do not declare different Step Definition for each Step. We try to find the pattern and bring down the number of step definitions required.
Regular Expressions is a string of text that allows us to create patterns that help match, locate, and manage text. Using Regular expression we can optimize our step definition.
For statically typed languages (Java, C, C++), Cucumber automatically transforms strings into appropriate type but for dynamically typed languages (Perl, Ruby, Python), no transformation happens automatically. Therefore, its good idea to understand the regular expression pattern used in Cucumber.
Cucumber Step Definition Regular Expression
Consider below Step definition
@Given("^I follow \"([^\"]*)\"$")
public void i_follow_on_page(String link)
{
driver.findElement(By.linkText(link)).click();
}
Regular Expression in Cucumber Examples
Let's break it down and understand each char:
Pattern | Step | Step Definition | Detail |
---|---|---|---|
. |
And I see gray text or And I see grey text |
@Then("I see (gr.y) text") |
You can use any character in place of dot. In our example we have used a and e |
* |
And I see hello text And I see hellooooooooo text |
@Then("I see (hello*) text") |
A repetition modifier. It tells character can be repeated n number of times. We can use o one or n times. |
.* |
a AbCD punctuation! 123-456 an empty string |
any character (except a newline) 0 or more times | |
.+ |
a AbCD punctuation! 123-456 |
at least one of anything (except a newline) | |
\d | For Numbers [0-9]. Matches a single character that is digit. | ||
\D | Not a Digit | ||
[0-9]* or \d* |
123456 9 an empty string |
matches a series of digits (or nothing) | |
[0-9]+ or \d+ |
123456 9 |
matches one or more digits | |
.{2} |
ab Ab a! 25 |
exactly two of any character | |
.{1,3} |
a Ab ab! 5 |
one to three of any character | |
\w | For Words. [A-Za-z0-9_]. Marches a word character, alphanumeric character plus underscore. | ||
\W | Not a Word | ||
\s | Matches white space characters including tab and line break. | ||
\S | Not White Space | ||
\b | Word Boundary | ||
\B | Not a Word Boundary | ||
? | colou?r matches colour or color. | Makes proceeding token optional. | |
| | cat|dog | Equivalent to or. | |
^ | /^foo/ matches foo and foo bar but not bar foo | beginning of string | |
$ | I'm logged in will match I'm logged in with admin, I'm logged in with editor. ^I'm logged$ in will match exact string. | End of string. | |
"[^"]*" | matches something (or nothing) in double quotes | ||
[] | to create a matching list that will match on any one of the characters in the list. | ||
() | When you surround part of a regular expression with parentheses, it becomes a capture group. In a Cucumber Step Definitions, the text matched within each capture group is passed to the code block as an argument. |
You can negate above characters by using capitalize version of it for example \D means any character except numbers.