Idiom for Browser-Selectable Selenium Tests

For some time I’ve wanted to share an idiom I personally use and recommend when building Selenium Tests. This idiom allows to control which browsers are used to run the tests without needing to update test sources or configuration.

The simple ideas behind this idiom are:

  • Test code and configuration should not depend on the test environment.
  • Tests can be executed in any given browser, independently from others.
  • To change the browsers used for test execution, it is not needed to update test sources or configuration.
  • Selenium Grid URL and application URL are also configurable.
  • Both environment variables and Java system variables can be used.
  • All settings have sensible defaults.

I call this idiom ‘Browser-Selectable Tests’. I promise I keep thinking on a better name 🙂

Continue reading “Idiom for Browser-Selectable Selenium Tests”

Selenium WebDriver: Waiting for an application to be fully loaded

While working with Selenium WebDriver to automate web application tests in multiple browsers and application platforms, we found out some cases in which the application under test was not fully loaded when our tests executed, causing the test with the first browser to fail (but not the subsequent ones).

For example, when using Cargo to provision an embedded JBoss container, the ‘server ready’ flag was sent once the HTTP services were available, but the application was not loaded until first request hit the server.

After unsuccessfully trying with some Cargo settings, we considered adapting the test script to handle this.

To our surprise, the WebDriver API provided us with a very elegant, minimally disruptive, way of handling with this situation.

In previous posts we were simply using a pattern like this one to load a page, find a link and click on it:

driver.get(baseUrl);
driver.findElement(By.linkText("Find owner")).click();

In those posts we also used another pattern for waiting a page to be loaded after a link click or form submit:

(new WebDriverWait(driver, 5)).until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        return d.getCurrentUrl().startsWith(baseUrl + "/owners/search");
    }
});

Up to here, nothing new. WebDriverWait and ExpectedCondition allows for defining a wide range of conditions, like waiting for a new page to become available, waiting for a new field to be enabled or waiting for some AJAX response to be received. Combining the previous two snippets, we can write a condition that reads as: try loading this page until it contains a link with the text “Find owner”, but do not wait for more than five seconds. Here is the code we used:

// wait for the application to get fully loaded
WebElement findOwnerLink = (new WebDriverWait(driver, 5)).until(new ExpectedCondition<WebElement>() {
    public WebElement apply(WebDriver d) {
        d.get(baseUrl);
        return d.findElement(By.linkText("Find owner"));
    }
});

findOwnerLink.click();

With this small change in code, the test script waits until the application is fully loaded and the first page in the test sequence is available.

... Cargo output provisioning the server and JBoss starting up

[INFO] [talledLocalContainer] 09:20:06,004 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: S
tarting deployment of "petclinic.war"
[INFO] [talledLocalContainer] 09:20:06,004 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015876: S
tarting deployment of "cargocpc.war"
[INFO] [talledLocalContainer] 09:20:07,779 INFO [org.jboss.web] (MSC service thread 1-5) JBAS018210: Registering web co
ntext: /cargocpc
[INFO] [talledLocalContainer] JBoss 7.1.1.Final started on port [8180]

... JBoss Cargo adapter sends the 'ready' flag at this point

[INFO]
[INFO] --- maven-failsafe-plugin:2.8.1:integration-test (integration-test) @ org.springframework.samples.petclinic-rhc -
--
[INFO] Failsafe report directory: C:\projects\deors.demos\petclinic\org.springframework.samples.petclinic-rhc\target\fai
lsafe-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.springframework.samples.petclinic.it.NewPetFirstVisitIT

... Test script is waiting!

[INFO] [talledLocalContainer] 09:20:12,521 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) JBAS01
0404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
[INFO] [talledLocalContainer] 09:20:12,605 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/pe
tclinic]] (MSC service thread 1-2) Initializing Spring root WebApplicationContext
[INFO] [talledLocalContainer] 09:20:12,608 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-2
) Root WebApplicationContext: initialization started
[INFO] [talledLocalContainer] 09:20:12,648 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC
 service thread 1-2) Refreshing Root WebApplicationContext: startup date [Fri Jan 11 09:20:12 CET 2013]; root of context
 hierarchy
[INFO] [talledLocalContainer] 09:20:12,704 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC se
rvice thread 1-2) Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/applicationContext-jdbc.xm
l]
[INFO] [talledLocalContainer] 09:20:13,037 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC se
rvice thread 1-2) Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/applicationContext-dataSou
rce.xml]

