3

We'd like to run our Selenium tests along with our other unit tests in our build scripts, but given that the builds are running on Jenkins, which is running as a service, the tests need to be run headless. Our Selenium tests are written in PHP, and everything that I've seen so far seems to apply to JavaScript or Python.

Is there any way for us to run our PHP Selenium tests headless (preferably using the same drivers as when not running headless so we can detect problems with specific browsers)?

RobH
  • 1,607
  • 2
  • 24
  • 44
  • I would imagine you can execute them just like any other script `php your_test.php`. You surely must know how to run those tests if you've written them, perhaps you've meant something else with your question - I don't think I've understood it correctly. – avloss Jul 13 '20 at 19:22
  • It's not a question of how to run the tests. The question is how to get them to run headlessly. If you check [this question](https://stackoverflow.com/questions/44374797/jenkins-selenium-do-not-run-test-headlessly), Jenkins, when run as a service, runs tests headlessly, meaning that they fail if you try to run them otherwise. We'd like to be able to run our tests with our set-up the way it is, rather than have to go through any of the hoops mentioned in the accepted answer to that question to get it to not run headlessly in Jenkins. – RobH Jul 13 '20 at 20:54

2 Answers2

6

This has been improved in php-webdriver 1.11.0 (2021-05-03).

Start headless Chrome

$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(['--headless']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);

// Start the browser with $capabilities
// A) When using RemoteWebDriver::create()
$driver = RemoteWebDriver::create($serverUrl, $capabilities);
// B) When using ChromeDriver::start to start local Chromedriver
$driver = ChromeDriver::start($capabilities);

See php-webdriver wiki article for more Chrome examples.

Start headless Firefox

$firefoxOptions = new FirefoxOptions();
$firefoxOptions->addArguments(['-headless']);

$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);

// Start the browser with $capabilities
// A) When using RemoteWebDriver::create()
$driver = RemoteWebDriver::create($serverUrl, $capabilities);
// B) When using FirefoxDriver::start to start local Geckodriver
$driver = FirefoxDriver::start($capabilities);

See php-webdriver wiki article for more Firefox examples.

Ondrej Machulda
  • 998
  • 1
  • 12
  • 24
0

Found this in the php-webdriver docs:

use Facebook\WebDriver\Remote\DesiredCapabilities;

$desiredCapabilities = DesiredCapabilities::firefox();
    .
    .
    .
// Run headless firefox
$desiredCapabilities->setCapability('moz:firefoxOptions', ['args' => ['-headless']]);

$driver = RemoteWebDriver::create($host, $desiredCapabilities);
RobH
  • 1,607
  • 2
  • 24
  • 44