Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit cdf7e8317c46a8ed09fc717a78f6735695e803af Austin Matzko committed May 31, 2011
Showing with 222 additions and 0 deletions.
  1. +107 −0 SearchContext.php
  2. +85 −0 WebDriver.php
  3. +30 −0 WebElement.php
@@ -0,0 +1,107 @@
+<?php
+
+class SearchContext
+{
+ protected $_driver;
+ protected $_session_id;
+ protected $_request_url;
+ protected $_host;
+ protected $_port = '4443';
+
+ public function __construct( $host, $port = '4443', $request_url = null )
+ {
+ $this->_host = $host;
+ $this->_port = $port;
+
+ if ( empty( $request_url ) && empty( $this->_host ) ) {
+ throw new InvalidArgumentException( 'The WebDriver API must receive a host URL.' );
+ }
+
+ if ( empty( $request_url ) ) {
+ $this->_request_url = 'http://' . $this->_host . ':' . $this->_port . '/wd/hub';
+ } else {
+ $this->_request_url = $request_url;
+ }
+ }
+
+ protected function _delete_request( $url = '' )
+ {
+ if ( empty( $url ) ) {
+ throw new InvalidArgumentException( 'Invalid request: No delete URL is set.' );
+ }
+
+ $session = curl_init( $url );
+ curl_setopt($session, CURLOPT_HTTPHEADER, array("application/json;charset=UTF-8"));
+ curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'DELETE');
+ curl_setopt($session, CURLOPT_HEADER, false);
+ curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
+
+ $response = trim( curl_exec( $session ) );
+ curl_close( $session );
+
+ return $response;
+ }
+
+ protected function _post_request( $url = '', $args = array(), $force_json = true )
+ {
+ $force_json = (bool) $force_json;
+
+ if ( empty( $url ) ) {
+ throw new InvalidArgumentException( 'Invalid request: URL must be set.' );
+ }
+
+ if ( ! is_array( $args ) ) {
+ throw new InvalidArgumentException( 'Invalid request: Arguments must be an array.' );
+ }
+
+ if ( $force_json ) {
+ $args = json_encode( $args, JSON_FORCE_OBJECT );
+ }
+
+ $session = curl_init( $url );
+ curl_setopt( $session, CURLOPT_HTTPHEADER, array("application/json;charset=UTF-8") );
+ curl_setopt( $session, CURLOPT_POST, true );
+ curl_setopt( $session, CURLOPT_POSTFIELDS, $args );
+ curl_setopt( $session, CURLOPT_HEADER, false );
+ curl_setopt( $session, CURLOPT_RETURNTRANSFER, true );
+ curl_setopt( $session, CURLOPT_FOLLOWLOCATION, true );
+
+ $response = trim( curl_exec( $session ) );
+ curl_close( $session );
+
+ return $response;
+ }
+
+ public function connect( $browser = 'firefox' )
+ {
+ $args = json_encode( array(
+ 'desiredCapabilities' => array(
+ 'browserName' => $browser,
+ 'javascriptEnabled' => 'true',
+ 'nativeEvents' => 'true',
+ ),
+ ), JSON_FORCE_OBJECT );
+
+ $session = curl_init( $this->_request_url . '/session' );
+ curl_setopt( $session, CURLOPT_HTTPHEADER, array("application/json;charset=UTF-8") );
+ curl_setopt( $session, CURLOPT_POST, true );
+ curl_setopt( $session, CURLOPT_POSTFIELDS, $args );
+ curl_setopt( $session, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt( $session, CURLOPT_FOLLOWLOCATION, true);
+ curl_setopt( $session, CURLOPT_HEADER, true );
+
+ $response = trim( curl_exec( $session ) );
+ $header = curl_getinfo($session);
+ if ( ! empty( $header['url'] ) ) {
+ $this->_request_url = $header['url'];
+ $_url_parts = explode('/', $header['url']);
+ $this->_session_id = trim( array_pop( $_url_parts ), '/' );
+ }
+ curl_close( $session );
+ }
+
+}
+
+require_once dirname( __FILE__ ) . '/WebDriver.php';
+require_once dirname( __FILE__ ) . '/WebElement.php';
@@ -0,0 +1,85 @@
+<?php
+
+class WebDriver extends SearchContext
+{
+ public function addCookie( $name = '', $value = null )
+ {
+ $args = array(
+ 'cookie' => array(
+ 'name' => $name,
+ 'value' => $value,
+ 'path' => '/',
+ 'domain' => $this->_domain,
+ 'secure' => true,
+ )
+ );
+
+ $response = $this->_post_request( $this->_request_url . '/cookie', $args );
+ }
+
+ public function close()
+ {
+ $response = $this->_delete_request( $this->_request_url . '/window' );
+ if (
+ isset( $response->value->class ) &&
+ (
+ 'org.openqa.selenium.NoSuchWindowException' == $response->value->class
+ )
+ ) {
+ throw new Exception( $response->value->localizedMessage );
+ }
+ }
+
+ public function findElementBy( $using = 'xpath', $expression = null )
+ {
+ if ( ! in_array(
+ $using,
+ array(
+ 'id',
+ 'xpath',
+ )
+ ) ) {
+ throw new InvalidArgumentException( 'Invalid "using" argument for findElementBy.' );
+ }
+
+ $args = array(
+ 'using' => $using,
+ 'value' => $expression,
+ );
+ $response = json_decode( $this->_post_request( $this->_request_url . '/element', $args ) );
+
+ if ( ! isset( $response->value ) || ! isset( $response->value->ELEMENT ) ) {
+ if (
+ isset( $response->value->class ) &&
+ (
+ 'org.openqa.selenium.NoSuchElementException' == $response->value->class ||
+ 'org.openqa.selenium.StaleElementException' == $response->value->class ||
+ 'org.openqa.selenium.XPathLookupErrorException' == $response->value->class
+ )
+ ) {
+ throw new Exception( $response->value->localizedMessage );
+ } else {
+ throw new Exception( 'No matching element found.' );
+ }
+ }
+
+ $element = new WebElement( null, null, $this->_request_url );
+ $element->session_id = $this->_session_id;
+ $element->session_element_id = $response->value->ELEMENT;
+
+ return $element;
+ }
+
+ public function get( $url = '' )
+ {
+ if ( empty( $url ) ) {
+ throw new InvalidArgumentException( 'Requested URL cannot be empty.' );
+ }
+
+ $args = array(
+ 'url' => $url,
+ );
+
+ $response = $this->_post_request( $this->_request_url . '/url', $args );
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+
+class WebElement extends SearchContext
+{
+ public $session_id = false;
+ public $session_element_id = false;
+
+ public function elementExists()
+ {
+ return (bool) ( false !== $this->session_element_id );
+ }
+
+ public function click()
+ {
+ $args = array(
+ 'sessionId' => $this->session_id,
+ 'id' => $this->session_element_id,
+ );
+ $url = $this->_request_url . '/element/' . $this->session_element_id . '/click';
+ $response = json_decode( $this->_post_request( $url ) );
+ if (
+ isset( $response->value->class ) &&
+ 'org.openqa.selenium.ElementNotVisibleException' == $response->value->class
+ ) {
+ if ( ! empty( $response->value->localizedMessage ) ) {
+ throw new Exception( $response->value->localizedMessage );
+ }
+ }
+ }
+}

0 comments on commit cdf7e83

Please sign in to comment.