|
xi-test-selenium
master
PHP bindings for Selenium 2
|
Manages a connection to a Selenium server. More...
Public Member Functions | |
| __construct ($serverUrl) | |
| enableDebug ($enable=true) | |
| get ($path) | |
| post ($path, $params=null) | |
| delete ($path) | |
| splitHeadersAndBody ($response) | |
| getLastRedirectLocation () | |
Protected Member Functions | |
| doRequest ($path, $curlOpts) | |
Protected Attributes | |
| $serverUrl | |
| $lastRedirectLocation | |
| $debuggingEnabled | |
Manages a connection to a Selenium server.
Most often the server will talk with `http://localhost:4444/wd/hub`, but a remote Selenium server can be used just as well.
A SeleniumServer does not hold a presistent connection. Instead each call becomes a new CURL request. For this reason, the object doesn't need to be explicitly "closed".
Thanks to http://code.google.com/p/php-webdriver-bindings/ for inspiration and for a point of reference for interfacing problems and curl usage.
Definition at line 18 of file SeleniumServer.php.
| Xi\Test\Selenium\SeleniumServer::__construct | ( | $ | serverUrl | ) |
Definition at line 24 of file SeleniumServer.php.
{
$this->serverUrl = rtrim($serverUrl, '/');
$this->lastRedirectLocation = null;
$this->debuggingEnabled = false;
}
| Xi\Test\Selenium\SeleniumServer::enableDebug | ( | $ | enable = true | ) |
Definition at line 31 of file SeleniumServer.php.
{
$this->debuggingEnabled = $enable;
}
| Xi\Test\Selenium\SeleniumServer::get | ( | $ | path | ) |
Definition at line 36 of file SeleniumServer.php.
{
return $this->doRequest($path, array(
CURLOPT_HTTPGET => true
));
}
| Xi\Test\Selenium\SeleniumServer::post | ( | $ | path, |
| $ | params = null |
||
| ) |
Definition at line 43 of file SeleniumServer.php.
{
if ($params === null) {
$params = array();
}
return $this->doRequest($path, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($params)
));
}
| Xi\Test\Selenium\SeleniumServer::delete | ( | $ | path | ) |
Definition at line 54 of file SeleniumServer.php.
{
return $this->doRequest($path, array(
CURLOPT_CUSTOMREQUEST => 'DELETE'
));
}
| Xi\Test\Selenium\SeleniumServer::doRequest | ( | $ | path, |
| $ | curlOpts | ||
| ) | [protected] |
Definition at line 61 of file SeleniumServer.php.
{
$curl = curl_init();
try {
$defaultOpts = array(
CURLOPT_HTTPHEADER => array("application/json; charset=UTF-8"),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => $this->debuggingEnabled
);
curl_setopt_array($curl, $defaultOpts);
curl_setopt_array($curl, $curlOpts); // (can't array_merge because integer keys)
if (strpos($path, $this->serverUrl) !== false) {
$url = $path;
} else {
$url = $this->serverUrl . '/' . ltrim($path, '/');
}
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
if ($response === false) {
throw new \Exception("Failed to connect to Selenium server: " . curl_error($curl));
}
list($headers, $response) = $this->splitHeadersAndBody($response);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$statusCodeCategory = (int)substr($statusCode, 0, 1);
if ($statusCodeCategory == 3) {
$this->lastRedirectLocation = $headers['Location'];
} elseif ($statusCodeCategory == 4) {
throw new \Exception("Selenium returned HTTP $statusCode: $response");
}
if (strpos(curl_getinfo($curl, CURLINFO_CONTENT_TYPE), 'json') !== false) {
$response = json_decode(trim($response), true);
if ($response['status'] != 0) {
$message = isset($response['message']) ? $response['message'] : null;
throw new SeleniumException($message, $response['status']);
}
$response = isset($response['value']) ? $response['value'] : null;
}
curl_close($curl);
} catch (\Exception $e) {
curl_close($curl);
throw $e;
}
return $response;
}
| Xi\Test\Selenium\SeleniumServer::splitHeadersAndBody | ( | $ | response | ) |
Definition at line 118 of file SeleniumServer.php.
{
if (strpos($response, "\r\n\r\n") !== false) {
$separator = "\r\n\r\n";
} else {
$separator = "\n\n";
}
$parts = explode($separator, $response, 2);
if (count($parts) == 1) {
$headerLines = $parts[0];
$body = null;
} else {
list($headerLines, $body) = $parts;
}
$headerLines = explode("\n", $headerLines);
array_shift($headerLines); // The HTTP response code line
$headerLines = array_map('trim', $headerLines);
$headerLines = array_filter($headerLines, function($line) { return !empty($line); });
$headers = array();
foreach ($headerLines as $headerLine) {
list($key, $value) = explode(':', $headerLine, 2);
$headers[$key] = trim($value);
}
return array($headers, $body);
}
| Xi\Test\Selenium\SeleniumServer::getLastRedirectLocation | ( | ) |
Definition at line 148 of file SeleniumServer.php.
{
return $this->lastRedirectLocation;
}
Xi::Test::Selenium\SeleniumServer::$serverUrl [protected] |
Definition at line 20 of file SeleniumServer.php.
Xi::Test::Selenium\SeleniumServer::$lastRedirectLocation [protected] |
Definition at line 21 of file SeleniumServer.php.
Xi::Test::Selenium\SeleniumServer::$debuggingEnabled [protected] |
Definition at line 22 of file SeleniumServer.php.