Skip to content

Commit

Permalink
Adding timeouts configuration
Browse files Browse the repository at this point in the history
Change the timeout setting by:
$driver->manage()->timeouts()
  ->implicitlyWait(3)
  ->setScriptTimeout(5);
  • Loading branch information
whhone committed Jun 14, 2013
1 parent f823f90 commit c19952f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
13 changes: 13 additions & 0 deletions WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ public function executeScript($script, array $arguments = array()) {
}
}

/**
* An abstraction for managing stuff you would do in a browser menu. For
* example, adding and deleting cookies.
*
* @return WebDriverOptions
*/
public function manage() {
return new WebDriverOptions(
$this->executor,
$this->sessionID
);
}

/**
* An abstraction allowing the driver to access the browser's history and to
* navigate to a given URL.
Expand Down
35 changes: 35 additions & 0 deletions WebDriverOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
// Copyright 2004-present Facebook. All Rights Reserved.

/**
* Managing stuff you would do in a browser.
*/
class WebDriverOptions {

protected $executor;
protected $sessionID;

public function __construct($executor, $session_id) {
$this->executor = $executor;
$this->sessionID = $session_id;
}

/**
* Return the interface for managing driver timeouts.
*
* @return WebDriverTimeouts
*/
public function timeouts() {
return new WebDriverTimeouts($this->executor, $this->sessionID);
}

private function execute($name, array $params = array()) {
$command = array(
'sessionId' => $this->sessionID,
'name' => $name,
'parameters' => $params,
);
$raw = $this->executor->execute($command);
return $raw['value'];
}
}
64 changes: 64 additions & 0 deletions WebDriverTimeouts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
// Copyright 2004-present Facebook. All Rights Reserved.

/**
* Managing timeout behavior for WebDriver instances.
*/
class WebDriverTimeouts {

protected $executor;
protected $sessionID;

public function __construct($executor, $session_id) {
$this->executor = $executor;
$this->sessionID = $session_id;
}

/**
* Specify the amount of time the driver should wait when searching for an
* element if it is not immediately present.
*
* @param int $seconds Wait time in second.
* @return WebDriverTimeouts The current instance.
*/
public function implicitlyWait($seconds) {
$this->execute('setImplicitWaitTimeout', array('ms' => $seconds * 1000));
return $this;
}

/**
* Set the amount of time to wait for an asynchronous script to finish
* execution before throwing an error.
*
* @param int $seconds Wait time in second.
* @return WebDriverTimeouts The current instance.
*/
public function setScriptTimeout($seconds) {
$this->execute('setScriptTimeout', array('ms' => $seconds * 1000));
return $this;
}

/**
* Set the amount of time to wait for a page load to complete before throwing
* an error.
*
* @param int $seconds Wait time in second.
* @return WebDriverTimeouts The current instance.
*/
public function pageLoadTimeout($seconds) {
$this->execute('setPageLoadTimeout', array(
'type' => 'page load',
'ms' => $seconds * 1000,
));
return $this;
}

private function execute($name, array $params = array()) {
$command = array(
'sessionId' => $this->sessionID,
'name' => $name,
'parameters' => $params,
);
$raw = $this->executor->execute($command);
}
}
2 changes: 2 additions & 0 deletions __init__.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
require_once('WebDriverExceptions.php');
require_once('WebDriverExpectedCondition.php');
require_once('WebDriverNavigation.php');
require_once('WebDriverOptions.php');
require_once('WebDriverSelect.php');
require_once('WebDriverTimeouts.php');
require_once('WebDriverWait.php');
require_once('remote/WebDriverBrowserType.php');
require_once('remote/WebDriverCapabilityType.php');
Expand Down
7 changes: 5 additions & 2 deletions remote/WebDriverCommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class WebDriverCommandExecutor {
* http://code.google.com/p/selenium/wiki/JsonWireProtocol#Command_Reference
*/
private $commands = array(
'addCookie' => array('method' => 'POST', 'url' => '/session/:sessionId/cookie'),
'clear' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/clear'),
'clickElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/click'),
'executeScript' => array('method' => 'POST', 'url' => '/session/:sessionId/execute'),
Expand All @@ -28,7 +29,7 @@ class WebDriverCommandExecutor {
'findElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element'),
'findElements' => array('method' => 'POST', 'url' => '/session/:sessionId/elements'),
'get' => array('method' => 'POST', 'url' => '/session/:sessionId/url'),
'getAllCookie' => array('method' => 'GET', 'url' => '/session/:sessionId/cookie'),
'getAllCookies' => array('method' => 'GET', 'url' => '/session/:sessionId/cookie'),
'getElementAttribute' => array('method' => 'GET', 'url' => '/session/:sessionId/element/:id/attribute/:name'),
'getElementCSSValue' => array('method' => 'GET', 'url' => '/session/:sessionId/element/:id/css/:propertyName'),
'getCurrentURL' => array('method' => 'GET', 'url' => '/session/:sessionId/url'),
Expand All @@ -43,9 +44,11 @@ class WebDriverCommandExecutor {
'isElementSelected'=> array('method' => 'GET', 'url' => '/session/:sessionId/element/:id/selected'),
'newSession' => array('method' => 'POST', 'url' => '/session'),
'refreshPage' => array('method' => 'POST', 'url' => '/session/:sessionId/refresh'),
'setImplicitWaitTimeout' => array('method' => 'POST', 'url' => '/session/:sessionId/timeouts/implicit_wait'),
'setPageLoadTimeout' => array('method' => 'POST', 'url' => '/session/:sessionId/timeouts'),
'setScriptTimeout' => array('method' => 'POST', 'url' => '/session/:sessionId/timeouts/async_script'),
'quit' => array('method' => 'DELETE', 'url' => '/session/:sessionId'),
'sendKeysToElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/value'),
'setCookie' => array('method' => 'POST', 'url' => '/session/:sessionId/cookie'),
'submitElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/submit'),
);

Expand Down

0 comments on commit c19952f

Please sign in to comment.