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

Commit

Permalink
Get core protocols in core, others moved into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
semperos committed Oct 27, 2011
1 parent 4a60ace commit aa61691
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 90 deletions.
59 changes: 25 additions & 34 deletions src/clj_webdriver/core.clj
Expand Up @@ -13,7 +13,7 @@
;; WebDriver API.
;;
(ns clj-webdriver.core
(:use [clj-webdriver util window-handle target-locator wait options cache])
(:use [clj-webdriver util window-handle options])
(:require [clj-webdriver.js.browserbot :as browserbot-js]
[fogus.clache :as clache])
(:import [org.openqa.selenium By WebDriver WebElement Cookie
Expand All @@ -35,6 +35,7 @@
:chrome ChromeDriver
:htmlunit HtmlUnitDriver})

;; TODO: Use precondition instead of throwing an exception
(defn new-webdriver*
"Instantiate a new WebDriver instance given a browser type. If an additional profile object or string is passed in, Firefox will be started with the given profile instead of the default."
([browser]
Expand All @@ -56,16 +57,6 @@
([browser cache-strategy cache-args]
(init-driver (new-webdriver* browser) cache-strategy cache-args)))

(defn start
"Shortcut to instantiate a driver, navigate to a URL, and return the driver for further use"
([browser url] (start browser url :driver))
([browser url driver-type]
(let [driver (if (= :webdriver driver-type)
(new-webdriver* browser)
(new-driver browser))]
(get-url driver url)
driver)))

;;; Protocols for API ;;;
(defprotocol IDriver
"Basics of driver handling"
Expand All @@ -80,6 +71,18 @@
(forward [driver] "Go forward to the next page in \"browsing history\".")
(refresh [driver] "Refresh the current page"))

;;; ## Windows and Frames ##
(defprotocol ITargetLocator
"Functions that deal with browser windows and frames"
(window-handle [driver] "Get the only (or first) window handle, return as a WindowHandler record")
(window-handles [driver] "Retrieve a vector of `WindowHandle` records which can be used to switchTo particular open windows")
(other-window-handles [driver] "Retrieve window handles for all windows except the current one")
(switch-to-frame [driver frame] "Switch focus to a particular HTML frame")
(switch-to-window [driver handle] "Switch focus to a particular open window")
(switch-to-other-window [driver] "Given that two and only two browser windows are open, switch to the one not currently active")
(switch-to-default [driver] "Switch focus to the first first frame of the page, or the main document if the page contains iframes")
(switch-to-active [driver] "Switch to element that currently has focus, or to the body if this cannot be detected"))

(defprotocol IFind
"Functions used to locate elements on a given page"
(find-element [driver by] "Retrieve the element object of an element described by `by`")
Expand All @@ -101,6 +104,16 @@
[driver attr-val]
[driver tag attr-val] "Call (first (find-them args))"))

(defn start
"Shortcut to instantiate a driver, navigate to a URL, and return the driver for further use"
([browser url] (start browser url :driver))
([browser url driver-type]
(let [driver (if (= :webdriver driver-type)
(new-webdriver* browser)
(new-driver browser))]
(get-url driver url)
driver)))


;; We've defined our own record type WindowHandler because
;; the String id which WebDriver returns by default to identify
Expand Down Expand Up @@ -133,25 +146,7 @@
(remove #(= % (window-handle* driver))
(doall (window-handles* driver))))

;; Functions dealing directly with cookie objects
(defn new-cookie
"Create a new cookie instance"
([name value] (new-cookie name value "/" nil))
([name value path] (new-cookie name value path nil))
([name value path date] (new Cookie name value path date)))

(defn cookie-name
"Retrieve the name of a particular cookie"
[cookie]
(.getName cookie))

(defn cookie-value
"Retrieve the value of a particular cookie"
[cookie]
(.getValue cookie))

;; ## By* Functions ##

(load "core_by")

;; ## Actions on WebElements ##
Expand Down Expand Up @@ -327,14 +322,9 @@
(.executeScript driver js (to-array js-args))
driver)

;; TODO: Script Timeout (wait functionality)

;; ## Select Helpers ##

(load "core_select")

;; ## Element-finding Utilities ##

;; Helper function to find-*
(defn filter-elements-by-regex
"Given a collection of WebElements, filter the collection by the regular expression values for the respective attributes in the `attr-val` map"
Expand All @@ -361,3 +351,4 @@
(load "core_driver")
;; API with Selenium-WebDriver's WebDriver class
(load "core_webdriver")

23 changes: 1 addition & 22 deletions src/clj_webdriver/core_driver.clj
Expand Up @@ -51,9 +51,8 @@
driver)


;;; Windows and Frames ;;;
;;; Window and Frame Handling ;;;
ITargetLocator

