public function __construct($json_response) { parent::__construct("No such element exception", WebDriverResponseStatus::NoSuchElement, null); $this->json_response = $json_response; }
/** * @param string $expected_tag_name * @param string $actual_tag_name */ public function __construct($expected_tag_name, $actual_tag_name) { parent::__construct(sprintf("Element should have been \"%s\" but was \"%s\"", $expected_tag_name, $actual_tag_name)); }
/** * Curl request to webdriver server. * * @param http_method 'GET', 'POST', or 'DELETE' * @param suffix What to append to the base URL. * @param command The Command object, modelled as a hash. * @param extra_opts key => value pairs of curl options for curl_setopt() * @return array */ protected static function curl($http_method, $url, $command, $extra_opts = array()) { $params = $command['parameters']; foreach ($params as $name => $value) { if ($name[0] === ':') { $url = str_replace($name, $value, $url); if ($http_method != 'POST') { unset($params[$name]); } } } if (isset($command['sessionId'])) { $url = str_replace(':sessionId', $command['sessionId'], $url); } if ($params && is_array($params) && $http_method !== 'POST') { throw new Exception(sprintf('The http method called for %s is %s but it has to be POST' . ' if you want to pass the JSON params %s', $url, $http_method, json_encode($params))); } $curl = curl_init($url); curl_setopt($curl, CURLOPT_TIMEOUT, 300); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8', 'Accept: application/json')); if ($http_method === 'POST') { curl_setopt($curl, CURLOPT_POST, true); if ($params && is_array($params)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); } } else { if ($http_method == 'DELETE') { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); } } foreach ($extra_opts as $option => $value) { curl_setopt($curl, $option, $value); } $raw_results = trim(curl_exec($curl)); $info = curl_getinfo($curl); if ($error = curl_error($curl)) { $msg = sprintf('Curl error thrown for http %s to %s', $http_method, $url); if ($params && is_array($params)) { $msg .= sprintf(' with params: %s', json_encode($params)); } WebDriverException::throwException(-1, $msg . "\n\n" . $error, array()); } curl_close($curl); $results = json_decode($raw_results, true); $value = null; if (is_array($results) && array_key_exists('value', $results)) { $value = $results['value']; } $message = null; if (is_array($value) && array_key_exists('message', $value)) { $message = $value['message']; } $sessionId = null; if (is_array($results) && array_key_exists('sessionId', $results)) { $sessionId = $results['sessionId']; } $status = isset($results['status']) ? $results['status'] : 0; WebDriverException::throwException($status, $message, $results); return array('value' => $value, 'info' => $info, 'sessionId' => $sessionId); }
/** * @param WebDriverCommand $command * @param array $curl_opts An array of curl options. * * @return mixed */ public function execute(WebDriverCommand $command) { if (!isset(self::$commands[$command->getName()])) { throw new InvalidArgumentException($command->getName() . " is not a valid command."); } $raw = self::$commands[$command->getName()]; $http_method = $raw['method']; $url = $raw['url']; $url = str_replace(':sessionId', $command->getSessionID(), $url); $params = $command->getParameters(); foreach ($params as $name => $value) { if ($name[0] === ':') { $url = str_replace($name, $value, $url); if ($http_method != 'POST') { unset($params[$name]); } } } if ($params && is_array($params) && $http_method !== 'POST') { throw new BadMethodCallException(sprintf('The http method called for %s is %s but it has to be POST' . ' if you want to pass the JSON params %s', $url, $http_method, json_encode($params))); } curl_setopt($this->curl, CURLOPT_URL, $this->url . $url); // https://github.com/facebook/php-webdriver/issues/173 switch ($command->getName()) { case DriverCommand::NEW_SESSION: curl_setopt($this->curl, CURLOPT_POST, 1); break; default: curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $http_method); } $encoded_params = null; if ($http_method === 'POST' && $params && is_array($params)) { $encoded_params = json_encode($params); } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $encoded_params); $raw_results = trim(curl_exec($this->curl)); if ($error = curl_error($this->curl)) { $msg = sprintf('Curl error thrown for http %s to %s', $http_method, $url); if ($params && is_array($params)) { $msg .= sprintf(' with params: %s', json_encode($params)); } WebDriverException::throwException(-1, $msg . "\n\n" . $error, array()); } $results = json_decode($raw_results, true); if ($results === null && json_last_error() !== JSON_ERROR_NONE) { throw new WebDriverException(sprintf("JSON decoding of remote response failed.\n" . "Error code: %d\n" . "The response: '%s'\n", json_last_error(), $raw_results)); } $value = null; if (is_array($results) && array_key_exists('value', $results)) { $value = $results['value']; } $message = null; if (is_array($value) && array_key_exists('message', $value)) { $message = $value['message']; } $sessionId = null; if (is_array($results) && array_key_exists('sessionId', $results)) { $sessionId = $results['sessionId']; } $status = isset($results['status']) ? $results['status'] : 0; WebDriverException::throwException($status, $message, $results); $response = new WebDriverResponse($sessionId); return $response->setStatus($status)->setValue($value); }