Suppose you’re creating a test case in Selenium IDE, and you need to check if something is true. However, that something may have two or more values. How can you do that?
The simplest and most straightforward way to use an OR
operator in Selenium IDE is to create a RegEx rule that matches two or more alternatives.
You do this by wrapping each alternative in parentheses and separating adjacent alternatives with the vertical line (|
) character, then prefixing the regular expression with “regexp:” to tell Selenium IDE that you want it to use RegEx matching for this field.
Let’s take a look at an example:
The test case above uses the verifyText
command to target a DOM element with the ID #my-target-element
and check if it’s value is either “Option A” or “Option B”.
The regular expression, which you can test at regex101, looks like this:
(Option A)|(Option B)
Where the |
character is the OR operator and the parentheses demark a capturing group to match either “Option A” or “Option B” in literally, with case sensitivity. But to make this regular expression work, you need to indicate to Selenium IDE that it is RegEx in the first place by adding “regexp:” to the beginning, like so:
regexp:(Option A)|(Option B)
If you need to test for three or more conditions, all you need to do is extend the regular expression:
regexp:(Option A)|(Option B)|(Option C)
Regular expressions are really useful and infinitely versatile, and you can use them to make much more complex checks than this. If you need to use RegEx in your Selenium IDE test cases a lot, I recommend learning morea about it.