Selenium Pattern Matching using globs
Patterns, the magic input typically in querying scenarios. These are the basis on which querying can be made simple and yet powerful. In order to verify text contents of websites, it is equally important to verify these through matching patterns.
In a sense, the patterns will deliver the much needed punch to match various content types on your webpage. The entities that could be verified includes (a) Links (b) elements (c) text.
Most Selenium commands supports the pattern parameters. To understand this, we need to know the type of patters that you can use in your tests.
Types of patterns
- globs
- exact
- regular expressions
What are globs?
Since the days of DOS, we have been using globbing. What is globbing? Remember the days you were searching files in DOS by using the command *.doc or De*.doc and so on. This is where you have used globs. So let’s see how Selenium supports globbing.
Globbing in Selennium is possible with the following set of characters
i) * (asterisk)
- It is used to match any number of characters. E.g. *.doc will indicate a.doc or abhilash.doc
ii) ? (question mark)
- It is used to match a single character. E.g. Pe?.doc will indicate Pet.doc. It supports just a single character.
iii) [ ] (square brackets)
- Typically a square bracket is used to denote a class of characters or set of characters.
- [a-z] means a set of lowercase alphabets
- [A-Z] means a set of uppercase alphabets
- [0-9] indicates a set of numeric values
How to use globs in Selenium?
Suppose we want to find/ search for the text “region” or “regional”, then we could use it as reg*
In Selenium, that would mean the following
Command | Target | Value |
verifyTextPresent | Glob: reg* |