Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Expose webdriver wait functionality #5

Merged
merged 1 commit into from

2 participants

@xeqi

Create wait-until and implicitly-wait functions for the driver.

Not all functionality is exposed. I imagine testing something like sleep in miliseconds would be ugly, though perhaps a mock would work since its calling a java object.

@xeqi xeqi Expose webdriver wait functionality
Use seconds for implicitly-wait for consistency with wait-until.
f130364
@semperos
Owner

I've been reviewing all the "wait" functionality provided by WebDriver. For WebDriverWait, I think we should support the three-member constructor at a minimum, which allows setting both a timeout and an interval for attempts. I'm not convinced that the five-member constructor adds much value, but I'm open to discussion on that point.

@semperos semperos merged commit f130364 into from
@semperos
Owner

I created conflicts with some subsequent edits before merging this request. Thank you very much for adding this functionality and for adding tests for it. Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 19, 2011
  1. @xeqi

    Expose webdriver wait functionality

    xeqi authored
    Use seconds for implicitly-wait for consistency with wait-until.
This page is out of date. Refresh to see the latest.
View
4 TODO.md
@@ -4,10 +4,6 @@ The following are features I would like to implement or limitations I would like
## Features ##
-### Wait Functionality ###
-
-WebDriver provides explicit and implicit waits.
-
### Grid Support ###
Selenium Grid is now part of the standalone jar distribution of Selenium server. It would be nice to provide mechanisms to easily start the grid and child nodes directly from code.
View
13 src/clj_webdriver/core.clj
@@ -22,9 +22,10 @@
[org.openqa.selenium.ie InternetExplorerDriver]
[org.openqa.selenium.chrome ChromeDriver]
[org.openqa.selenium.htmlunit HtmlUnitDriver]
- [org.openqa.selenium.support.ui Select]
+ [org.openqa.selenium.support.ui Select WebDriverWait ExpectedCondition]
[java.util Date]
- [java.io File]))
+ [java.io File]
+ [java.util.concurrent TimeUnit]))
(def webdriver-drivers
{:firefox FirefoxDriver
@@ -394,3 +395,11 @@
(first (find-them driver attr-val)))
([driver tag attr-val]
(first (find-them driver tag attr-val))))
+
+(defn implicit-wait [driver timeout]
+ (.implicitlyWait (.. driver manage timeouts) timeout TimeUnit/SECONDS))
+
+(defn wait-until [driver pred & {:keys [timeout] :or {timeout 5}}]
+ (let [wait (WebDriverWait. driver timeout)]
+ (.until wait (proxy [ExpectedCondition] []
+ (apply [d] (pred d))))))
View
30 test/clj_webdriver/test/core.clj
@@ -2,7 +2,8 @@
(:require [clj-webdriver.test.example-app.core :as web-app])
(:use [clj-webdriver.core] :reload)
(:use ring.adapter.jetty)
- (:use [clojure.test]))
+ (:use [clojure.test])
+ (:import [org.openqa.selenium TimeoutException]))
;; Setup
(def test-port 5744)
@@ -222,6 +223,33 @@
(close b)
(is (= test-base-url
(:url (window-handle b)))))
+
+(deftest wait-until-should-wait-for-condition
+ (is (= "Ministache" (title b)))
+ (doto b
+ (execute-script "setTimeout(function () { window.document.title = \"asdf\"}, 3000)")
+ (wait-until (fn [d] (= "asdf" (title d)))))
+ (is (= "asdf" (title b))))
+
+(deftest wait-until-should-throw-on-timeout
+ (is (thrown? TimeoutException
+ (doto b
+ (execute-script "setTimeout(function () { window.document.title = \"test\"}, 6000)")
+ (wait-until (fn [d] (= "test" (title d))))))))
+
+(deftest wait-until-should-allow-timeout-argument
+ (is (thrown? TimeoutException
+ (doto b
+ (execute-script "setTimeout(function () { window.document.title = \"test\"}, 10000)")
+ (wait-until (fn [d] (= "test" (title d))) :timeout 1)))))
+
+(deftest implicit-wait-should-cause-find-to-wait
+ (doto b
+ (implicit-wait 3)
+ (execute-script "setTimeout(function () { window.document.body.innerHTML = \"<div id='test'>hi!</div>\"}, 1000)"))
+ (is (= "test"
+ (attribute (find-element b (by-id "test")) :id))))
+
;; TODO:
;; * Form element tests (comprehensive)
;; * Exception throwing (esp. for find-it/find-them argument handling)p
Something went wrong with that request. Please try again.