Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions WebDriver/Driver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require 'WebElement.php';

class WebDriver_Driver {
protected $session_id;
protected $server_url;
Expand Down Expand Up @@ -188,7 +190,58 @@ public function get_element($locator) {
$element_id = WebDriver::GetJSONValue($response, "ELEMENT");
return new WebDriver_WebElement($this, $element_id, $locator);
}


// Added 31-oct-11, dbyron
public function wait_for_selected_window($window_handle,$end_time = NULL) {
$start_time = time();
if ($end_time == NULL)
{
$end_time = $start_time + WebDriver::$ImplicitWaitMS/1000;
}
do {
echo "wait_for_selected_window: calling get_window_handle\n";
$current_selected_window = $this->get_window_handle();
echo "wait_for_selected_window: back from get_window_handle\n";
if ($window_handle == $current_selected_window)
{
return;
}
} while (time() < $end_time);
throw new Exception("timeout waiting for window handle " . $window_handle . " to be the currently selected window");
}

// Added 22-aug-11, dbyron
public function wait_for_element($locator,$end_time = NULL) {
$start_time = time();
if ($end_time == NULL)
{
$end_time = $start_time + WebDriver::$ImplicitWaitMS/1000;
}
do {
$retval = $this->get_element($locator);
if ($retval)
{
return $retval;
}
} while (time() < $end_time);
throw new Exception("timeout looking for element \"" . $locator . "\"");
}

// Added 22-aug-11, dbyron
public function wait_for_element_value($locator,$compare_func) {
$start_time = time();
$end_time = $start_time + WebDriver::$ImplicitWaitMS/1000;
$element = $this->wait_for_element($locator,$end_time);
do {
$this_value = $element->get_value();
if ($compare_func($this_value))
{
return;
}
} while (time() < $end_time);
throw new Exception("timeout waiting for value of element \"" . $locator . "\", last value: \"" . $this_value . "\"");
}

// WebDriver can do implicit waits for AJAX elements, but sometimes you need explicit reloads
// Note: is_element_present() will use the wait time, if any, that you've set with set_implicit_wait()
public function get_element_reload($locator, $max_wait_minutes = 2) {
Expand Down Expand Up @@ -472,7 +525,29 @@ private function click_mouse($button) {
public function click() { $this->click_mouse(0); }
public function middle_click() { $this->click_mouse(1); }
public function right_click() { $this->click_mouse(2); }


// Added 27-oct-11, dbyron)
public function safe_click($element,$expected_title_after_click) {
// For some reason asking Selenium to click isn't
// reliable. Even when it responds with what looks
// like a successful HTTP response, the browser
// doesn't navigate to the page.
$element->click();

try
{
$this->assert_title($expected_title_after_click);
}
catch (Exception $ex)
{
// Failing once is OK. Try clicking again.
$element->click();

// If this fails, we let the exception go.
$this->assert_title($expected_title_after_click);
}
}

// See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttondown
public function click_and_hold() {
$this->execute("POST", "/session/:sessionId/buttondown");
Expand Down Expand Up @@ -616,7 +691,7 @@ public function assert_title($expected_title, $ie_hash = '') {
$actual_title = $this->get_title();
$title_matched = ($this->browser == 'internet explorer' && $actual_title == $expected_title . $ie_hash) || ($actual_title == $expected_title);
} while (time() < $end_time && !$title_matched);
PHPUnit_Framework_Assert::assertTrue($title_matched, "Failed asserting that <$actual_title> is <$expected_title> with optional hash <$ie_hash>.");
PHPUnit_Framework_Assert::assertTrue($title_matched, "Failed assertion: actual title <$actual_title> is not expected title <$expected_title> with optional hash <$ie_hash>.");
}

public function assert_element_present($element_locator) {
Expand Down