Skip to content

Instantly share code, notes, and snippets.

@gggeek
Last active December 7, 2022 10:38
Show Gist options
  • Save gggeek/cf534864273e7fa8dfec to your computer and use it in GitHub Desktop.
Save gggeek/cf534864273e7fa8dfec to your computer and use it in GitHub Desktop.
class WebDriver extends AbstractWebDriver
{
protected $proxy = '';
public function setProxy($proxy)
{
$this->proxy = $proxy;
}
/**
* Given a set of options, adds to it the default 'extra' options
* @param array $extraOptions
* @return array
*/
protected function getExtraOptions(array $extraOptions = array())
{
$defaults = array();
if ($this->proxy !== '') {
$defaults[CURLOPT_PROXY] = $this->proxy;
}
$result = $extraOptions + $defaults;
return $result;
}
/**
* Magic method that maps calls to class methods to execute WebDriver commands
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @return mixed
*
* @throws \WebDriver\Exception if invalid WebDriver command
*/
public function __call($name, $arguments)
{
if (count($arguments) > 1) {
throw WebDriverException::factory(
WebDriverException::JSON_PARAMETERS_EXPECTED,
'Commands should have at most only one parameter, which should be the JSON Parameter object'
);
}
if (preg_match('/^(get|post|delete)/', $name, $matches)) {
$requestMethod = strtoupper($matches[0]);
$webdriverCommand = strtolower(substr($name, strlen($requestMethod)));
} else {
$webdriverCommand = $name;
$requestMethod = $this->getRequestMethod($webdriverCommand);
}
$methods = $this->methods();
if (!in_array($requestMethod, (array) $methods[$webdriverCommand])) {
throw WebDriverException::factory(
WebDriverException::INVALID_REQUEST,
sprintf(
'%s is not an available http request method for the command %s.',
$requestMethod,
$webdriverCommand
)
);
}
$result = $this->curl(
$requestMethod,
'/' . $webdriverCommand,
array_shift($arguments),
$this->getExtraOptions()
);
return $result['value'];
}
/// All the methods below are copy-pasted from Webdriver/Webdriver, which is unluckily declared FINAL :-(
/**
* {@inheritdoc}
*/
protected function methods()
{
return array(
'status' => 'GET',
);
}
/**
* New Session: /session (POST)
* Get session object for chaining
*
* @param array|string $requiredCapabilities Required capabilities (or browser name)
* @param array $desiredCapabilities Desired capabilities
*
* @return \WebDriver\Session
*/
public function session($requiredCapabilities = Browser::FIREFOX, $desiredCapabilities = array())
{
// for backwards compatibility when the only required capability was browser name
if (! is_array($requiredCapabilities)) {
$desiredCapabilities[Capability::BROWSER_NAME] = $requiredCapabilities ?: Browser::FIREFOX;
$requiredCapabilities = array();
}
// required
$parameters = array(
'desiredCapabilities' => array_merge($desiredCapabilities, $requiredCapabilities)
);
// optional
if ( ! empty($requiredCapabilities)) {
$parameters['requiredCapabilities'] = $requiredCapabilities;
}
$result = $this->curl(
'POST',
'/session',
$parameters,
$this->getExtraOptions(array(CURLOPT_FOLLOWLOCATION => true))
);
return new Session($result['sessionUrl']);
}
/**
* Get list of currently active sessions
*
* @return array an array of \WebDriver\Session objects
*/
public function sessions()
{
$result = $this->curl('GET', '/sessions', null, $this->getExtraOptions());
$sessions = array();
foreach ($result['value'] as $session) {
$sessions[] = new Session($this->url . '/session/' . $session['id']);
}
return $sessions;
}
}
@spolischook
Copy link

add .php extension for syntax highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment