Saturday, November 12, 2011

A simple WebDriver runner for QUnit


I have been working on a Confluence plugin during my 20% time and, as part of it, writing a number of qunit tests. I was looking for a way to run them as part of CI and found this qunit-maven-plugin out there. After trying to use it unsuccessfully for a couple of minutes, I confess to falling into the well-known “reinvent the wheel” mentality trap.

I’ve been working with WebDriver quite a lot and the idea came to me that it would not be hard to create a simple qunit runner using it. Well, here it is.

How to use it?

Download and include it in your pom.xml (in this case I downloaded the .jar to the /lib folder of my project):

<dependency>
    <groupId>org.farmas</groupId>
    <artifactId>qunit-webdriver-runner</artifactId>
    <version>0.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/qunit-webdriver-runner-0.1.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.atlassian.selenium</groupId>
    <artifactId>atlassian-pageobjects-elements</artifactId>
    <version>2.1.0-m8</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.24</version>
    <scope>test</scope>
</dependency>

Then you can use the QunitWebDriver from your test (in this case I am using JUnit):

public class QunitTest {

    public static QunitWebDriver driver;

    @BeforeClass
    public static void start() throws Exception
    {
        // these are the class loaders particular to my project
        driver = QunitWebDriver.start(
                        QunitTest.class.getClassLoader(), 
                        TagParser.class.getClassLoader()); 
    }

    @Test
    public void test()
    {
        driver.runTest("/spec.htm");
    }

    @AfterClass
    public static void stopServer() throws Exception
    {
        driver.stop();
    }
}

How does it work?

  • The module assumes that all your javascript, html and css files (product and test) are included as resources.
  • At the beginning of your test, start a QunitWebDriver passing the class loaders that are capable of locating all your resources.
  • QunitWebDriver will start a local webserver (using Jetty) that can serve requests from the resource files.
  • QunitWebDriver will then download, configure and start an instance of FireFox browser using WebDriver.
  • Lastly, you call QunitWebDriver.runTest() passing the html page that will load qunit and run your tests.
  • QunitWebDriver checks the UI of the page to verify that no test has failed.
blogpost

atlassian-pageobjects-elements?

You might be wondering what’s up with this dependency. This is the Atlassian WebDriver PageObject library, another project that I worked on at Atlassian. One of the things I get for free by using it is auto-download and configuration of browsers which was very useful to get this thing going quickly

Watch it live

Unable to display content. Adobe Flash is required.
qunit-webdriver-runner

- Federico

0 comments:

Post a Comment