Skip to content

Commit 18124a3

Browse files
authored
tests: fix window switch tests (#581)
Safaridriver is not great with mouse clicks, press enter on link to create new target window instead. - `test-switch-window` - assert window count earlier for clearer failures - `test-switch-window-next` - was using unrealized `repeat`s so was effectively testing nothing. Reviewed and rewrote with some compensations for our odd little safaridriver. Closes #579
1 parent d8e3eba commit 18124a3

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

env/test/resources/static/test2.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>Webdriver Test Document2</title>
55
</head>
66
<body>
7-
<div>
7+
<div id="document2">
88
<div>
99
<div>
1010
<ul>
@@ -37,5 +37,6 @@
3737
</div>
3838
<a href="">link2</a>
3939
</div>
40+
<h3 id="document-end">Document end</h3>
4041
</body>
4142
</html>

test/etaoin/api_test.clj

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
[etaoin.api :as e]
1111
[etaoin.impl.util :as util]
1212
[etaoin.impl.client :as client]
13+
[etaoin.keys :as k]
1314
[etaoin.test-report :as test-report]
1415
[slingshot.slingshot :refer [try+]])
1516
(:import [java.net ServerSocket]))
@@ -489,24 +490,35 @@
489490

490491
(deftest test-switch-window
491492
(let [init-handle (e/get-window-handle *driver*)
492-
init-url (e/get-url *driver*)
493-
_ (e/click *driver* :switch-window)
494-
new-handles (e/get-window-handles *driver*)
495-
new-handle (first (filter #(not= % init-handle) new-handles))
496-
_ (e/switch-window *driver* new-handle)
497-
target-handle (e/get-window-handle *driver*)
498-
target-url (e/get-url *driver*)]
499-
(is (not= init-handle target-handle))
500-
(is (= 2 (count new-handles)))
501-
(is (= new-handle target-handle))
502-
(is (not= init-url target-url))))
493+
init-url (e/get-url *driver*)]
494+
;; press enter on link instead of clicking (safaridriver is not great with the click)
495+
(e/fill *driver* :switch-window k/return)
496+
(is (= 2 (count (e/get-window-handles *driver*))) "2 windows now exist")
497+
(let [new-handles (e/get-window-handles *driver*)
498+
new-handle (first (filter #(not= % init-handle) new-handles))
499+
_ (e/switch-window *driver* new-handle)
500+
target-handle (e/get-window-handle *driver*)
501+
target-url (e/get-url *driver*)]
502+
(is (not= init-handle target-handle))
503+
(is (= new-handle target-handle))
504+
(is (not= init-url target-url)))))
503505

504506
(deftest test-switch-window-next
505-
(let [_ (repeat 3 #(e/click *driver* :switch-window))
506-
init-handle (e/get-window-handle *driver*)
507-
_ (repeat 4 #(e/switch-window-next *driver*))
508-
target-handle (e/get-window-handle *driver*)]
509-
(is (= init-handle target-handle))))
507+
(let [init-handle (e/get-window-handle *driver*)]
508+
(doseq [_ (range 3)]
509+
;; press enter on link instead of clicking (safaridriver is not great with click)
510+
(e/fill *driver* :switch-window k/return)
511+
;; compensate: safari navigates to target window, others stay at source
512+
(e/when-safari *driver*
513+
(e/wait 3) ;; safari seems to need a breather
514+
(e/switch-window *driver* init-handle)))
515+
(is (= 4 (count (e/get-window-handles *driver*))) "4 windows now exist")
516+
(is (= init-handle (e/get-window-handle *driver*)) "on first window")
517+
(doseq [_ (range 3)]
518+
(e/switch-window-next *driver*)
519+
(is (not= init-handle (e/get-window-handle *driver*)) "navigating new windows"))
520+
(e/switch-window-next *driver*)
521+
(is (= init-handle (e/get-window-handle *driver*)) "wrapped around to original window")))
510522

511523
;; TODO: need refactoring not working for headless & firefox
512524
#_
@@ -781,3 +793,22 @@
781793
(e/perform-actions *driver* keyboard mouse)
782794
(e/wait 1)
783795
(is (str/ends-with? (e/get-url *driver*) "?login=1&password=2&message=3"))))))
796+
797+
(comment
798+
;; start test server
799+
(def test-server (p/process {:out :inherit :err :inherit} "bb test-server --port" 9993))
800+
(def url (format "http://localhost:%d/%s" 9993 "test.html"))
801+
802+
;; start your favourite webdriver
803+
(def driver (e/safari))
804+
(def driver (e/firefox))
805+
806+
;; mimic test fixture
807+
(e/go driver url)
808+
(e/wait-visible driver {:id :document-end})
809+
810+
;; cleanup
811+
(e/quit driver)
812+
(p/destroy test-server)
813+
814+
:eoc)

0 commit comments

Comments
 (0)