xi-test-selenium
master
PHP bindings for Selenium 2
|
A Selenium WebDriver session. More...
Public Member Functions | |
__construct (SeleniumServer $server, array $options=array()) | |
Constructs a WebDriver to use a given server. | |
__destruct () | |
Attempts to close the session, if possible. | |
getServer () | |
Returns the SeleniumServer object. | |
setBaseUrl ($baseUrl) | |
Sets the URL prefix to use when calling visit() with a relative path. | |
getBaseUrl () | |
Returns the URL prefix to use when calling visit() with a relative path. | |
getImplicitWait () | |
Returns the number of seconds an element is waited for when trying to find it. | |
setImplicitWait ($value) | |
Sets the number of seconds an element is waited for when trying to find it. | |
visit ($url) | |
Navigates to the given URL. | |
getUrl () | |
Returns the (full) contents of the browser's URL bar. | |
getPageTitle () | |
Returns the page title. | |
getPageSourceCode () | |
Returns the source code of the current page. | |
screenshot ($pngFile) | |
Takes a screenshot of the page. | |
back () | |
Moves back one step in the browser history. | |
forward () | |
Moves forward one step in the browser history. | |
refresh () | |
Refreshes the current page. | |
getAlertText () | |
Returns the alert/confirm/prompt text. | |
acceptAlert () | |
Accepts an alert/confirm/prompt by pressing OK/Yes. | |
dismissAlert () | |
Dismisses an alert/confirm/prompt by pressing Cancel/No/[X]. | |
answerPrompt ($answer) | |
Writes to a JS prompt() and clicks 'Ok'. | |
clearCookies () | |
Deletes all cookies visible to the current page. | |
runJavascript ($script, array $args=array()) | |
Runs some JavaScript on the current page. | |
closeSession () | |
You should call this when you're finished with the WebDriver or else a browser window is left open until Selenium eventually times it out. | |
isClosed () | |
Tells whether closeSession has been called. | |
Public Attributes | |
const | DEFAULT_IMPLICIT_WAIT = 5 |
Protected Member Functions | |
webElementsToRequest (array $a) | |
webElementsFromResponse ($a) | |
filterUrl ($url) | |
isAbsoluteUrl ($url) | |
openSession () | |
getSession () | |
makeRelativePostRequest ($relPath, $params) | |
createWebElement ($elementId) | |
sessionGet ($path) | |
sessionPost ($path, $params=null) | |
sessionDelete ($path) | |
getDefaultOptions () | |
Protected Attributes | |
$server | |
$options | |
$sessionPath | |
$baseUrl | |
$implicitWait |
A Selenium WebDriver session.
Tests should call an instance of this to interact with Selenium. See WebDriverInjectingTestDecorator for a way to get a hold of one in your PHPUnit tests. Remeber to call closeSession() when done or a browser window will be left open.
Subclassing this and adding your own convenience methods is fully supported. See also: WebElement.
Definition at line 15 of file WebDriver.php.
Xi\Test\Selenium\WebDriver::__construct | ( | SeleniumServer $ | server, |
array $ | options = array() |
||
) |
Constructs a WebDriver to use a given server.
The options may include any capabilities that the session opening request of Selenium Server accepts. Most important of these are probably 'browserName' and 'javascriptEnabled'.
SeleniumServer | $server | |
array | $options |
Definition at line 40 of file WebDriver.php.
{ $this->server = $server; $this->options = array_merge($this->getDefaultOptions(), $options); $this->openSession(); $this->setImplicitWait(static::DEFAULT_IMPLICIT_WAIT); }
Xi\Test\Selenium\WebDriver::__destruct | ( | ) |
Attempts to close the session, if possible.
Note that this is not called on fatal error, and I'm afraid the garbage collection order when PHP exits might cause this to fail randomly.
Definition at line 56 of file WebDriver.php.
{ if ($this->server) { echo "Warning: The Selenium session was not explicitly closed!\n"; echo " Call closeSession() on your WebDriver instance.\n"; try { $this->closeSession(); } catch (Exception $e) { echo "(failed to close Selenium session)\n"; } } }
Xi\Test\Selenium\WebDriver::getServer | ( | ) | [final] |
Returns the SeleniumServer object.
Definition at line 74 of file WebDriver.php.
{
return $this->server;
}
Xi\Test\Selenium\WebDriver::setBaseUrl | ( | $ | baseUrl | ) |
Sets the URL prefix to use when calling visit() with a relative path.
string | null | $baseUrl |
Definition at line 84 of file WebDriver.php.
Xi\Test\Selenium\WebDriver::getBaseUrl | ( | ) | [final] |
Returns the URL prefix to use when calling visit() with a relative path.
Definition at line 96 of file WebDriver.php.
{
return $this->baseUrl;
}
Xi\Test\Selenium\WebDriver::getImplicitWait | ( | ) | [final] |
Returns the number of seconds an element is waited for when trying to find it.
Definition at line 106 of file WebDriver.php.
{
return $this->implicitWait;
}
Xi\Test\Selenium\WebDriver::setImplicitWait | ( | $ | value | ) |
Sets the number of seconds an element is waited for when trying to find it.
int | float | $value | The amount of time to wait in seconds. Decimals are allowed. |
Definition at line 116 of file WebDriver.php.
{ if (!is_int($value)) { $value = floatval($value); } $this->implicitWait = $value; $this->sessionPost('/timeouts/implicit_wait', array('ms' => (int)($value * 1000))); }
Xi\Test\Selenium\WebDriver::visit | ( | $ | url | ) |
Navigates to the given URL.
If the URL is relative, then the baseUrl (if any) is prepended.
Definition at line 130 of file WebDriver.php.
{ $this->sessionPost('/url', array('url' => $this->filterUrl($url))); }
Xi\Test\Selenium\WebDriver::getUrl | ( | ) |
Returns the (full) contents of the browser's URL bar.
Definition at line 139 of file WebDriver.php.
{ return $this->sessionGet('/url'); }
Xi\Test\Selenium\WebDriver::getPageTitle | ( | ) |
Returns the page title.
Definition at line 148 of file WebDriver.php.
{ return $this->sessionGet('/title'); }
Xi\Test\Selenium\WebDriver::getPageSourceCode | ( | ) |
Returns the source code of the current page.
Definition at line 157 of file WebDriver.php.
{ return $this->sessionGet('/source'); }
Xi\Test\Selenium\WebDriver::screenshot | ( | $ | pngFile | ) |
Takes a screenshot of the page.
string | $pngFile | A path to a PNG file to write the screenshot to. |
Definition at line 166 of file WebDriver.php.
{ if (strtolower(pathinfo($pngFile, PATHINFO_EXTENSION)) !== 'png') { throw new SeleniumException("Can only take PNG screenshots. This doesn't seem like a path to a PNG: $pngFile"); } $base64Data = $this->sessionGet('/screenshot'); $f = fopen($pngFile, 'wb'); try { stream_filter_append($f, 'convert.base64-decode'); fwrite($f, $base64Data); } catch (\Exception $e) { fclose($f); throw $e; } fclose($f); }
Xi\Test\Selenium\WebDriver::back | ( | ) |
Moves back one step in the browser history.
Definition at line 186 of file WebDriver.php.
{ $this->sessionPost('/back'); }
Xi\Test\Selenium\WebDriver::forward | ( | ) |
Moves forward one step in the browser history.
Definition at line 194 of file WebDriver.php.
{ $this->sessionPost('/forward'); }
Xi\Test\Selenium\WebDriver::refresh | ( | ) |
Refreshes the current page.
Definition at line 202 of file WebDriver.php.
{ $this->sessionPost('/refresh'); }
Xi\Test\Selenium\WebDriver::getAlertText | ( | ) |
Returns the alert/confirm/prompt text.
SeleniumException | if there is no alert/confirm/prompt window. |
Definition at line 211 of file WebDriver.php.
{ return $this->sessionGet('/alert_text'); }
Xi\Test\Selenium\WebDriver::acceptAlert | ( | ) |
Accepts an alert/confirm/prompt by pressing OK/Yes.
SeleniumException | if there is no alert/confirm/prompt window. |
Definition at line 220 of file WebDriver.php.
{ $this->sessionPost('/accept_alert'); }
Xi\Test\Selenium\WebDriver::dismissAlert | ( | ) |
Dismisses an alert/confirm/prompt by pressing Cancel/No/[X].
SeleniumException | if there is no alert/confirm/prompt window. |
Definition at line 229 of file WebDriver.php.
{ $this->sessionPost('/dismiss_alert'); }
Xi\Test\Selenium\WebDriver::answerPrompt | ( | $ | answer | ) |
Writes to a JS prompt() and clicks 'Ok'.
SeleniumException | if there is no prompt() to answer. |
Definition at line 238 of file WebDriver.php.
{ $this->sessionPost('/alert_text', array('text' => $answer)); $this->sessionPost('/accept_alert'); }
Xi\Test\Selenium\WebDriver::clearCookies | ( | ) |
Deletes all cookies visible to the current page.
Definition at line 247 of file WebDriver.php.
{ $this->sessionDelete('/cookie'); }
Xi\Test\Selenium\WebDriver::runJavascript | ( | $ | script, |
array $ | args = array() |
||
) |
Runs some JavaScript on the current page.
string | $script | JavaScript code. |
array | $args | Arguments to be made available in the script via the 'arguments' array. May contain WebElement objects. |
Definition at line 259 of file WebDriver.php.
{ $args = $this->webElementsToRequest($args); $response = $this->sessionPost('/execute', array('script' => $script, 'args' => $args)); return $this->webElementsFromResponse($response); }
Xi\Test\Selenium\WebDriver::webElementsToRequest | ( | array $ | a | ) | [protected] |
Definition at line 266 of file WebDriver.php.
{ foreach ($a as $key => $value) { if (is_array($value)) { $a[$key] = $this->webElementsToRequest($value); } elseif ($value instanceof WebElement) { $a[$key] = array('ELEMENT' => $value->getSeleniumId()); } } return $a; }
Xi\Test\Selenium\WebDriver::webElementsFromResponse | ( | $ | a | ) | [protected] |
Definition at line 278 of file WebDriver.php.
{ if (is_array($a)) { if (isset($a['ELEMENT'])) { return $this->createWebElement($a['ELEMENT']); } else { foreach ($a as $key => $value) { $a[$key] = $this->webElementsFromResponse($value); } } } return $a; }
Xi\Test\Selenium\WebDriver::filterUrl | ( | $ | url | ) | [protected] |
Definition at line 293 of file WebDriver.php.
{ if ($this->baseUrl && !$this->isAbsoluteUrl($url)) { if ($url[0] == '/') { return $this->baseUrl . $url; } else { return $this->baseUrl . '/' . $url; } } else { return $url; } }
Xi\Test\Selenium\WebDriver::isAbsoluteUrl | ( | $ | url | ) | [protected] |
Definition at line 306 of file WebDriver.php.
{ $colon = strpos($url, ':'); $slash = strpos($url, '/'); if ($colon !== false && $slash !== false) { return $slash === 0 || $colon < $slash; } else { return $colon !== false; } }
Xi\Test\Selenium\WebDriver::openSession | ( | ) | [protected] |
Definition at line 317 of file WebDriver.php.
{ $resp = $this->server->post('/session', array( 'desiredCapabilities' => $this->options )); $this->sessionPath = rtrim($this->server->getLastRedirectLocation(), '/'); }
Xi\Test\Selenium\WebDriver::closeSession | ( | ) |
You should call this when you're finished with the WebDriver or else a browser window is left open until Selenium eventually times it out.
Definition at line 329 of file WebDriver.php.
{ $this->server->delete($this->sessionPath); $this->server = null; }
Xi\Test\Selenium\WebDriver::isClosed | ( | ) |
Tells whether closeSession has been called.
Calling any other function after the session has been closed is likely to causes an error.
Definition at line 340 of file WebDriver.php.
{
return ($this->server === null);
}
Xi\Test\Selenium\WebDriver::getSession | ( | ) | [protected] |
Reimplemented from Xi\Test\Selenium\HasWebElements.
Definition at line 345 of file WebDriver.php.
{
return $this;
}
Xi\Test\Selenium\WebDriver::makeRelativePostRequest | ( | $ | relPath, |
$ | params | ||
) | [protected] |
Reimplemented from Xi\Test\Selenium\HasWebElements.
Definition at line 350 of file WebDriver.php.
{ return $this->sessionPost($relPath, $params); }
Xi\Test\Selenium\WebDriver::createWebElement | ( | $ | elementId | ) | [protected] |
Reimplemented from Xi\Test\Selenium\HasWebElements.
Definition at line 355 of file WebDriver.php.
{ return new WebElement($this->getSession(), $this->sessionPath, $elementId); }
Xi\Test\Selenium\WebDriver::sessionGet | ( | $ | path | ) | [protected] |
Definition at line 360 of file WebDriver.php.
{
return $this->server->get($this->sessionPath . $path);
}
Xi\Test\Selenium\WebDriver::sessionPost | ( | $ | path, |
$ | params = null |
||
) | [protected] |
Definition at line 365 of file WebDriver.php.
{
return $this->server->post($this->sessionPath . $path, $params);
}
Xi\Test\Selenium\WebDriver::sessionDelete | ( | $ | path | ) | [protected] |
Definition at line 370 of file WebDriver.php.
{
return $this->server->delete($this->sessionPath . $path);
}
Xi\Test\Selenium\WebDriver::getDefaultOptions | ( | ) | [protected] |
Definition at line 375 of file WebDriver.php.
{ return array( 'browserName' => 'firefox', 'version' => '', 'javascriptEnabled' => true, 'nativeEvents' => false ); }
const Xi::Test::Selenium\WebDriver::DEFAULT_IMPLICIT_WAIT = 5 |
Definition at line 17 of file WebDriver.php.
Xi::Test::Selenium\WebDriver::$server [protected] |
Definition at line 22 of file WebDriver.php.
Xi::Test::Selenium\WebDriver::$options [protected] |
Definition at line 23 of file WebDriver.php.
Xi::Test::Selenium\WebDriver::$sessionPath [protected] |
Definition at line 24 of file WebDriver.php.
Xi::Test::Selenium\WebDriver::$baseUrl [protected] |
Definition at line 26 of file WebDriver.php.
Xi::Test::Selenium\WebDriver::$implicitWait [protected] |
Definition at line 28 of file WebDriver.php.