Please post your Web Driver questions in official Web Driver forum

Friday, January 6, 2012

Selenium 2 methods are no more Weird!!!

tl;dr A long post about using Selenium 1 sort syntax with Selenium 2/WebDriver.

I have seen automation teams suffering from verbosity of web driver way of writing code. One automation guy told me once that he does not want to use Selenium 2 on a brand new project because he finds its syntax utterly weird. But before discussing "Weirdness" of Selenium 2 syntax, let us agree on fact that teams migrating from pure manual testing to automation code find it more weird than ever. While selenium.type("elementLocator", "testData") looks simple to them. Using driver.findElement(By.xPath).sendKeys("testData") looks excessive coding to be able to perform same operation.
Herein I am illustrating the way to wrap Selenium 1 and Selenium 2/WebDriver methods. Don't consider this as the only way of wrapping, I am sure there are better solutions than this.

This is how Selenium1 code (Google again) goes. In the following example there are 5 basic entities -

  • SelTestCase class - which sets up test bed for Selenium
  • ActionDriver class - which carries out operations on page
  • Page element classes - which store element locators
  • Page object classes - which represents services offered by page and
  • Test class - yup, it tests.
This example assumes that you have knowledge of testng

Now same example is converted to Selenium 2. Notice that how small SelTestCase class is and there are no more waitForPageToLoad statements. The biggest change is the page element class, which return By object instead of String objects which are returned in case of Selenium 1 page element classes. So if you compare the searchGoogle method in GoogleHomePage class of both Selenium 1 and Selenium 2 examples then both of them use type(GoogleHomePageElements.getSearchBox(), testdata); to perform type operation.

Here is Selenium 2 example -

So do you still find Selenium 2 method calls weird?
Fork me on GitHub