Explaining how the real world works!     RSS Feed The Automated Tester on Twitter The Automated Tester on LinkedIn The AutomatedTester on github

Selenium Regular Expression Tutorial

Thu 10 Jul 2008

If you have gone through all the tutorials available on this site you are probably starting to feel quite confident in your ability to automate testing of a web site. You have been able to record a script and add non-functional verifications to the test. You have also been able to find elements that have not been referenced properly by using XPath and in the last tutorial I showed you all how to add JavaScript blocks to your Selenium Scripts.

These three bits of knowledge can be further enhanced by working your way through this tutorial. I am pretty sure by the end this you will be able to automate testing of most sites and the things that you can't do will hopefully be covered in future advanced tutorials.

Lets start this tutorial like all of the others by creating a script to test the functionality below. Instead of the answer appearing in a text box it will appear as an alert that you will need to handle in your script.

  1. Value 1
    Value 2


    When creating this script you hopefully would have noticed the reason why you would have to use regular expressions. If you haven't see the reason, run your script again. Does it pass? I didn't think. So an why is that? The reason is because the alert has a dynamic section and to handle that we need to put a regular expression marker with a regular expression. Your command should look something like verifyAlert | regex:The answer is 2 when you ran it on (\w{3}\s+\w{3}\s+\d{2}\s+\d{4}\s+\d{2}:\d{2}:\d{2}\s+\w{3}+\d{4}\s+(\w{3}\s+\w+\s+\w{4})). This allows Selenium to carry through the test as long as alert has a string that fits into the regular expression phrase like what appears after the regex command.

    I have created an example script here for people to reference. You will notice that I have used globbing as well as well formed regular expressions in my script.

    NOTE: Handling Alerts with globbing may be a good practice because FireFox and Internet Explorer can write multi-line alerts slightly differently.

  2. We have seen what would be the best way to handle an alert that has dynamic data in it or if the browser thrown the alert in a different format to what you were expecting. We are now going to have a look at using full regular expressions to find what we want.


    We need to create a script to select each of the items in the list. The easy option in this case would be to just type out each of the options as a label e.g. select | sel_selection | label=Type 1. But what would happen if you were populating the select command with dynamic data. The best option would be to create a select command to look something like select | sel_selection |label=regexp:^[Tt]ype 1. This means that no matter what the case the first character is in, the test should always be able to find that string. An example script for this can be found here for handling the select input.

  3. Regular expressions do not only need to be find a label within a select box or validate an alert. You can also use regular expressions to validate dynamic text that appears on a page. As long as you can verify the text is there using the verifyX (verifyText, verifyTextPresent, etc.) or assertX (assertText, assertTextPresent, etc) commands you can use regular expressions.Just make sure that you put the regex or regexp marker at the beginning of the pattern.

Thank you for using this tutorial and I hope that you found it useful.

The answer to the regular expression question is: Email Validation. We have been doing some work on email validation and we have to make sure that we do it properly. While doing research into numerous RFCs revolving we found this the regular expression for validating emails. There seems to be a lot more valid characters in an email address than we had originally thought.

    Area: tutorials

blog comments powered by Disqus