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