/** * test factory() */ public function testFactory() { $out = Exception::factory(255, 'wtf'); $this->assertTrue(get_class($out) === 'Exception'); $this->assertTrue($out->getMessage() === 'wtf'); $out = Exception::factory(Exception::SUCCESS); $this->assertTrue(get_class($out) === 'Exception'); $this->assertTrue($out->getMessage() === 'Unknown Error'); $out = Exception::factory(Exception::CURL_EXEC); $this->assertTrue($out instanceof Exception\CurlExec); $this->assertTrue($out->getMessage() === 'curl_exec() error.'); }
/** * helper method to wait until user-defined condition is met * * @param function $callback callback which returns non-false result if wait condition was met * @param integer $maxIterations maximum number of iterations * @param integer $sleep sleep duration in seconds between iterations * @param array $args optional args; if the callback needs $this, then pass it here * * @return mixed result from callback function * * @throws \Exception if thrown by callback, or \WebDriver\Exception\Timeout if helper times out */ public function wait($callback, $maxIterations = 1, $sleep = 0, $args = array()) { $i = max(1, $maxIterations); while ($i-- > 0) { $result = call_user_func_array($callback, $args); if ($result !== false) { return $result; } // don't sleep on the last iteration $i && sleep($sleep); } throw WebDriverException::factory(WebDriverException::TIMEOUT, 'wait() method timed out'); }
/** * {@inheritdoc} */ public function execute($requestMethod, $url, $parameters = null, $extraOptions = array()) { $customHeaders = array('Content-Type: application/json;charset=UTF-8', 'Accept: application/json;charset=UTF-8'); $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); switch ($requestMethod) { case 'GET': break; case 'POST': if ($parameters && is_array($parameters)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); } else { $customHeaders[] = 'Content-Length: 0'; } // Suppress "Expect: 100-continue" header automatically added by cURL that // causes a 1 second delay if the remote server does not support Expect. $customHeaders[] = 'Expect:'; curl_setopt($curl, CURLOPT_POST, true); break; case 'DELETE': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'PUT': if ($parameters && is_array($parameters)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); } else { $customHeaders[] = 'Content-Length: 0'; } // Suppress "Expect: 100-continue" header automatically added by cURL that // causes a 1 second delay if the remote server does not support Expect. $customHeaders[] = 'Expect:'; curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); break; } foreach ($extraOptions as $option => $value) { curl_setopt($curl, $option, $value); } curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders); $rawResult = trim(curl_exec($curl)); $info = curl_getinfo($curl); if (CURLE_GOT_NOTHING !== curl_errno($curl) && ($error = curl_error($curl))) { $message = sprintf('Curl error thrown for http %s to %s%s', $requestMethod, $url, $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : ''); throw WebDriverException::factory(WebDriverException::CURL_EXEC, $message . "\n\n" . $error); } curl_close($curl); return array($rawResult, $info); }
/** * Get default HTTP request method for a given WebDriver command * * @param string $webdriverCommand * * @return string * * @throws \WebDriver\Exception if invalid WebDriver command */ private function getRequestMethod($webdriverCommand) { if (!array_key_exists($webdriverCommand, $this->methods())) { throw WebDriverException::factory(array_key_exists($webdriverCommand, $this->obsoleteMethods()) ? WebDriverException::OBSOLETE_COMMAND : WebDriverException::UNKNOWN_COMMAND, sprintf('%s is not a valid WebDriver command.', $webdriverCommand)); } $methods = $this->methods(); $requestMethods = (array) $methods[$webdriverCommand]; return array_shift($requestMethods); }
/** * Return JSON parameter for element / elements command * * @param string $using locator strategy * @param string $value search target * * @return array * * @throws \WebDriver\Exception if invalid locator strategy */ public function locate($using, $value) { if (!in_array($using, $this->strategies)) { throw WebDriverException::factory(WebDriverException::UNKNOWN_LOCATOR_STRATEGY, sprintf('Invalid locator strategy %s', $using)); } return array('using' => $using, 'value' => $value); }
/** * Delete storage or a specific key * * @return \WebDriver\Storage * * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters */ public function delete() { // delete storage if (func_num_args() === 0) { $this->curl('DELETE', ''); return $this; } // delete key from storage if (func_num_args() === 1) { return $this->deleteKey(func_get_arg(0)); } throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); }
/** * Assertion handler to instead throw exceptions * * @param string $file Source file * @param integer $line Line number * @param string $code Error code * * @throws \WebDriver\Exception\WebTestAssertion */ public static function assert_handler($file, $line, $code) { throw WebDriverException::factory(WebDriverException::WEBTEST_ASSERTION, "assertion failed: {$file}:{$line}: {$code}"); }
/** * Assert (expect) exception * * @param function $callback Callback function * @param string $message Message * * @throw \WebDriver\Exception\WebTestAssertion if not exception is thrown */ protected function assertException($callback, $message) { $this->assertStats['total']++; try { $callback(); $this->assertStats['failure']++; throw WebDriverException::factory(WebDriverException::WEBTEST_ASSERTION, $message); } catch (\Exception $e) { // expected exception } $this->assertStats['pass']++; }