'How to add Options through Facebook/php-webdriver?

I'm seeking to add options to an instance of Facebook/php-webdriver.

This works to get the initial options:

$options = \Facebook\WebDriver\Remote\DesiredCapabilities::chrome();

Now I'd like to add additional options:

$options->setCapability("enablePassThrough", FALSE);
$options->setCapability("no-sandbox", TRUE);

I'm getting the error:

Call to undefined function setCapability()

I've tried a few approaches, but haven't yet found out how to do it.

What is the correct way to add options to an instance of Facebook/php-webdriver?



Solution 1:[1]

setCapability()

setCapability() method configures the WebDriver instance with the capabilities through an instance of DesiredCapabilities() as follows:

public function testShouldProvideAccessToCapabilitiesUsingSettersAndGetters()
{
    $capabilities = new DesiredCapabilities();
    // generic capability setter
    $capabilities->setCapability('custom', 1337);
    // specific setters
    $capabilities->setBrowserName(WebDriverBrowserType::CHROME);
    $capabilities->setPlatform(WebDriverPlatform::LINUX);
    $capabilities->setVersion(333);
    $this->assertSame(1337, $capabilities->getCapability('custom'));
    $this->assertSame(WebDriverBrowserType::CHROME, $capabilities->getBrowserName());
    $this->assertSame(WebDriverPlatform::LINUX, $capabilities->getPlatform());
    $this->assertSame(333, $capabilities->getVersion());
}

--no-sandbox

-no-sandbox argument can be added through instance of ChromeOptions() and further can be added to the instance of DesiredCapabilities() as follows:

$options = new ChromeOptions();
$options->addArguments(array('--no-sandbox'));
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

enablePassThrough

enablePassThrough mode was introduced for the first time in Selenium Client v3.5.0 . enablePassThrough allowed a connection from your test's RemoteWebDriver, through the Grid Hub, to a Grid Node, and down to a DriverService and then to the browser to use the same WebDriver protocol (the Json Wire Protocol or the W3C one) end to end without translation.

enablePassThrough mode could have been disabled by starting the standalone server or Grid node with the argument -enablePassThrough false

With the release and availability of Selenium Client v3.9.0 all HTTP communication was switched to OkHttp. Though you can still change the version back to the Apache HttpClient by setting the webdriver.http.factory system property to apache.

Simultanously support for the passthrough mode for the server was dropped.

Here you can find a detailed discussion on enablePassThrough not available for selenium server 3.9.1

Solution 2:[2]

This syntax works on my system:

    $options = new \Facebook\WebDriver\Chrome\ChromeOptions();
    $options->addArguments(array('--no-sandbox'));
    $capabilities = \Facebook\WebDriver\Remote\DesiredCapabilities::chrome();
    $capabilities->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options);
    $seleniumDriver = \Facebook\WebDriver\Remote\RemoteWebDriver::create(
        $host,
        $capabilities,
        5000
    );

Solution 3:[3]

$options = new ChromeOptions();
$options->addArguments(array('--no-sandbox'));
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium
Solution 2 VikR
Solution 3 Rodrigo Carmona