Selenium and PHP

Applies to CrossBrowserTesting SaaS, last modified on October 07, 2021

Get started

With Selenium, you can use the PHP Bindings to easily create browser automation tests. Using CrossBrowserTesting, you can automate all of your PHP test scripts against our huge collection of browsers and devices. To learn more about the Selenium API, we recommend you read Selenium documentation.

Note: You will need a Username and Authkey to run your tests on CrossBrowserTesting To get yours, sign up for a free trial or purchase a plan.


To get started, we will need Composer in order to gather the necessary dependencies for our test. You can find download instructions for Composer here. Once we have Composer in place, we need to add the following libraries:

php composer.phar require facebook/webdriver
php composer.phar require phpunit/phpunit
php composer.phar require rmccue/requests

Set up our test

We will be setting our test up as a PHPUnit TestCase. This allows us to make assertions about our webpage as we go along. Copy the following code into your favorite text editor, and be sure to change the username and authorization key to your email address for CrossBrowserTesting and the private key found on the Manage Account section of our app.

#Insert opening PHP tag
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedConditions;
use PHPUnit\Framework\TestCase;

class ToDoTest extends TestCase {
    protected $driver;
    static private $username = "YOUR_USERNAME";
    static private $authkey = "YOUR_AUTHKEY";
    static private $sessionId = NULL;

    public function setScore($score) {
        print "Setting the score to ". $score. "\n";
        $url = "" . self::$sessionId;
        $options = array('action'=>'set_score', 'score'=>$score);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, self::$username . ":" . self::$authkey);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($options));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);
        curl_close ($ch);

    public function takeSnapshot(){
        $url = "" . self::$sessionId . "/snapshots";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, self::$username . ":" . self::$authkey);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);


    public function setUp() {
        $capabilities = array("name"=> "Selenium Test Example PHP",
                  "build"=> "1.0",
                  "browser_api_name"=> "Chrome76",
                  "os_api_name"=> "Win10",
                  "record_video"=> "true",
                  "record_network"=> "false",);

        $host = "http://" . self::$username . ":" . self::$authkey . "";
        $this->driver = RemoteWebDriver::create($host, $capabilities );
    public function testToDos() {
        try {
            self::$sessionId = $this->driver->getSessionId();
            print "this is the session id: " . self::$sessionId;

            print "\nNavigating to URL\n";

            $this->assertEquals("Todo App -",$this->driver->getTitle());

            // if we've made it this far, assertions have passed and we'll set the score to pass.

        } catch (Exception $ex) {
            // if we caught an exception along the way, an assertion failed and we'll set the score to fail.

            print "Caught Exception: " . $ex->getMessage();

    public function tearDown() {

Once we have our script setup, we can run the test and see our results using Phpunit. Run your test by executing the following command:

phpunit php_example.php

Jump over to our app, and you should see the test being executed on Windows 10 and Chrome 76. If our test case passes, we should see the following output from PHPUnit:

OK (1 test, 1 assertions)

Test capabilities

CrossBrowserTesting allows you to add certain capabilities to your test, like video recordings, naming structure and more.

Name tests

Naming your tests can help organize your automated test cases for easier debugging. You can also mark Build number to denote releases.

$capabilities = array("name"=> "Selenium Test Example");

$capabilities = array("build"=> "1");

Choose browsers

To choose your environment, you can select from Browsers, Operating System, Resolutions, and Devices. You can use our easy Selenium Wizard to select your configuration or hit our browser list endpoint.

Record videos

For superior debugging capabilities, CrossBrowserTesting offers the ability to record a video of your Selenium test session. You can find a copy of your test results here.

$capabilities = array("record_video"=> "true");

Record network

To record the network packets during your test for performance debugging, set the following to "true".

$capabilities = array("record_network"=> "true");

Run a local test

With our Local Tunnel, you can run a test on a local URL or behind your company's firewall. There are two different ways to connect the CrossBrowserTesting local tunnel: our Node.js client and our Chrome Extension. You can read more about setting up, starting, and using your tunnel connection here. When a tunnel is open, any automated test can access websites locally, behind a firewall, or across a proxy. There is no need to set a capability during your test.

See Also

Selenium and C#

Highlight search results