Start your webtests with Selenium2, Maven, TestNG – now

Every time you are playing around with web tests you think: why have I waited so long. Doesn’t matter, you have done it and that’s it what matters.

The last time I used Selenium 1 which is very often used for web tests. Selenium 1 is great, but has a few limitations. It is based around an injected Javascript library – the Selenium core. It works, but starting the browser with the Selenium core is often a little bit tricky. With every browser update you hope that it is still running or hope for the update, which needs to be included in the Maven plugin, is pushed to the Maven mirrors, is really working on all systems, …

But now there is Selenium 2 with the merge of Webdriver. It is no longer a Javascript core, it is a native browser plugin. And now you are much deeper inside the browser. It feels more stable from the first second and it works really well. With the Webdriver part of Selenium 2 there is a new API. But it isn’t very complicated, just different.

I can give you a damn short example which works really well for me and runs through our entire webpage. The great part: it takes screenshots of the complete page (not just the visible part) and stores them in the target folder.

First of all, the Maven pom:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>de.viaboxx.tests</groupId>
  8. <artifactId>integrationtest</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.testng</groupId>
  14. <artifactId>testng</artifactId>
  15. <version>5.13.1</version>
  16. <scope>test</scope>
  17. </dependency>
  18.  
  19. <dependency>
  20. <groupId>org.seleniumhq.selenium</groupId>
  21. <artifactId>selenium</artifactId>
  22. <version>2.0a6</version>
  23. </dependency>
  24.  
  25. <dependency>
  26. <groupId>org.seleniumhq.selenium</groupId>
  27. <artifactId>selenium-firefox-driver</artifactId>
  28. <version>2.0a6</version>
  29. </dependency>
  30. </dependencies>
  31.  
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.apache.maven.plugins</groupId>
  36. <artifactId>maven-surefire-plugin</artifactId>
  37. <configuration>
  38. <skip>true</skip>
  39. </configuration>
  40. <executions>
  41. <execution>
  42. <phase>integration-test</phase>
  43. <goals>
  44. <goal>test</goal>
  45. </goals>
  46. <configuration>
  47. <skip>false</skip>
  48. </configuration>
  49. </execution>
  50. </executions>
  51. </plugin>
  52. <plugin>
  53. <groupId>org.apache.maven.plugins</groupId>
  54. <artifactId>maven-compiler-plugin</artifactId>
  55. <configuration>
  56. <source>1.5</source>
  57. <target>1.5</target>
  58. </configuration>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

And it follows the hole logic for the test placed under src/test/java/de/viaboxx/tests/SeleniumTest.java:

  1. package de.viaboxx.tests;
  2.  
  3. import org.apache.commons.io.FileUtils;
  4. import org.openqa.selenium.OutputType;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. import org.testng.Assert;
  7. import org.testng.annotations.Test;
  8.  
  9. import java.io.File;
  10. import java.io.IOException;
  11.  
  12. public class SeleniumTest {
  13. private static final String FOLDER_NAME = "target/screenshots/";
  14.  
  15. @Test
  16. public void website() throws IOException, InterruptedException {
  17. FirefoxDriver driver = new FirefoxDriver();
  18. driver.get("http://www.viaboxxsystems.de");
  19. Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Innovation");
  20. FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "startpage.png"));
  21.  
  22. driver.findElementByLinkText("Team").click();
  23. Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Team");
  24. FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "team.png"));
  25.  
  26. driver.findElementByLinkText("Blog").click();
  27. FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "blog.png"));
  28.  
  29. driver.findElementByLinkText("Impressum").click();
  30. FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "impressum.png"));
  31.  
  32. driver.close();
  33. }
  34.  
  35. }

Run it with:

mvn integration-test

Readable, isn’t it. And that’s all – a complete working infrastructure for your web tests. The example works with Firefox, but you can also run headless with HtmlUnit, or make your tests running in IE or Chrome. The great thing: just put the application in your continuous integration server and run it on a your defined build agent.

After just one evening I fall in love with the new Selenium 2. Why? Because it works much more stable in kind of browser starting and running. I think it is a great way to go.

Watch the screenshots, just taken from the target folder:

startpage

team

Download the entire example as zip:

viaboxxtests.zip

More documentation:

With this example you have no excuse to wait for your automated web tests. Start it now and give a feedback about your impressions with Selenium 2 in the comments.

2 Responses

  1. Hi, it's a very nice example, I tried to download the zip you have in this post but I got a 404. Could you supply another link for the file? Thank you
    • Simon Tiffert
      Hi Jairo, thanks for the comment. The download is now working again, but have a look at new dependencies in the pom.xml, as the file is 5 years old. Cheers, Simon