Setting up your PHP environment for Selenium webdriver testing

Selenium WebDriver is a tool for automating web testing. It automates testing by making direct calls to the browser allowing you to compare expected against actual results. It makes direct calls to the browser using javascript.

Advantages of web test automation are:

  • run test on multiple browsers
  • receive rapid feedback
  • documentation of test cases
  • repeatable
  • find defects missed by manual teseting

But manual testing may be a better option if you know the user interface will change considerably.

Download composer

Composer helps you manage dependencies in PHP simular to node’s npm and ruby’s bundler. Run the following in your terminal to get composer.

php -r "readfile('');" > composer-setup.php
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'a52be7b8724e47499b039d53415953cc3d5b459b9d9c0308301f867921c19efc623b81dfef8fc2be194a5cf56945d223') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

After running the above script, it will place a composer.phar file in your current working directory. The .phar extension stands for PHP archive used for bundling entire PHP applications. Lets move the composer.phar so we can use it globally.

mv composer.phar /usr/local/bin/composer

To manage dependencies edit the composer.json file. If you don’t have the file create one in your project directory.

require keyword tells composer which packages our project depends on. Then underneath you specify package and version. The syntax is “package name:version constraints”.

    "require-dev": {
        "phpunit/phpunit": "*",
        "facebook/webdriver": "dev-master"

We are using a thirdparty binding for Selenium WebDriver provided by Facebook. On your first run, type this to get dependencies

composer install 

This will place a vendor folder in your present working directory with all your dependencies. If you modify composer.json you can use update to grab the latest changes

composer update 

Download selenium server

Go to selenium and download the server

Start running server by typing below and replace # with the version you downloaded.

java -jar selenium-server-standalone-#.jar

The selenium server needs to be started when you run your test code.

Running the webdriver test code

Put the following code in a file called GoogleTest.php in your working directory. This is a simple test scenario for testing the Google search box. The code will launch your browser, navigate to and search for ‘what is composer’ in the Google search box.

class TestGoogle extends PHPUnit_Framework_TestCase {

    protected $url = '';
     * @var RemoteWebDriver
    protected $webDriver;

    public function setUp()
        $capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'firefox');
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);

    public function tearDown()

    public function testSearch()

        // find search field by its name
        $search = $this->webDriver->findElement(WebDriverBy::name('q'));

        // typing our search query
        $this->webDriver->getKeyboard()->sendKeys('what is composer');

        // submit our query


Then in your terminal, type:

vendor/bin/phpunit GoogleTest.php

This will launch the browser specified in the code and run the tests. Make sure you start the selenium server first, otherwise you will get an error.

Further Reading:

Working with PHPUnit and Selenium Webdriver
Facebook php-webdriver binding