Permalink
Browse files

Progress on (probably futile) attempt to template Taxi API tests with…

… optional driver param
  • Loading branch information...
1 parent 77919d5 commit ebeb4448681becf4b0f1b837a9a4cd879811038e @semperos committed Nov 6, 2012
Showing with 72 additions and 61 deletions.
  1. +50 −41 test/clj_webdriver/test/taxi.clj
  2. +22 −20 test/clj_webdriver/test/util.clj
@@ -8,40 +8,47 @@
[clj-webdriver.test.example-app.core :as web-app])
(:import [org.openqa.selenium TimeoutException NoAlertPresentException]))
-;; Alternate driver (not used with Taxi's set-driver!)
-(def alt-driver (atom nil))
+(def templated (or (read-string (get (System/getenv) "CLJ_WEBDRIVER_TEMPLATED_TEST"))
+ false))
+
+(when templated
+ (def alt-driver (atom nil)))
+
(defn start-browser-fixture
[f]
- (set-driver! {:browser :firefox})
- (reset! alt-driver (core/new-driver {:browser :firefox}))
+ (if templated
+ (reset! alt-driver (core/new-driver {:browser :firefox}))
+ (set-driver! {:browser :firefox}))
(f))
(defn reset-browser-fixture
[f]
- (to test-base-url)
- (to @alt-driver test-base-url)
+ (if templated
+ (to @alt-driver test-base-url)
+ (to test-base-url))
(f))
(defn quit-browser-fixture
[f]
(f)
- (quit)
- (quit @alt-driver))
+ (if templated
+ (quit @alt-driver)
+ (quit)))
(use-fixtures :once start-server start-browser-fixture quit-browser-fixture)
(use-fixtures :each reset-browser-fixture)
;; RUN TESTS HERE
(deftest-template-param
- test-browser-basics @alt-driver
+ test-browser-basics templated @alt-driver
(is (= (class *driver*) clj_webdriver.driver.Driver))
(is (= (current-url __) test-base-url))
(is (= (title __) "Ministache"))
(is (re-find #"(?i)html>" (page-source __))))
(deftest-template-param
- back-forward-should-traverse-browser-history @alt-driver
+ back-forward-should-traverse-browser-history templated @alt-driver
(click __ (find-element __ {:tag :a, :text "example form"}))
(is (= (current-url __) (str test-base-url "example-form")))
(back __)
@@ -55,13 +62,13 @@
(is (= (current-url __) (str test-base-url "clojure"))))
(deftest-template-param
- to-should-open-given-url-in-browser @alt-driver
+ to-should-open-given-url-in-browser templated @alt-driver
(to __ (str test-base-url "example-form"))
(is (= (current-url __) (str test-base-url "example-form")))
(is (= (title __) "Ministache")))
(deftest-template-param
- test-cookie-handling @alt-driver
+ test-cookie-handling templated @alt-driver
(add-cookie __ {:name "my_cookie" :value "my_cookie_value"})
(is (> (count (cookies __)) 0))
(is (= (:value (cookie __ "my_cookie")) "my_cookie_value"))
@@ -71,7 +78,7 @@
(is (zero? (count (cookies __)))))
(deftest-template-param
- test-finding-elements-with-CSS @alt-driver
+ test-finding-elements-with-CSS templated @alt-driver
(click __ (find-element __ {:tag :a, :text "example form"}))
(is (= (attribute __ "#first_name" :id) "first_name"))
(is (= (attribute __ "*[type='text']" :id) "first_name"))
@@ -102,7 +109,7 @@
(is (= (count (elements __ "a[class*='exter'][href*='github']")) 2)))
(deftest-template-param
- test-querying-under-elements @alt-driver
+ test-querying-under-elements templated @alt-driver
(is (= (text (find-element-under "div#content" (core/by-css "a.external"))) "Moustache"))
(is (= (text (find-element-under "div#content" {:css "a.external"})) "Moustache"))
(is (= (text (find-element-under "div#content" {:tag :a, :class "external"})) "Moustache"))
@@ -113,7 +120,7 @@
(is (= (count (find-elements-under "div#content" {:css "a[class*='exter']"})) 2)))
(deftest-template-param
- text-exists-visible-present @alt-driver
+ text-exists-visible-present templated @alt-driver
(is (exists? __ "a"))
(is (not (exists? __ "area")))
(is (exists? __ "a[href='#pages']"))
@@ -125,21 +132,23 @@
(is (not (present? __ "a[href='#pages']"))))
(deftest-template-param
- test-drag-and-drop @alt-driver
- (click (find-element {:tag :a, :text "javascript playground"}))
- (is (present? "#draggable"))
- (let [el-to-drag (element {:id "draggable"})
- {o-x :x o-y :y} (location el-to-drag)
+ test-drag-and-drop templated @alt-driver
+ (click __ (find-element __ {:tag :a, :text "javascript playground"}))
+ (println (str "Draggable page source:" (page-source __)))
+ (is (exists? __ "#draggable"))
+ (is (present? __ "#draggable"))
+ (let [el-to-drag (element __ {:id "draggable"})
+ {o-x :x o-y :y} (location __ el-to-drag)
{n-x :x n-y :y} (do
- (drag-and-drop-by el-to-drag {:x 20 :y 20})
- (location el-to-drag))
+ (drag-and-drop-by __ el-to-drag {:x 20 :y 20})
+ (location __ el-to-drag))
x-diff (Math/abs (- n-x o-x))
y-diff (Math/abs (- n-y o-y))]
(is (= x-diff 20))
(is (= y-diff 20))))
(deftest-template-param
- drag-and-drop-on-elements-should-work @alt-driver
+ drag-and-drop-on-elements-should-work templated @alt-driver
(click (find-element {:tag :a, :text "javascript playground"}))
;; Just check to make sure this page still has the element we expect,
;; since it's an external site
@@ -156,21 +165,21 @@
(is (re-find #"ui-state-highlight" (attribute droppable :class)))))
(deftest-template-param
- test-element-intersection @alt-driver
+ test-element-intersection templated @alt-driver
(click (find-element {:tag :a, :text "example form"}))
(is (intersects? "#first_name" "#personal-info-wrapper"))
(is (not (intersects? "#first_name" "#last_name"))))
(deftest-template-param
- test-xpath-output @alt-driver
+ test-xpath-output templated @alt-driver
(is (= (xpath "a.external") "/html/body/div[2]/div/p/a")))
(deftest-template-param
- test-html-output @alt-driver
+ test-html-output templated @alt-driver
(is (re-find #"href=\"https://github\.com/cgrand/moustache\"" (html "a.external"))))
(deftest-template-param
- test-table-finding @alt-driver
+ test-table-finding templated @alt-driver
(is (= (current-url) test-base-url))
(is (exists? "#pages-table"))
(is (= (lower-case (tag (find-table-cell "#pages-table" [0 0]))) "th"))
@@ -182,7 +191,7 @@
(is (= (lower-case (tag (first (find-table-row (element"#pages-table") 1)))) "td")))
(deftest-template-param
- form-elements @alt-driver
+ form-elements templated @alt-driver
(click (find-element {:tag :a :text, "example form"}))
;; Clear
(clear "form#example_form input[id^='last_']")
@@ -262,7 +271,7 @@
(is (zero? (count (selected-options q))))))
(deftest-template-param
- test-quickfill @alt-driver
+ test-quickfill templated @alt-driver
(click (find-element {:tag :a :text, "example form"}))
(is (= (count (quick-fill
{"#first_name" clear}
@@ -299,7 +308,7 @@
{"select#countries" #(select-by-value % "france")}))))
(deftest-template-param
- test-window-handling @alt-driver
+ test-window-handling templated @alt-driver
(is (= (count (windows)) 1))
(is (= (:title (window)) "Ministache"))
(click "a[target='_blank'][href*='clojure']")
@@ -314,7 +323,7 @@
(is (= (:url (window)) test-base-url)))
(deftest-template-param
- test-waiting-until @alt-driver
+ test-waiting-until templated @alt-driver
(is (= (title) "Ministache"))
(execute-script "setTimeout(function () { window.document.title = \"asdf\"}, 3000)")
(wait-until #(= (title) "asdf") 5000 0)
@@ -332,7 +341,7 @@
)
(deftest-template-param
- test-implicit-wait @alt-driver
+ test-implicit-wait templated @alt-driver
(implicit-wait 3000)
(execute-script "setTimeout(function () { window.document.body.innerHTML = \"<div id='test'>hi!</div>\"}, 1000)")
(is (= (attribute "#test" :id) "test")))
@@ -355,7 +364,7 @@
;; (is (exclusive-between (count (elements "a")) 50 100)))
(deftest-template-param
- test-frames-by-element @alt-driver
+ test-frames-by-element templated @alt-driver
(to "http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html")
(is (= (count (elements "frame")) 3))
(switch-to-frame "frame[name='packageListFrame']")
@@ -368,11 +377,11 @@
(is (exclusive-between (count (elements "a")) 50 100)))
(deftest-template-param
- test-flash @alt-driver
+ test-flash templated @alt-driver
(is (= (attribute (flash "a.external") :class) "external")))
(deftest-template-param
- test-using-xpath-instead-of-css @alt-driver
+ test-using-xpath-instead-of-css templated @alt-driver
(set-finder! xpath-finder)
(click (find-element {:tag :a :text, "example form"}))
(is (= (text {:tag :a, :text "home"}) "home"))
@@ -383,7 +392,7 @@
(is (exists? (find-element {:text "File's Name"}))))
(deftest-template-param
- test-alert-dialog-handling @alt-driver
+ test-alert-dialog-handling templated @alt-driver
(click (find-element {:tag :a, :text "example form"}))
(let [act (fn [] (click "button"))]
(act)
@@ -399,7 +408,7 @@
(alert-obj)))))
(deftest-template-param
- test-window-size @alt-driver
+ test-window-size templated @alt-driver
(let [orig-size (window-size)
small {:width 500 :height 400}
large {:width 1024 :height 800}]
@@ -411,7 +420,7 @@
(is (= (window-size) orig-size))))
(deftest-template-param
- test-window-resize-with-one-dimension @alt-driver
+ test-window-resize-with-one-dimension templated @alt-driver
(let [orig-size (window-size)
small {:height 400}
large {:width 1024}]
@@ -423,7 +432,7 @@
(is (= (:height (window-size)) (:height orig-size)))))
(deftest-template-param
- test-window-position @alt-driver
+ test-window-position templated @alt-driver
(let [origin (window-position)
new-position {:x 100 :y 245}]
(window-reposition new-position)
@@ -432,7 +441,7 @@
(is (= (window-position) origin))))
(deftest-template-param
- test-window-reposition-with-one-coordinate @alt-driver
+ test-window-reposition-with-one-coordinate templated @alt-driver
(let [origin (window-position)
position-y {:y 245}
position-x {:x 100}]
@@ -444,7 +453,7 @@
(is (= (:y (window-position)) (:y origin)))))
(deftest-template-param
- test-window-maximizing @alt-driver
+ test-window-maximizing templated @alt-driver
(let [orig-size (window-size (window-resize {:width 300 :height 300}))
max-size (window-size (window-maximize))]
(is (> (:width max-size) (:width orig-size)))
@@ -34,17 +34,22 @@
[item l]
(cond
(empty? l) '()
-
- (not (list? (first l)))
- (cond
- (= (first l) item) (multirember item (next l))
- :else (cons (first l) (multirember item (next l))))
-
- :else (cons (multirember item (first l)) (multirember item (next l)))))
+
+ (list? (first l))
+ (cons (multirember item (first l)) (multirember item (next l)))
+
+ (vector? (first l))
+ (cons (vec (multirember item (first l))) (multirember item (next l)))
+
+ :else (cond
+ (= (first l) item) (multirember item (next l))
+ :else (cons (first l) (multirember item (next l))))))
(defmacro deftest-template-param
"Create two deftest's for the price of one. Every instance of `__` in the body of this macro will be (1) replaced with `optional-param` for one deftest definition and (2) will be deleted for the other deftest definition generated by this macro. If there are no replacements to be made, the macro will not produce an extra deftest.
+ The `templated` boolean has been added because having both browsers going at once is causing issues. Only generate one set of tests per run.
+
Note: Though this macro looks like it fills the same role as clojure.template/apply-template, the apply-template macro can only replace templated values with other values. We do that (using apply-template), but we also produce a deftest form that completely removes the templated portion, since the intention is to template in an optional argument to functions.
Example:
@@ -57,16 +62,13 @@
(is (= (attribute \"div\" \"class\") \"active\")))
(deftest test-attribute-with-optional-param
(is (= (attribute my-driver \"div\" \"class\") \"active\"))))"
- [test-name optional-param & body]
- (let [body-no-param (multirember '__ body)
- body-with-param (apply-template '[__] body [optional-param])
- test-name-no-param test-name
- test-name-with-param (symbol (str test-name "-with-optional-param"))]
- (if (= body-no-param body-with-param)
- `(deftest ~test-name-no-param
- ~@body-no-param)
- `(do
- (deftest ~test-name-no-param
- ~@body-no-param)
- (deftest ~test-name-with-param
- ~@body-with-param)))))
+ [test-name templated? optional-param & body]
+ (let [body-with-param (apply-template '[__] body [optional-param])
+ body-no-param (multirember '__ body)
+ test-name-with-param (symbol (str test-name "-with-optional-param"))
+ test-name-no-param test-name]
+ `(if ~templated?
+ (deftest ~test-name-with-param
+ ~@body-with-param)
+ (deftest ~test-name
+ ~@body-no-param))))

0 comments on commit ebeb444

Please sign in to comment.