top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

A Glimpse of Regular Expressions in Xpath/CSS and in Cucumber

A Regular Expression in Xpath helps with using 'part of a locator attribute' that stays constant to identify a web element on a web page.Because some times the values within the HTML code for the attributes change.

For instance attributes would change every time the web page you are working on is refreshed

Any value which is alphanumeric has a high probability to change every time a page is refreshed.

In some cases, XPath includes the part which was always changed when we are loading the page and it will cause a failure. Also, we can use the wildcards for handling the wildcards operator on which XPath is not applied on those methods. Standard of XPath introduces the regular expression which was new and used for the general purpose. The XPath regex plugin is used to match the style which contains one or more values unique from the attributes and elements in our xml documents. XPath regex is very useful and important in python and selenium.


How to use regex in selenium locators?

We can use regex in locators in Selenium web driver. This can be achieved while we identify elements with the help of xpath or css locator. Let us have a look at the class of an element in its html code. The class attribute value is gsc-input.

  • Using * regex:

Here, with the css expression, we can use the * and perform a partial match with the class attribute

value.

The css value shall be input[class*='input'].

This means the subtext input is present in the actual text gsc-input.

  • Using ^ regex :

We can also use the ^ and perform a match with the class.

The css value shall be input[class^='gsc'].

This means the actual text gsc-input starts with the subtext gsc.

  • Using $ regex:

We can also use the $ and perform a match with the class.

The css value shall be input[class$='put'].

This means the actual text gsc-input ends with the subtext put.

  • Using contains():

The xpath expression, we can use the contains() method and perform a partial match with the class.

The xpath value shall be //*[contains(@class, 'input')].

This means the subtext input is present in the actual text gsc-input.

  • Using starts-with()

We can also use the starts-with() and perform a match with the class.

The xpath shall be //*[starts-with(@class, 'gsc')].

This means the actual text gsc-input starts with the subtext gsc.

  • Using ends-with()

We can also use the ends-with() and perform a match with the class.

The xpath shall be //*[ends-with(@class, 'put')].

This means the actual text gsc-input ends with the subtext put.


How do you use regular expressions in Cucumber?

We can use regular expressions in Cucumber for selecting a collection of similar statements in the feature file.

feature file

Feature: Exam Syllabus

Scenario Outline: Summer and Winter Exam Schedule

Given Exam time table in summer season

Given Mathematics and Physics Syllabus

Given Exam time table in winter season

The step Definition file has @Given("^Exam time table in ([^\"]*) season$") which maps two Given statements in a Feature file with the help of regular expression.

Example

@Given ("^Exam time table in ([^\"]*) season$")

public void timeTable(String season)

{

if (season.equals("winter"))

` {

System.out.println("The winter syllabus");

}

else

{

System.out.println("The summer syllabus");

}

}

@Given ("^Mathematics and Physics Syllabus$")

public void syllabusList()

{

System.out.println("Mathematics and Physics syllabus is");

}


XPath regex Function
  • At the time the attribute value is long it will be a mess with the code, we need to take unique text from the said attribute and need to generate the regular expression by using CSS selector or XPath. Below is the XPath regex function as follows.

  • Fn:matches – This function will take the regular expression and subject string as an input. If our regular expression is matched in any string then this function will return the true. If suppose function has not matched any string then it will return false. We need to use anchors when we want only to return the true value when regex will match the entire string.

  • Fn:replace – This function is taking replacement string as input and takes the subject as a string in a regular expression. It will return the new string by all the matches by using the pattern of regex, and it was replaced with the text of replacement. We can use $1 to $99 to capture replacement into the groups. Zero will insert all regex matches. The $0 will insert whole the regex match. Backslashes and literal will escaped in a backslash. Zero-length matches are not replaced by replacing function, it will raise an error rather than return an output.

  • Fn:tokenize – It looks like a spit function in other programming languages. This function will return the string array which consisting all other substrings in the subject of all matches of regex. The array will not contain the matches of regex. If the regex is matching the first and last character into the string of the subject then the last and the first string will be resulting in an array. Tokenize is not handling the regular expression of zero length.

Conclusion:

Using XPath and regular expressions ensures that you are collecting data in the most effective and accurate way.

Scenarios In Which You Will Use Regular Expressions

  • When the attribute value is too long, 30 to 40 characters long will mess up your code so just take the unique text out of the said attribute and use it to generate a regular expression in Xpath or CSS Selector

  • Where the only available attributes are alphanumeric. These attribute change with every page refresh


To have more idea on what is regex.Go through https://www.numpyninja.com/post/regex-search-edit-and-validate-text

4,783 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page