Chris@14: Chris@14: * @author Anthon Pang Chris@14: * @author Fabrizio Branca Chris@14: * @author Tsz Ming Wong Chris@14: */ Chris@14: Chris@14: namespace WebDriver; Chris@14: Chris@14: use WebDriver\Exception as WebDriverException; Chris@14: Chris@14: /** Chris@14: * Abstract WebDriver\AbstractWebDriver class Chris@14: * Chris@14: * @package WebDriver Chris@14: */ Chris@14: abstract class AbstractWebDriver Chris@14: { Chris@14: /** Chris@14: * URL Chris@14: * Chris@14: * @var string Chris@14: */ Chris@14: protected $url; Chris@14: Chris@14: /** Chris@14: * Return array of supported method names and corresponding HTTP request methods Chris@14: * Chris@14: * @return array Chris@14: */ Chris@14: abstract protected function methods(); Chris@14: Chris@14: /** Chris@14: * Return array of obsolete method names and corresponding HTTP request methods Chris@14: * Chris@14: * @return array Chris@14: */ Chris@14: protected function obsoleteMethods() Chris@14: { Chris@14: return array(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Constructor Chris@14: * Chris@14: * @param string $url URL to Selenium server Chris@14: */ Chris@14: public function __construct($url = 'http://localhost:4444/wd/hub') Chris@14: { Chris@14: $this->url = $url; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Magic method which returns URL to Selenium server Chris@14: * Chris@14: * @return string Chris@14: */ Chris@14: public function __toString() Chris@14: { Chris@14: return $this->url; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns URL to Selenium server Chris@14: * Chris@14: * @return string Chris@14: */ Chris@14: public function getURL() Chris@14: { Chris@14: return $this->url; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Curl request to webdriver server. Chris@14: * Chris@14: * @param string $requestMethod HTTP request method, e.g., 'GET', 'POST', or 'DELETE' Chris@14: * @param string $command If not defined in methods() this function will throw. Chris@14: * @param array $parameters If an array(), they will be posted as JSON parameters Chris@14: * If a number or string, "/$params" is appended to url Chris@14: * @param array $extraOptions key=>value pairs of curl options to pass to curl_setopt() Chris@14: * Chris@14: * @return array array('value' => ..., 'info' => ...) Chris@14: * Chris@14: * @throws \WebDriver\Exception if error Chris@14: */ Chris@14: protected function curl($requestMethod, $command, $parameters = null, $extraOptions = array()) Chris@14: { Chris@14: if ($parameters && is_array($parameters) && $requestMethod !== 'POST') { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::NO_PARAMETERS_EXPECTED, Chris@14: sprintf( Chris@14: 'The http request method called for %s is %s but it has to be POST if you want to pass the JSON parameters %s', Chris@14: $command, Chris@14: $requestMethod, Chris@14: json_encode($parameters) Chris@14: ) Chris@14: ); Chris@14: } Chris@14: Chris@14: $url = sprintf('%s%s', $this->url, $command); Chris@14: Chris@14: if ($parameters && (is_int($parameters) || is_string($parameters))) { Chris@14: $url .= '/' . $parameters; Chris@14: } Chris@14: Chris@14: list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions); Chris@14: Chris@14: $httpCode = $info['http_code']; Chris@14: Chris@14: // According to https://w3c.github.io/webdriver/webdriver-spec.html all 4xx responses are to be considered Chris@14: // an error and return plaintext, while 5xx responses are json encoded Chris@14: if ($httpCode >= 400 && $httpCode <= 499) { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::CURL_EXEC, Chris@14: 'Webdriver http error: ' . $httpCode . ', payload :' . substr($rawResult, 0, 1000) Chris@14: ); Chris@14: } Chris@14: Chris@14: $result = json_decode($rawResult, true); Chris@14: Chris@14: if (!empty($rawResult) && $result === null && json_last_error() != JSON_ERROR_NONE) { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::CURL_EXEC, Chris@14: 'Payload received from webdriver is not valid json: ' . substr($rawResult, 0, 1000) Chris@14: ); Chris@14: } Chris@14: Chris@14: if (is_array($result) && !array_key_exists('status', $result)) { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::CURL_EXEC, Chris@14: 'Payload received from webdriver is valid but unexpected json: ' . substr($rawResult, 0, 1000) Chris@14: ); Chris@14: } Chris@14: Chris@14: $value = (is_array($result) && array_key_exists('value', $result)) ? $result['value'] : null; Chris@14: $message = (is_array($value) && array_key_exists('message', $value)) ? $value['message'] : null; Chris@14: Chris@14: // if not success, throw exception Chris@14: if ((int) $result['status'] !== 0) { Chris@14: throw WebDriverException::factory($result['status'], $message); Chris@14: } Chris@14: Chris@14: $sessionId = isset($result['sessionId']) Chris@14: ? $result['sessionId'] Chris@14: : (isset($value['webdriver.remote.sessionid']) Chris@14: ? $value['webdriver.remote.sessionid'] Chris@14: : null Chris@14: ); Chris@14: Chris@14: return array( Chris@14: 'value' => $value, Chris@14: 'info' => $info, Chris@14: 'sessionId' => $sessionId, Chris@14: 'sessionUrl' => $sessionId ? $this->url . '/session/' . $sessionId : $info['url'], Chris@14: ); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Magic method that maps calls to class methods to execute WebDriver commands Chris@14: * Chris@14: * @param string $name Method name Chris@14: * @param array $arguments Arguments Chris@14: * Chris@14: * @return mixed Chris@14: * Chris@14: * @throws \WebDriver\Exception if invalid WebDriver command Chris@14: */ Chris@14: public function __call($name, $arguments) Chris@14: { Chris@14: if (count($arguments) > 1) { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::JSON_PARAMETERS_EXPECTED, Chris@14: 'Commands should have at most only one parameter, which should be the JSON Parameter object' Chris@14: ); Chris@14: } Chris@14: Chris@14: if (preg_match('/^(get|post|delete)/', $name, $matches)) { Chris@14: $requestMethod = strtoupper($matches[0]); Chris@14: $webdriverCommand = strtolower(substr($name, strlen($requestMethod))); Chris@14: } else { Chris@14: $webdriverCommand = $name; Chris@14: $requestMethod = $this->getRequestMethod($webdriverCommand); Chris@14: } Chris@14: Chris@14: $methods = $this->methods(); Chris@14: Chris@14: if (!in_array($requestMethod, (array) $methods[$webdriverCommand])) { Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::INVALID_REQUEST, Chris@14: sprintf( Chris@14: '%s is not an available http request method for the command %s.', Chris@14: $requestMethod, Chris@14: $webdriverCommand Chris@14: ) Chris@14: ); Chris@14: } Chris@14: Chris@14: $result = $this->curl( Chris@14: $requestMethod, Chris@14: '/' . $webdriverCommand, Chris@14: array_shift($arguments) Chris@14: ); Chris@14: Chris@14: return $result['value']; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Get default HTTP request method for a given WebDriver command Chris@14: * Chris@14: * @param string $webdriverCommand Chris@14: * Chris@14: * @return string Chris@14: * Chris@14: * @throws \WebDriver\Exception if invalid WebDriver command Chris@14: */ Chris@14: private function getRequestMethod($webdriverCommand) Chris@14: { Chris@14: if (!array_key_exists($webdriverCommand, $this->methods())) { Chris@14: throw WebDriverException::factory( Chris@14: array_key_exists($webdriverCommand, $this->obsoleteMethods()) Chris@14: ? WebDriverException::OBSOLETE_COMMAND : WebDriverException::UNKNOWN_COMMAND, Chris@14: sprintf('%s is not a valid WebDriver command.', $webdriverCommand) Chris@14: ); Chris@14: } Chris@14: Chris@14: $methods = $this->methods(); Chris@14: $requestMethods = (array) $methods[$webdriverCommand]; Chris@14: Chris@14: return array_shift($requestMethods); Chris@14: } Chris@14: }