Selenium tests over Multiple Browsers

The Selenium API is great for writing Java tests for web applications. An important aspect of it is the ability to quickly regression test over multiple browsers.

A common pattern I ise is shown in the following code snippet. A common base method is called by various methods configured for different browsers (appended with “_FF” for Firefox for example).

I want to test over three browsers so I create a method for each browser which all call the same method, the only difference being that a different web driver is configured. It may seem cumbersome to replicate the method over three browsers, but one of the main benefits is that I can individually test/debug a method specific to one browser and one method. Something I really do a lot of!

The “MyWebPage” class being created is just using the page object pattern to mask the web application I want to test. It holds a web driver, and the base URL. A load() method calls the initial load of the web page. In this class all other business logic can be found.

More info here on page objects can be found here:
http://code.google.com/p/selenium/wiki/PageObjects

The super class for my tests called MultibrowserSeleniumTest really only initialises the web drivers and destroys them, as well as a references MyWebPage from the page object pattern.

Using lazy loading for the get****Driver methods, prevents having to load the driver for each method, which can become very tedious and time consuming. A browser instance should last over the life time of a test class.

It is important that we close off the browsers @AfterClass, and not after every method, otherwise what is the point of lazy loading the drivers?
Your test case method should then look something like this :

TIPS

  • Always keep the test code as flat as possible. All complexity should be hidden inside the page object pattern classes.
  • User a pattern similar to my super class, that way only the browsers are loaded as needed.
  • Design the tests so that all the tests can share the same lifetime of the browser instance (which is why the browsers are closed off in the @afterClass method).

 

 


Posted in Design Pattern | Tagged , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

THREE_COLUMN_PAGE