Permalink
Browse files

Add screenshot functionality, fixes #14

  • Loading branch information...
1 parent adcc46b commit a52070fac5dd38e3a52267fe3161df0fd4290622 @semperos semperos committed Nov 11, 2011
Showing with 57 additions and 34 deletions.
  1. +10 −8 src/clj_webdriver/core.clj
  2. +45 −26 src/clj_webdriver/core_driver.clj
  3. +2 −0 test/clj_webdriver/test/core.clj
@@ -16,11 +16,12 @@
(:use [clj-webdriver driver element util window-handle options])
(:require [clj-webdriver.js.browserbot :as browserbot-js]
[clj-webdriver.cache :as cache]
+ [clojure.java.io :as jio]
[clojure.tools.logging :as log])
(:import [clj_webdriver.driver Driver]
[clj_webdriver.element Element]
[org.openqa.selenium By WebDriver WebElement Cookie
- NoSuchElementException]
+ OutputType NoSuchElementException]
[org.openqa.selenium.firefox FirefoxDriver]
[org.openqa.selenium.ie InternetExplorerDriver]
[org.openqa.selenium.chrome ChromeDriver]
@@ -59,16 +60,17 @@
;;; Protocols for API ;;;
(defprotocol IDriver
"Basics of driver handling"
- (get-url [driver url] "Navigate the driver to a given URL")
- (to [driver url] "Navigate to a particular URL. Arg `url` can be either String or java.net.URL. Equivalent to the `get` function, provided here for compatibility with WebDriver API.")
+ (back [driver] "Go back to the previous page in \"browsing history\"")
+ (close [driver] "Close this browser instance, switching to an active one if more than one is open")
(current-url [driver] "Retrieve the URL of the current page")
- (title [driver] "Retrieve the title of the current page as defined in the `head` tag")
+ (forward [driver] "Go forward to the next page in \"browsing history\".")
+ (get-screenshot [driver] [driver format] [driver format destination] "Take a screenshot using Selenium-WebDriver's getScreenshotAs method")
+ (get-url [driver url] "Navigate the driver to a given URL")
(page-source [driver] "Retrieve the source code of the current page")
- (close [driver] "Close this browser instance, switching to an active one if more than one is open")
(quit [driver] "Destroy this browser instance")
- (back [driver] "Go back to the previous page in \"browsing history\"")
- (forward [driver] "Go forward to the next page in \"browsing history\".")
- (refresh [driver] "Refresh the current page"))
+ (refresh [driver] "Refresh the current page")
+ (title [driver] "Retrieve the title of the current page as defined in the `head` tag")
+ (to [driver url] "Navigate to a particular URL. Arg `url` can be either String or java.net.URL. Equivalent to the `get` function, provided here for compatibility with WebDriver API."))
;;; ## Windows and Frames ##
(defprotocol ITargetLocator
@@ -4,25 +4,11 @@
;;; Basic Functions ;;;
IDriver
- (get-url [driver url]
- (.get (:webdriver driver) url)
- (cache/seed driver)
- driver)
-
- (to [driver url]
- (.to (.navigate (:webdriver driver)) url)
+ (back [driver]
+ (.back (.navigate (:webdriver driver)))
(cache/seed driver)
driver)
-
- (current-url [driver]
- (.getCurrentUrl (:webdriver driver)))
-
- (title [driver]
- (.getTitle (:webdriver driver)))
-
- (page-source [driver]
- (.getPageSource (:webdriver driver)))
-
+
(close [driver]
(let [handles (window-handles* (:webdriver driver))]
(if (> (count handles) 1) ; get back to a window that is open before proceeding
@@ -39,26 +25,59 @@
(do
(.close (:webdriver driver))
(cache/seed driver {})))))
-
- (quit [driver]
- (.quit (:webdriver driver))
- (cache/seed driver {}))
-
- (back [driver]
- (.back (.navigate (:webdriver driver)))
- (cache/seed driver)
- driver)
+
+ (current-url [driver]
+ (.getCurrentUrl (:webdriver driver)))
(forward [driver]
(.forward (.navigate (:webdriver driver)))
(cache/seed driver)
driver)
+
+ (get-url [driver url]
+ (.get (:webdriver driver) url)
+ (cache/seed driver)
+ driver)
+
+ (get-screenshot
+ ([driver] (get-screenshot driver :file))
+ ([driver format] (get-screenshot driver format nil))
+ ([driver format destination]
+ {:pre [(or (= format :file)
+ (= format :base64)
+ (= format :bytes))]}
+ (let [wd (:webdriver driver)
+ output (case format
+ :file (.getScreenshotAs wd OutputType/FILE)
+ :base64 (.getScreenshotAs wd OutputType/BASE64)
+ :bytes (.getScreenshotAs wd OutputType/BYTES))]
+ (if destination
+ (do
+ (jio/copy output (jio/file destination))
+ (log/info "Screenshot written to destination")
+ output)
+ output))))
+
+ (page-source [driver]
+ (.getPageSource (:webdriver driver)))
+
+ (quit [driver]
+ (.quit (:webdriver driver))
+ (cache/seed driver {}))
(refresh [driver]
(.refresh (.navigate (:webdriver driver)))
(cache/seed driver)
driver)
+ (title [driver]
+ (.getTitle (:webdriver driver)))
+
+ (to [driver url]
+ (.to (.navigate (:webdriver driver)) url)
+ (cache/seed driver)
+ driver)
+
;;; Window and Frame Handling ;;;
ITargetLocator
@@ -373,6 +373,8 @@
(quit temp-dr)))
+;; TODO: write tests for screenshot functionality, ensure diff outputs
+;;(deftest test-screenshot-should-return-different-outputs)
;; ## Tests (sans cache) ##

0 comments on commit a52070f

Please sign in to comment.