The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing


External Resources

More Info

Page tree
Skip to end of metadata
Go to start of metadata

PAGE DEPRECATED: You can find related content in Sauce Labs Demonstration Scripts

Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications). This topic will provide you with an example of how to get your PHP tests up and running on Sauce.

Example Only

The code in this topic is presented as an example only, since your tests and testing environments may require specialized scripting. This information should be taken only as an illustration of how you could set up your tests with Sauce Labs, and is not directly supported by Sauce.


Before you get started, you should review the Best Practices for Running Tests.

You need to have these components installed to set up testing on Sauce with PHP:

Running the Example PHP Test

After you have everything installed and set up, you can run the WebDriverDemo.php example test that's included with Sausage to make sure everything works. 
Running the demo test on Mac OS X/Linux
vendor/bin/phpunit WebDriverDemo.php 
Running the demo test on Windows
bat vendor\bin\phpunit.bat WebDriverDemo.php 

This starts the PHPUnit test runner and gives it the name of an example test suite that Sausage downloaded. After a few moments you should see that PHPUnit has started. You might not see any output instantaneously, but eventually you will see a series of dots inching across the screen.

Each of these dots represents a test that successfully passed. If a test had an error or if it failed, PHPUnit prints an E or an F respectively.

While the tests are running, navigate to your Sauce Labs tests page. From there you'll be able to see each test as it queues, runs, and finishes. You'll notice that each test has a name -- that information is sent automatically by Sausage at the beginning of each test. Sausage also automatically notifies Sauce of the status of your tests when they complete.

Right now each test runs one at a time because PHPUnit currently doesn't support running multiple tests in parallel, however we've developed a way to do that which we'll describe in one of the later tutorials. For now, take advantage of the serial nature of the tests and click on a running test on your tests page. You'll jump to a detail view where, if you caught the test while it was running, you'll be able to watch Selenium controlling the browser. When the test finishes, the page updates with a video of the test and a list of the various Selenium commands that were sent.

If you don't catch a test while it's running, you can click the test's link on the Sauce Labs tests page to see the test's details and video.

Code Example

In this code example for PHP, the test runs a check to make sure that clicking a link brings you to the expected page. While simple, this example illustrates everything you'll need to run an automated test on Sauce Labs. First, it checks for your Sauce authentication credentials, which are set as environmental variables, and then sets the platform, browser, version, and other capabilities to use in the test. 

What You'll Need

Example Script

You can also clone this script directly from our GitHub repo.

  // Setup: $ php composer.phar require facebook/webdriver

  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $web_driver = RemoteWebDriver::create(
    array("platform"=>"Windows 7", "browserName"=>"chrome", "version"=>"40")

    Test actions here...


Running Local Tests

Developing websites/apps within your local network is secure and efficient. The drawback to this method is that local assets are not publicly-accessible on the Internet, so the browsers/devices in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect is a proxy server that creates a secure tunnel connection between the Sauce Labs virtual machine that runs your test and your local  network. You can also use Sauce Connect to test applications and websites that are located within your corporate firewall. Sauce Connect is not required to run tests on Sauce Labs, only in situations where the application or website you want to test is not publicly accessible. 

Running Tests in Parallel

Now that you’re running tests on Sauce, you may wonder how you can run your tests faster. One way to increase speed is by running tests in parallel across multiple virtual machines.

You can run your tests in parallel at two levels, and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and want to run on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example this would be more like 50 parallel tests. Doing this requires that your tests are written in a way that they do not collide with one another, as described in our Best Practice topics avoiding external test dependencies and avoiding dependencies between tests.

Check out Using Frameworks to Run Tests in Parallel for more information, and examples of framework setups for Java, Python, and other programming languages. 

Paratest is a command line tool for running PHPUnit that comes bundled with Sausage that you can use to run PHP tests in parallel on Sauce using the PHPUnit test framework. 

These examples, for Mac OS X/Linux and Windows, show the commands used to specify the path to the test file you want to run, and to simultaneously run two instances of PHPUnit. In these examples, the path to the WebDriverDemo.php demo test is shown, while the -p 2 arguments set the number of instances to run. 

Mac OSX/Linux Command for Launching Parallel PHP Tests
vendor/bin/paratest -p 2 -f --phpunit=vendor/bin/phpunit WebDriverDemo.php 
Windows Command for Launching Parallel PHP Tests
vendor\bin\paratest.bat -p 2 -f --phpunit=vendor\bin\phpunit.bat WebDriverDemo.php 

For more information, check out the example scripts in our GitHub repo

Reporting Tests to the Sauce Labs Dashboard

Sausage includes a number of features that make it easier for Sauce to report on your test results. 

By default, Sauce Labs doesn't know how to display the name of your test. Sausage comes up with a good name (TestClass::testFunction) and reports it with your test so it's easy to find on your dashboard. Similarly, Sauce has no way to know if a particular test passed or failed. Sausage catches any failed assertions and reports the status of the test to Sauce after it's complete. Upon test failure Sausage will generate an authorized link to the failed job report on the Sauce Labs website, to facilitate reporting to people who need to know the details of the test. The job remains private (unless you change the status yourself), but others can follow the link without needing to log in with your credentials. See the topic Building Sharable Links to Test Results for more information.

You should also follow our recommended best practice of adding build numbers, tags, and other identifying information to your tests so you can easily find and manage them in your test results and archives pages, and associate tests with build numbers in your continuous integration pipeline.