... Rest of JBoss and Pet Clinic initialization - test is waiting for the app to be available

[INFO] [talledLocalContainer] 09:20:16,743 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deplo
yed "petclinic.war"
[INFO] [talledLocalContainer] 09:20:16,744 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deplo
yed "cargocpc.war"
Tests run: 6, Failures: 0, Errors: 0, Skipped: 5, Time elapsed: 11.745 sec

... The test executed successfully (some browsers skipped)

Out of curiosity, this does not happen with Cargo and Tomcat/Jetty, but it did no harm to add the wait!

Happy testing!

P.S.: More on WebDriver waits here:  http://seleniumhq.org/docs/04_webdriver_advanced.jsp

Selenium in One Minute (Video)

For some time I’ve been willing to record and upload a short video showing how Selenium works, working in a grid with several computers/browsers and an Android table as well.

For this short demo I’ve used the Spring PetClinic reference application, tweaked to work with Spring 3.0.6 and Tomcat 7.0.22.

This short video is slightly above one minute long. Hope you enjoy it!

Using Selenium to Automate Tests in Android Browser

Yes, it’s true. With Selenium you can automate UI tests for Android browsers.

Validating how a web application behaves in multiple browsers is a growing need, as users require using any browser of their choice to consume applications. Moreover, this need spans to mobile devices: applications are demanded to be ubiquituous, and so our tests should be.

To my (pleasant) surprise, it’s very easy to run automated tests in Android browsers. Selenium includes an Android driver that supports most of the Android browsers, both simulated and in physical devices.

Continue reading to know more.

Continue reading “Using Selenium to Automate Tests in Android Browser”

Test Automation with Selenium WebDriver and Selenium Grid – part 3: Continuous Integration

In part 1 in the series (read it here) I discussed about Selenium, the widely used tool for browser test automation, and I showed how easy is to setup a testing grid with multiple OS and browsers. In part 2 (read it here) I showed how to leverage WebDriver API to create and execute tests distributed across the grid that was created.

Now in part 3 I will show how to execute Selenium tests under a Continuous Integration process with Maven, Cargo and Jenkins, and how to gather code coverage metrics for those tests using Sonar and JaCoCo.

Continue reading “Test Automation with Selenium WebDriver and Selenium Grid – part 3: Continuous Integration”

Test Automation with Selenium WebDriver and Selenium Grid – part 2: Creating and Executing Tests

In part 1 in the series (read it here) I presented Selenium, a widely known tool for browser test automation.

Starting with Selenium 2, the most important components from the suite are Selenium WebDriver and Selenium Grid. In part 1 I showed how easy is to setup a testing grid with multiple OS and browsers. Now in part 2 I will show how to leverage WebDriver API to create and execute tests.

Continue reading “Test Automation with Selenium WebDriver and Selenium Grid – part 2: Creating and Executing Tests”

Test Automation with Selenium WebDriver and Selenium Grid – part 1: Setting Up the Grid

For a long while I’ve been “dying to play” with Selenium (www.seleniumhq.org and code.google.com/p/selenium/). I’ve heard and read very good things about this tool from colleagues and from the blogosphere.

Selenium is, in short, an open source tool to automate web browser interactions. A primary use case is, of course, browser test automation.

Selenium has greatly evolved with time, specially since the 2.0 release when the legacy Selenium project merged with Google’s WebDriver. Nowadays, Selenium offers a wide range of programming languages supported to write the tests, an impressive browser compatibility list, the ability to record tests from user interactions and, above it all in my opinion, the ability to re-execute tests across a grid of machines with various operating systems, browser families and versions.

Although Selenium seems to be primarily chosen for functional/regression test automation, it’s also a great choice – precisely because of the grid feature – for cross-browser compatibility testing: ensuring in an easy, cost-effective way, that our web applications are usable in all sorts of operating systems and browsers.

In this and forthcoming posts in a short series I will share my experiences setting up a Selenium Grid, building some automated tests for a simple Spring application, re-executing them from Eclipse IDE and finally re-executing them in continuous integration (including code coverage) with Maven, Cargo, Jenkins, Sonar and JaCoCo.

Continue reading “Test Automation with Selenium WebDriver and Selenium Grid – part 1: Setting Up the Grid”