PHPUnit and Selenium: Running PHPUnit Automated Testing Scripts on Cloud Selenium Grid

Execute your automation test scripts with Selenium WebDriver instantly through LambdaTest which is a cloud-based, cross browser testing tool offering a Selenium grid consisting 2000+ browsers and browser versions running on real operating systems to pace up automation testing of your web-app or website. In this topic, you will learn how to automate your website testing using PHPUnit Selenium test framework on LambdaTest.

By the end of this topic, you will be able to:

  • Run a sample PHPUnit Selenium test script on LambdaTest automation.
  • Set up an environment for testing your locally hosted web pages.
  • Understand & configure PHPUnit desired capabilities for executing a Selenium test.
  • Explore advanced features of LambdaTest.

Prerequisites For Running PHPUnit automated testing with Selenium

Before you begin PHPUnit Automated Testing with Selenium, be ready with the below essentials:

  • Make sure that you have the latest PHP installed on your system. If you are using a Mac then PHP would be installed by default. For Linux you would need to enter the below command in terminal:

    sudo apt-get install curl libcurl3 libcurl3-dev php

    For Windows, you can download PHP from here. Also, refer to this documentation for ensuring the accessibility of PHP through Command Prompt(cmd).

  • Install PHPUnit Selenium Test framework using the below command with composer:

      $ php -r "copy('', 'composer-setup.php');"
      $ php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
      $ php composer-setup.php
      $ php -r "unlink('composer-setup.php');"
      $ php composer.phar install phpunit/phpunit-selenium
  • Once you have composer installed, you can leverage Composer to download and install Selenium dependencies. For that first create a composer.json file in your directory and add the following code to the json.

        "require": {
          "phpunit/phpunit-selenium": "*",
          "facebook/webdriver": "dev-master"
        "scripts": {  
          "test": "vendor\\bin\\phpunit tests\\LambdaTest.php"   
        "autoload": {
          "classmap": ["lib/"]

Check out our GitHub repository for finding all the code references that will be discussed in this topic.

Getting Started With PHPUnit Selenium Test Execution on LambdaTest

Let us have a look at an example which will validate your LambdaTest credentials for authentication purpose. Later, the code will select the basic capabilities such as OS, browser, browser version and so on. This code is also available on our PHPUnit GitHub repository for you to clone and play around.(smile)

    require 'vendor/autoload.php';
    class LambdaTest extends LambdaTestSetup {
    public function todotest() {
        $itemName = 'Yey, Lets add it to list';
        $element1 = self::$driver->findElement(WebDriverBy::name("li1"));
        $element2 = self::$driver->findElement(WebDriverBy::name("li2"));
        $element3 = self::$driver->findElement(WebDriverBy::id("sampletodotext"));
        $element4 = self::$driver->findElement(WebDriverBy::id("addbutton"));           
        self::$driver->wait(10, 500)->until(function($driver) {
           $elements = $driver->findElements(WebDriverBy::cssSelector("[class='list-unstyled'] li:nth-child(6) span"));
           return count($elements) > 0;
        $element5 = self::$driver->findElement(WebDriverBy::cssSelector("[class='list-unstyled'] li:nth-child(6) span"));
        $this->assertEquals($itemName, $element5->getText());

Now, Create LambdaTestSetup.php.

require 'vendor/autoload.php';
require 'lib/globals.php';
class LambdaTestSetup extends PHPUnit\Framework\TestCase
    protected static $driver;
    public static function setUpBeforeClass()
        $url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."";
        $task_id = 0;    
        $desired_capabilities = new DesiredCapabilities();
        $desired_capabilities->setCapability('version', $GLOBALS['LT_BROWSER_VERSION']);
        $desired_capabilities->setCapability('platform', $GLOBALS['LT_OPERATING_SYSTEM']);
        $desired_capabilities->setCapability('name', "PHPUnit Sample Test");
        $desired_capabilities->setCapability('build', "PHPUnit Selenium Sample");
        $desired_capabilities->setCapability('network', true);
        $desired_capabilities->setCapability('visual', true);       
        self::$driver = RemoteWebDriver::create($url, $desired_capabilities);      
    public static function tearDownAfterClass(){

Now, Create globals.php

$config_file = getenv('CONFIG_FILE');
if(!$config_file) $config_file = 'config/single.conf.json';
$GLOBALS['CONFIG'] = json_decode(file_get_contents($config_file), true);
if(!$GLOBALS['LT_BROWSER']) $GLOBALS['LT_BROWSER'] = $GLOBALS['CONFIG']['environments']["browserName"];

Now, finally add single.conf.json.

  "capabilities": {
    "build": "PHPUnit Selenium Sample",
    "name": "PHPUnit Sample Test"
  "environments": {
    "browserName": "chrome",
    "version": "69.0",
    "platform": "WIN7"

Execute The PHPUnit Selenium Test

You would need to execute the below command in your terminal/cmd.

composer install
composer test
1 Like