如何在PHP中运行Selenium headless?

如何在PHP中运行Selenium headless?,php,selenium,selenium-webdriver,geckodriver,headless-browser,Php,Selenium,Selenium Webdriver,Geckodriver,Headless Browser,我们希望在构建脚本中与其他单元测试一起运行Selenium测试,但是由于构建是在Jenkins上运行的,而Jenkins是作为服务运行的,因此测试需要无头运行。我们的Selenium测试是用PHP编写的,到目前为止,我所看到的一切似乎都适用于JavaScript或Python 我们有没有办法运行我们的PHP Selenium测试headless(最好使用与不运行headless时相同的驱动程序,以便我们可以检测特定浏览器的问题)?在PHP webdriver文档中找到了这一点: use Face

我们希望在构建脚本中与其他单元测试一起运行Selenium测试,但是由于构建是在Jenkins上运行的,而Jenkins是作为服务运行的,因此测试需要无头运行。我们的Selenium测试是用PHP编写的,到目前为止,我所看到的一切似乎都适用于JavaScript或Python


我们有没有办法运行我们的PHP Selenium测试headless(最好使用与不运行headless时相同的驱动程序,以便我们可以检测特定浏览器的问题)?

在PHP webdriver文档中找到了这一点:

use Facebook\WebDriver\Remote\DesiredCapabilities;

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

$driver = RemoteWebDriver::create($host, $desiredCapabilities);

php webdriver 1.11.0(2021-05-03)对此进行了改进

启动无头镀铬 有关详细信息,请参阅php webdriver wiki文章

启动无头Firefox
请参阅php webdriver wiki文章。

我想您可以像执行任何其他脚本一样执行它们
php your_test.php
。如果你已经编写了这些测试,那么你肯定必须知道如何运行这些测试,也许你的问题有别的意思——我认为我没有正确理解。这不是如何运行测试的问题。问题是如何让他们无头运行。如果您进行检查,Jenkins在作为服务运行时会无头运行测试,这意味着如果您尝试以其他方式运行测试,测试将失败。我们希望能够以现有的方式运行我们的测试,而不必经历这个问题的公认答案中提到的任何障碍,以使它不在詹金斯无头运行。
$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);
$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);