(window-handle [driver]
(init-window-handle (:webdriver driver)
(.getWindowHandle (:webdriver driver))
Expand Down Expand Up @@ -109,27 +108,8 @@
(.activeElement (.switchTo (:webdriver driver))))


;;; Wait Functionality ;;;
IWait

(implicit-wait [driver timeout]
(.implicitlyWait (.. (:webdriver driver) manage timeouts) timeout TimeUnit/MILLISECONDS)
driver)

(wait-until [driver pred]
(wait-until driver pred 5000 0))
(wait-until [driver pred timeout]
(wait-until driver pred timeout 0))
(wait-until [driver pred timeout interval]
(let [wait (WebDriverWait. (:webdriver driver) (/ timeout 1000) interval)]
(.until wait (proxy [ExpectedCondition] []
(apply [d] (pred d))))
driver))


;;; Options Interface (cookies) ;;;
IOptions

(add-cookie [driver cookie]
(.addCookie (.manage (:webdriver driver)) cookie))
(delete-cookie-named [driver cookie]
Expand All @@ -146,7 +126,6 @@

;;; Find Functions ;;;
IFind

(find-element [driver by]
(.findElement (:webdriver driver) by))

Expand Down
21 changes: 1 addition & 20 deletions src/clj_webdriver/core_webdriver.clj
Expand Up @@ -51,9 +51,8 @@
driver)


;;; Windows and Frames ;;;
;;; Window and Frame Handling ;;;
ITargetLocator

(window-handle [driver]
(init-window-handle driver
(.getWindowHandle driver)
Expand Down Expand Up @@ -108,27 +107,9 @@
(switch-to-active [driver]
(.activeElement (.switchTo driver)))


;;; Wait Functionality ;;;
IWait

(implicit-wait [driver timeout]
(.implicitlyWait (.. driver manage timeouts) timeout TimeUnit/MILLISECONDS)
driver)

(wait-until
([driver pred] (wait-until driver pred 5000 0))
([driver pred timeout] (wait-until driver pred timeout 0))
([driver pred timeout interval]
(let [wait (WebDriverWait. driver (/ timeout 1000) interval)]
(.until wait (proxy [ExpectedCondition] []
(apply [d] (pred d))))
driver)))


;;; Options Interface (cookies) ;;;
IOptions

(add-cookie [driver cookie]
(.addCookie (.manage driver) cookie))
(delete-cookie-named [driver cookie]
Expand Down
19 changes: 18 additions & 1 deletion src/clj_webdriver/options.clj
@@ -1,4 +1,5 @@
(ns clj-webdriver.options)
(ns clj-webdriver.options
(:import org.openqa.selenium.Cookie))

(defprotocol IOptions
"Options interface, including cookie and timeout handling"
Expand All @@ -8,3 +9,19 @@
(delete-all-cookies [driver] "Delete all cookies defined in the current session")
(cookies [driver] "Retrieve a set of cookies defined in the current session")
(cookie-named [driver name] "Retrieve a cookie object given its name"))

(defn new-cookie
"Create a new cookie instance"
([name value] (new-cookie name value "/" nil))
([name value path] (new-cookie name value path nil))
([name value path date] (new Cookie name value path date)))

(defn cookie-name
"Retrieve the name of a particular cookie"
[cookie]
(.getName cookie))

(defn cookie-value
"Retrieve the value of a particular cookie"
[cookie]
(.getValue cookie))
12 changes: 0 additions & 12 deletions src/clj_webdriver/target_locator.clj

This file was deleted.

38 changes: 37 additions & 1 deletion src/clj_webdriver/wait.clj
@@ -1,9 +1,45 @@
(ns clj-webdriver.wait)
(ns clj-webdriver.wait
(:import clj_webdriver.core.Driver
org.openqa.selenium.WebDriver))

;;; ## Wait Functionality ##
(defprotocol IWait
"Implicit and explicit waiting"
(implicit-wait [driver timeout] "Specify the amount of time the `driver` should wait when searching for an element if it is not immediately present. This setting holds for the lifetime of the driver across all requests. Units in milliseconds.")
(wait-until
[driver pred]
[driver pred timeout]
[driver pred timeout interval] "Set an explicit wait time `timeout` for a particular condition `pred`. Optionally set an `interval` for testing the given predicate. All units in milliseconds"))

(extend-type Driver

IWait
(implicit-wait [driver timeout]
(.implicitlyWait (.. (:webdriver driver) manage timeouts) timeout TimeUnit/MILLISECONDS)
driver)

(wait-until [driver pred]
(wait-until driver pred 5000 0))
(wait-until [driver pred timeout]
(wait-until driver pred timeout 0))
(wait-until [driver pred timeout interval]
(let [wait (WebDriverWait. (:webdriver driver) (/ timeout 1000) interval)]
(.until wait (proxy [ExpectedCondition] []
(apply [d] (pred d))))
driver)))

(extend-type WebDriver

IWait
(implicit-wait [driver timeout]
(.implicitlyWait (.. driver manage timeouts) timeout TimeUnit/MILLISECONDS)
driver)

(wait-until
([driver pred] (wait-until driver pred 5000 0))
([driver pred timeout] (wait-until driver pred timeout 0))
([driver pred timeout interval]
(let [wait (WebDriverWait. driver (/ timeout 1000) interval)]
(.until wait (proxy [ExpectedCondition] []
(apply [d] (pred d))))
driver))))

0 comments on commit aa61691

Please sign in to comment.