BlogGetting Started with Selenium2 Web Driver and PHPUnit

4th November 2013

This post is to help me consolidate what I'm doing and what I've learnt so far. It's really just a brain-dump recording my experimentation rather than providing advice. However, if you find anything helpful here then that's great!

Assumptions

Using a Mac already running PHP v5.3+

Getting Started

Download the latest Selenium server from here…

http://docs.seleniumhq.org/download/

Run the server using this command…

$ java -jar PATH-TO-FILE i.e. $ java -jar /usr/local/bin/selenium-server-standalone-2.37.0.jar

Now that the Selenium server is up and running we can start writing some tests!

Useful Links to Help with Setup

How to Use Selenium 2 With PHPUnit

http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/

PHPUnit Selenium setup

http://phpunit.de/manual/3.7/en/selenium.html

Basic PHPUnit Tests

I started off creating a simple object with some basic tests…

class BasicTest extends PHPUnit_Extensions_Selenium2TestCase {
    public function setUp(){
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/');
    }

    public function testHomeTitle(){
        $this->url('/home');
        $this->assertEquals("Welcome", $this->title());
    }

    public function testAboutusTitle(){
        $this->url('/about-us');
        $this->assertEquals("About us", $this->title());
    }
}

Other Tests

Documentation about the PHPUnit implementation of Selenium2 (Web Driver) seems to be pretty sparse at the moment but I've found these site very useful in finding out about the types of tests that you can run…

https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php

Run the test

Save your script. I called mine basictest.php, representing the object name. Now, use the following command to run your script…

phpunit -v basictest

Problems I've Encountered

Page not found, exception thrown

The main problem I've encountered seems to be that the when a page isn't found, instead of being marked as a failed test, an exception will be thrown. This is not ideal and although I may be doing something wrong to cause this, I'm having trouble finding anything out.

Waiting for loading

Another major problem seems to the ability to wait. You get your script to click a link, the script performs some ajax action and changes part of the page. However, it is tricky to tell your test script to wait until the ajax has run. I've read many tutorials and Stack Overflow answers, most of which are hacks, on this but it doesn't seem that this is a nice answer to this dilemma yet and i haven't managed to get anything to work on this front as yet.

What's next?

In the future I'd like to create a modular, extendable testing solution which is easy to set up and run and can be applied to any site nice and easily.

Let's Talk