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:
Before you begin PHPUnit Automated Testing with Selenium, be ready with the below essentials:
1 |
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).
1 2 3 4 5 |
$ 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//composer.json { "require": { "phpunit/phpunit-selenium": "*", "facebook/webdriver": "dev-master" }, "scripts": { "test": "vendor\\bin\\phpunit tests\\LambdaTest.php" }, "autoload": { "classmap": ["lib/"] } } |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//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"); $desired_capabilities->setCapability('network', true); $desired_capabilities->setCapability('visual', true); self::$driver = RemoteWebDriver::create($url, $desired_capabilities); } public static function tearDownAfterClass(){ self::$driver->quit(); } } ?> |
Now, Create globals.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//single.conf.json { "capabilities": { "build": "PHPUnit Selenium Sample", "name": "PHPUnit Sample Test" }, "environments": { "browserName": "chrome", "version": "69.0", "platform": "WIN7" } } |
You would need to execute the below command in your terminal/cmd.
1 2 |
composer install composer test |