Selenium Grid PHP Webdriver Example

PHP Webdriver is supported by Gridlastic, run your tests in the cloud with real browsers.

// Install with composer, https://github.com/facebook/php-webdriver

namespace Facebook\WebDriver;

  use Facebook\WebDriver\Remote\DesiredCapabilities;
  use Facebook\WebDriver\Remote\RemoteWebDriver;

  require_once('vendor/autoload.php');
  
  $host = 'https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub';
  // If the requested test environment is not registered with the hub
  // or busy, allow enough time (600000, 600 sec, see below) for the Gridlastic auto scaling
  // functionality to launch grid nodes with the requested environment.
  // Note: starting from selenium version 3.9.1 you must also include "platformName": "windows" in the request when testing with firefox and IE.
  $driver = RemoteWebDriver::create(
    $host, 
    array("platform"=>"WIN10", "platformName"=>"windows", "browserName"=>"chrome", "version"=>"latest", "video"=>"True"),
	600000,600000
  );
  $driver->manage()->window()->maximize(); // If Linux set window size, max 1920x1080
  $driver->get("https://www.google.com/ncr");
  $element = $driver->findElement(WebDriverBy::name("q"));
  if($element) {
      $element->sendKeys("php webdriver");
      $element->submit();
  }
  print "Video: " . VIDEO_URL . $driver->getSessionId();
  $driver->quit();  

The Gridlastic hub endpoint and the VIDEO_URL parameter used in this code example is displayed after launching your Gridlastic selenium grid. See documention Selenium Grid Configuration Parameters where to find these credentials and replace with your own.

Get a free account and launch your Gridlastic selenium grid, then run this code locally.

NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that "driver.quit()" is always called for proper test execution and creation of video recordings of failed tests.