June 16, 2013

There are many products for automated testing optimization presented by Selenium: RC, GRID, IDE, WebDriver.  To my mind, the most interesting  is the last one - Selenium WebDriver (SWD). Let’s take a closer look at it.

1. What is SWD?

SWD is a library providing an interface to emulate user’s actions in browser, manage browser’s behavior for other software, manage data flow between browser and these pieces of software.

SWD is being developed by Seleniumhq team and has bindings for many programming languages: Java, C#, Python, Ruby, JS, Objective-C and Perl.

There are no Php bindings developed by seleniumhq team, but there is a couple of third-party bindings. The most recommended was developed by Adam Gouche.

SWD supports all mainstream browsers  Google Chrome, Mozilla Firefox, IE, Safari, Operа (3rd party) + mobile OS browsers (IPhone and Android). You can find more detailed information about browser support here.

2. SWD functionality

SWD has pretty rich API reference, so let’s stop at the most basic and interesting things.

  • Get page by URL
  • Locate element at the page (via css selectors/xpath)
  • Forms interaction: fill textfield, file uploading, choose an item from selectbox, check the box, click the button etc.
  • Execute JS code on the page
  • Drag’n’Drop emulation
  • Take a screenshot
  • Cookies management

SWD also has functionality helping to test ajax-based web-applications. SWD has two kinds of wait functions: implicit wait (wait for given amount of time and then continue execution) and explicit wait (SWD waits for certain conditions to be fullfilled, e.g., some element should appear/dissapear from the page). This functionality allows us to write tests for ajax-based applications in agile manner.

It is a small part of functionality the SWD has.

By the way, SWD is not a test library. So, you have to add some test library to your project to write a test using the SWD. It could be JUnit, NUnit, PHPUnit etc. See full list here.

Let’s test some examples.

3. SWD-based testing framework

We need to have some web-application/website, SWD, testing library and some IDE.

All the examples were written using SWD’s Java binding, JUnit and Eclipse. The test set and all other files needed for test execution are being stored at the bitbucket repository.

SWD has rich functionality, but it is kind of rough.  So, it is widely spreaded practice among the developers to write separate testing framework wrapping the SWD library for the given project. For example, SWD has a click function and wait function, but it has no clickAndWait function which is very useful. All pros of this approach are obvious:

  • easy scalability
  • every member of the team works with the same set of tests
  • independency from the SWD core changes

4. SWD tests in Drupal

Drupal is actively developing system, so SWD didn’t simply pass it by. There is a module called Selenium utilizing SWD functionality. It has several advantages:

  • test structure and syntax are mostly the same as core Simpletest module has, so developers who dealt with writing Simpletest’s tests will learn the new things fast
  • tests written using this module are running as Drupal native test through Configure->Development->Testing-> Run tests
  • allows test JS

This module implements DrupalSeleniumWebTestCase class which extends DrupalWebTestCase class of the Simpletest module. SeleniumWebDriver and SeleniumWebElement (analogues of the native SWD lib classes) are also implemented. To get this module’s test working you should have Selenium Server enabled. All the interactions between tests and Selenium Server are going through cURL requests.

5. Conclusion

So, we saw, that the SWD is a powerful tool drastically saving  time and efforts of the team members. It allows to test some typical functionality (including ajax) whith pre-defined behavior. This tool can be used to write agile automatic tests and can simplify the regression testing process.

Was this article helpful? Click to rate: 
Average: 5 (1 vote)

You may also like

When you create a theme which is supposed to support different...