4

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)?

2
  • 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. Commented Jul 13, 2020 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, 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. Commented Jul 13, 2020 at 20:54

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.