diff --git a/WebDriver/Driver.php b/WebDriver/Driver.php index df0f3bd..7325ccf 100644 --- a/WebDriver/Driver.php +++ b/WebDriver/Driver.php @@ -1,5 +1,7 @@ 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) { @@ -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"); @@ -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) {