1

Sauce Labs, Selenium WebDriver, and PHP Tutorials

Ready to get your project up and running with Selenium WebDriver and Sauce Labs? You've come to the right place. If you are new to Selenium, you might want to jump down and have a look at the complete tutorial.

Sausage

We recommend using the Sausage library to write your WebDriver tests in. Sausage is a Composer package that comes with a lot of features that make using Sauce trivially easy. It uses Paratest for running tests in parallel, and is based on PHPUnit's WebDriver class, so if you're familiar with that you're pretty much ready to go.

We'll assume for this tutorial that you have a recent (>=5.4) version of PHP installed on your system. If you need to get that set up, check out the complete setup tutorial. Now on to getting Sausage:

Sausage Setup for Mac and Linux

First, let's create a project directory that we'll use for this tutorial:

mkdir ~/sauce-tutorial && cd ~/sauce-tutorial

Now we can use this curl one-liner to download and install Sausage using your username and your Sauce access key. You can find your Sauce access key on your Sauce account page:

curl -s https://raw.github.com/jlipps/sausage-bun/master/givememysausage.php | SAUCE_USERNAME=<username> SAUCE_ACCESS_KEY=<access_key> php

This will start the download script and install Sausage in the ~/sauce-tutorial directory. Sausage checks for a number of requirements. If any are not met, notification messages display in the terminal. Fix any issues then run the curl command again.

You're all set up!

Run your first test suite

Now that Sausage is installed, let's run our first suite of tests in parallel, with the following command (paratest is simply a wrapper around PHPUnit that allows us to run multiple sessions in parallel):

Mac/Linux:

vendor/bin/paratest -p 2 -f --phpunit=vendor/bin/phpunit WebDriverDemo.php

While the tests are running head to your Sauce Labs account page and you can see them running 2 at a time. You can even watch them running live if you want by clicking the test names!

Understand the test framework

Let's have a look at what we just tested. If you want, open up WebDriverDemo.php in your own editor to follow along. Here's the code without the test logic itself, so you can see how we're setting up the test suite:

<?php

require_once 'vendor/autoload.php';

class WebDriverDemo extends Sauce\Sausage\WebDriverTestCase
{
    public static $browsers = array(
        // run FF15 on Windows 8 on Sauce
        array(
            'browserName' => 'firefox',
            'desiredCapabilities' => array(
                'version' => '15',
                'platform' => 'Windows 2012'
            )
        )//,
        // run Chrome on Linux on Sauce
        //array(
            //'browserName' => 'chrome',
            //'desiredCapabilities' => array(
                //'platform' => 'Linux'
          //)
        //),
        // run Chrome locally
        //array(
            //'browserName' => 'chrome',
            //'local' => true,
            //'sessionStrategy' => 'shared'
        //)
    );

A few things to note:

  1. The only autoload we need is vendor/autoload.php -- Composer takes care of loading all our classes for us.
  2. Our test suite extends the Sauce\Sausage\WebDriverTestCase class, which is where all of our Sauce-specific functionality comes from (like test pass/fail reporting, automatic test naming, etc...)
  3. We tell Sauce which browsers and platforms we want to use by passing in a desiredCapabilities key to the $browsers static array. (To see all the different possibilities, check out Sauce's browsers page).

And that's all we need to do to get the test suite running on Sauce! If you're interested in the details of the test functions themselves, check out the First Test section of the complete tutorial.

Wrapping up and next steps

For more information, please explore the various documentation available:

If you're tied to a different WebDriver library than Sausage and want to see how Sauce can be integrated into any PHP framework, check out the quickstart for existing test suites. Or if this tutorial went by a little too quickly, look below for the complete tutorial, where we go into much more detail about how Selenium and Sauce Labs work, and how to avoid certain pitfalls along the road to becoming a functional testing ninja.

Complete Tutorial

Want a bit more background on Selenium and Sauce Labs, or are you starting a new project and want to do it right from the beginning? These tutorials explain how to use PHP scripts to run automated Selenium WebDriver tests on the Sauce Labs cloud of Selenium servers.

We assume that you have some familiarity with PHP scripts, the command line interface in your operating system, and the fundamentals of automated testing. However, even if this is your first time working with PHP and automated testing you should be able to successfully follow these step-by-step instructions.

Let's get started! Click the first link below.