Skip to main content

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 perform Selenium automation testing using PHPUnit on LambdaTest.

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

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

Prerequisites For Running PHPUnit automated testing with Selenium#


All the code samples in this documentation can be found in the PHPUnit LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

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('https://getcomposer.org/installer', '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

If you don’t have composer installed in your system then click here.

  • 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.
//composer.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)

//LambdaTest.php<?php    require 'vendor/autoload.php';     class LambdaTest extends LambdaTestSetup {     public function todotest() {        $itemName = 'Yey, Lets add it to list';        self::$driver->get("https://lambdatest.github.io/sample-todo-app/");        $element1 = self::$driver->findElement(WebDriverBy::name("li1"));        $element1->click();                                  $element2 = self::$driver->findElement(WebDriverBy::name("li2"));        $element2->click();                     $element3 = self::$driver->findElement(WebDriverBy::id("sampletodotext"));        $element3->sendKeys($itemName);                     $element4 = self::$driver->findElement(WebDriverBy::id("addbutton"));                   $element4->click();                     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.

//LambdaTestSetup.php<?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'] ."@hub.lambdatest.com/wd/hub";        $task_id = 0;             $desired_capabilities = new DesiredCapabilities();        $desired_capabilities->setCapability('browserName',$GLOBALS['LT_BROWSER']);        $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");        self::$driver = RemoteWebDriver::create($url, $desired_capabilities);                   }         public static function tearDownAfterClass(){        self::$driver->quit();    }}?>

Now, Create globals.php

//globals.php<?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); $GLOBALS['LT_USERNAME'] = getenv('LT_USERNAME');if(!$GLOBALS['LT_USERNAME']) $GLOBALS['LT_USERNAME'] = "<YOUR USERNAME>"; $GLOBALS['LT_APPKEY'] = getenv('LT_APPKEY');if(!$GLOBALS['LT_APPKEY']) $GLOBALS['LT_APPKEY'] = "<YOUR ACCESS_KEY>"; $GLOBALS['LT_BROWSER'] = getenv('LT_BROWSER');if(!$GLOBALS['LT_BROWSER']) $GLOBALS['LT_BROWSER'] = $GLOBALS['CONFIG']['environments']["browserName"]; $GLOBALS['LT_BROWSER_VERSION'] = getenv('LT_BROWSER_VERSION');if(!$GLOBALS['LT_BROWSER_VERSION']) $GLOBALS['LT_BROWSER_VERSION'] = $GLOBALS['CONFIG']['environments']["version"]; $GLOBALS['LT_OPERATING_SYSTEM'] = getenv('LT_OPERATING_SYSTEM');if(!$GLOBALS['LT_OPERATING_SYSTEM']) $GLOBALS['LT_OPERATING_SYSTEM'] = $GLOBALS['CONFIG']['environments']["platform"]; ?>

Now, finally add single.conf.json.

//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#

composer installcomposer test
Last updated on