Saturday, April 22, 2023

 

Regular expression in step definition

Regular Expression

Below are some lists of special character that can be used while writing step definition.

1. .(dot)-We can use any character in place of "." In below example, single step definition will work for both the steps mentioned in feature file. It will replace "." with "e" and "o" as mentioned in steps.

  
    
Feature StepsStep Definition
Then This is red Then This is rod@Then("^This is r.d$")

2. * (star) - "*" is use to repeat the same char. In below example, we can repeat "l" one or many time. Steps Step Definition

  
    
Feature StepsStep Definition
Thenyou are wellll text @Then("^you are wel* text$")


3. .* - "." is use for any char and "*" is for repeat the char. In below example, any char can be entered up to any length.

  
    
Feature StepsStep Definition
ThenThis is my book text @Then("^This is my bo.* text$")

4. (+) (plus sign)-"+" is used to avoid any empty string. In below example, "b" and "r" can be repeat multiple times. It will show error when the entered char is other than "b" or "r" or empty.

  
    
Feature StepsStep Definition
Then make a brbat @Then("^make a [br]+at$")

5. (.+)- "." is use for any char and "+" is for avoiding empty string. In below example, “.” Can be replacing with any char. So here any char can be entered up to any length.

  
    
Feature StepsStep Definition
Then I make new bat @Then("^I make new .+at$")

6. (\d or [0-9])- This will accept only one digit between [0-9] and no empty string. See the below example.

  
    
Feature StepsStep Definition
Then The roll number is 3 @Then ("^The roll number is (\\d)$") or @Then("^The roll number is [0-9]$")

7. (\d+ or [0-9]+)- This will accept one or more than one digit between [0-9]. In below example, we have entered 2 digits.

  
    
Feature StepsStep Definition
Then This is my book 31 @Then ("^This is my book (\\d+)$") or @Then ("^This is my book [0-9]+$")

8. [a-f]- "[a-f]" it will accept any one char between “a” To “f”. So it will restrict the char base on given range. See the below example.

  
    
Feature StepsStep Definition
Then This is limit to alpha b @Then ("^This is limit to alpha [a-f]$") OR For accepting number and char, see the step definition as below. @Then ("^This is limit to alpha [0-9a-cA-C]$")


9. [ea]- Any char mentioned in square bracket only those char can be used. This will accept either "e" or "a". Any char enter other than this will throw error. In below example, single step definition will work for both the steps.

  
    
Feature StepsStep Definition
Then This is blua OR Then This is blue @Then ("^This is blu[ea]$")

10. .{2}- This will accept exact 2 char. Anything entered less or more than 2 will throw error. In below example, 2 size limit is given in step definition.

  
    
Feature StepsStep Definition
Then This is two char ab @Then ("^This is two char .{2}$")

11. .{2,4}- Here the minimum length is 2 and Maximum length is 4. So anything entered less than 2 or more than 4 will give error. In below example, min length given as 2 and Max as 4 in step definition.

  
    
Feature StepsStep Definition
Then This is limit to four char abc @Then ("^This is limit to three char .{2,4}$")

12. \w- This will accept [A-Za-z0-9_] any alpha, number or underscore. In below example, we have given underscore between “s” and “w”.

  
    
Feature StepsStep Definition
Then This is my word s_w @Then ("^This is my word s[\\w]w(.*)$")

13. ? (question sign) – This anchor is use to make preceding char as optional. In below example, here "u" kept as optional. So "?" mark before any char will be optional. In feature file we can pass "color" or "colour". It will work for both.

  
    
Feature StepsStep Definition
Then This is color @Then ("^This is colou?r$")


14. |(pipe line) - This "|" pipe line is like “or” condition. We can give multiple words in pipe line as or option. For example, in feature file we can have two different steps, then a single step definition will work for both the steps.

  
    
Feature StepsStep Definition
Then he has taken book OR Then Then she has taken book @Then ("^This is colou?r$")

15. ^(caret sign) - "^" This anchor is used to exclude the char mentioned in square bracket. In below example, if you enter "s" or "n", it will throw error. For rest other char, it will work fine. "^" this anchor also indicate that the line start from there and $ indicate the end of line.

  
    
Feature StepsStep Definition
Then This is greep @Then ("^This is gree[^sn]$")

16. $ - "$" indicate end of line. ^ and $ use for exact string match. Ex.. I'm logged in will match I'm logged in with admin, I'm logged in with editor. But if we use "^" at the start and "$" at end. It treat all three step different.

  
    
Feature StepsStep Definition
Then This is greep @Then ("^This is gree[^sn]$")


17. \"(.*)\" - (.*) will take value and display as "open" however \"(.*)\" will take value within "" comma such as "open" will be taken as open. In below example, method will receive word as open instead of “open”.

  
    
Feature StepsStep Definition
Then school is "open" @Then ("^school is \\\"(.*)\\\"$")

18. {String} - {string} is used to pass the string from feature file. But "^" and "$" cannot be used at the start and end of the line. See the below example.

  
    
Feature StepsStep Definition
Then gate is "closed" @Then ("gate is {string}")

19. {int} - {int} is used to pass the integer value from feature file to methods. But "^" and "$" cannot be used at the start and end of the line.

  
    
Feature StepsStep Definition
Then flat number is 7 @Then ("flat number is {int}")

20. {float} - {float} is used to pass the float value from feature file to methods. But "^" and "$" cannot be used at the start and end of the line.

  
    
Feature StepsStep Definition
Then float value is 7.67 @Then ("float value is {float}")

21. ()- When you write any 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. In below example (.*), this will pass the value to code block.

  
    
Feature StepsStep Definition
Then This is number <char> @Then ("^This is number(.*)$")

0 comments:

Post a Comment

Translate

Popular Posts

Total Pageviews