Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Enhance WindowHandle record, add browser driver to make using find-it…
Browse files Browse the repository at this point in the history
… more elegant with -> (see tests in next commit)
  • Loading branch information
semperos committed Mar 24, 2011
1 parent 34a8e93 commit 2b77f13
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/clj_webdriver/core.clj
Expand Up @@ -71,6 +71,7 @@

(declare window-handles*)
(declare window-handle*)
(declare switch-to-window)
(defn close
"Close this browser instance, switching to an active one if more than one is open"
[driver]
Expand All @@ -79,10 +80,10 @@
(let [this-handle (window-handle* driver)
idx (.indexOf handles this-handle)]
(cond
(zero? idx) (do ; if first window, go to next
(zero? idx) (do ; if first window, switch to next
(.close driver)
(switch-to-window driver (nth handles (inc idx))))
:else (do ; otherwise, go back one window
:else (do ; otherwise, switch back one window
(.close driver)
(switch-to-window driver (nth handles (dec idx))))))
(.close driver))))
Expand All @@ -103,9 +104,10 @@
(defn window-handle
"Get the only (or first) window handle, return as a WindowHandler record"
[driver]
(WindowHandle. (.getWindowHandle driver)
(title driver)
(current-url driver)))
(WindowHandle. driver
(.getWindowHandle driver)
(title driver)
(current-url driver)))

(defn window-handle*
"For WebDriver API compatibility: this simply wraps `.getWindowHandle`"
Expand All @@ -119,7 +121,8 @@
all-handles (seq (.getWindowHandles driver))
handle-records (doall (for [handle all-handles]
(let [b (switch-to-window driver handle)]
(WindowHandle. handle
(WindowHandle. driver
handle
(title b)
(current-url b)))))]
(switch-to-window driver current-handle)
Expand Down Expand Up @@ -174,13 +177,14 @@

(defn switch-to-window
"Switch focus to a particular open window"
[driver handle]
(cond
(string? handle) (.window (.switchTo driver) handle)
(= (class handle) clj-webdriver.record.WindowHandle) (.window (.switchTo driver) (:handle handle))
(number? handle) (switch-to-window driver (nth (window-handles driver) handle))
(nil? handle) (throw (RuntimeException. "No window can be found"))
:else (.window (.switchTo driver) handle)))
([handle] (switch-to-window (:driver handle) handle))
([driver handle]
(cond
(string? handle) (.window (.switchTo driver) handle)
(= (class handle) clj-webdriver.record.WindowHandle) (.window (.switchTo (:driver handle)) (:handle handle))
(number? handle) (switch-to-window driver (nth (window-handles driver) handle))
(nil? handle) (throw (RuntimeException. "No window can be found"))
:else (.window (.switchTo driver) handle))))

(defn switch-to-other-window
"Given that two and only two browser windows are open, switch to the one not currently active"
Expand Down
2 changes: 1 addition & 1 deletion src/clj_webdriver/record.clj
Expand Up @@ -2,4 +2,4 @@

(ns clj-webdriver.record)

(defrecord WindowHandle [handle title url])
(defrecord WindowHandle [driver handle title url])

0 comments on commit 2b77f13

Please sign in to comment.