• Selenium Video Tutorials

Selenium IDE - Pattern Matching



Selenium IDE can be used for pattern matching for the tests especially for tests requiring verification scenarios. Like locators, patterns are a type of parameter frequently used by Selenium. It allows users to describe patterns with the help of special characters. Many times, the text that we would like to verify is dynamic, in that case, pattern matching is very useful.

Pattern matching is commonly used with all the verification point commands like verify text, verify not text, verify title, verify value, verify, assert text, assert alert, assert not text, assert prompt, assert selected value, assert title, assert not selected value, assert selected label, and so on.

Different Ways of Pattern Matching

  • Globbing

  • Regular Expressions

  • Exact Matching

Globbing

Globbing is used to match a string with respect to a glob(which is the wild character expression). It is commonly used for file matching patterns in Linux or Windows while searching for a certain file type like *.doc, *.jpg, and so on.

There are multiple ways to use globbing in Selenium like * which is used to match any count or order of characters ? which is used to match a unique character, and [] which is used to match any one character provided within the brackets. It is a character class. For example, [0-9] will match any digit. The pattern matching with globbing is by default available in Selenium and it is optional to mention the keyword glob in the test step in the Selenium IDE.

Let us take the example of the below page, where we would verify the text Alerts and header Selenium - Automation Practice using the globbing pattern matching.

Selenium IDE Pattern Matching 1

The steps to be followed are listed below −

Step 1 − Click on the Selenium IDE extension visible on the browser after its installation.

Step 2 − Selenium IDE should be launched along with it, a welcome message should be displayed with the version of the tool. In the below image, the version installed is 3.17.2.

Along with this, it gives us the option to select what we would like to do with the tool, like Record a new test in a new project, Open an existing project, Create a new project, and Close Selenium IDE.

Step 3 − Click on the Create a new project link, then enter a project name, say, Test7 under the Please provide a name for your new project. Finally, we would click on the OK button.

Step 4 − We would have the workspace ready in the Selenium IDE now.

Step 5 − Enter open in the Command field, and https://www.tutorialspoint.com/selenium/ in the Target field to launch the application.

Step 6 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/h1 in the Target field, and glob: Alert* in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/h1 is the text and glob: Alert* is the value to be a matched pattern.

Step 7 − Enter assert text in the Command field, xpath=/html/body/div/header/div[2]/h1 in the Target field, and glob: Selenium - Automation* in the Value field. Please note, xpath=/html/body/div/header/div[2]/h1 is the header and glob: Selenium - Automation* is the value to be a matched pattern.

Step 8 − Enter close in the Command field to close the browser window.

Selenium IDE Pattern Matching 2

Step 9 − Click on the three dots appearing at the left pane, then select the Rename option.

Step 10 − Enter a name, say Test14 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.

Step 11 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 0, and a green bar denoting the test ran successfully without any failures. Also, the message Test14 completed successfully appeared under the Log.

In the example above, we had verified the texts with the help of the globbing.

Exact Matching

Exact matching is used to match a string as it is present. For example, if an exact match with the string value is required, the exact pattern can be used. In exact matching, the operator * works as a normal character rather than for pattern matching wildcard characters. Let us take the same example as discussed above and use exact pattern technique.

The steps to be followed are listed below −

Step 1 − Follow Steps 1 to 5 of the previous example.

Step 2 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/h1 in the Target field, and exact: Alerts in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/h1 is the text and exact: Alerts is the value to be matched exactly.

Step 3 − Enter assert text in the Command field, xpath=/html/body/div/header/div[2]/h1 in the Target field, and exact: Selenium - Automation Practice in the Value field. Please note, xpath=/html/body/div/header/div[2]/h1 is the header and exact: Selenium - Automation Practice is the value to be matched exactly.

Step 4 − Follow Steps 8 to 11 of the previous example.

Regular Expressions

Regular expressions are the most useful among the pattern matching techniques available. Selenium supports the complete set of regular expression patterns that Javascript supports. Hence the users are no longer limited by *, ? and [] globbing patterns.

To use RegEx patterns, we need to prefix with either "regexp:" or "regexpi:". The prefix "regexpi" is case-insensitive. The glob: and the exact: patterns are the subsets of the Regular Expression patterns. Everything that is done with glob: or exact: can be accomplished with the help of RegExp.

There is more information on regular expressions available in the below link −

https://www.tutorialspoint.com/javascript/javascript_regexp_object.htm

This concludes our comprehensive take on the tutorial on Selenium IDE - Pattern Matching. We’ve started with describing different ways of Pattern Matching like globbing, regular expression, and exact match and how to use them with Selenium.

This equips you with in-depth knowledge of the Pattern Matching in Selenium IDE. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons

Advertisements