This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
taxi.clj
1540 lines (1201 loc) · 45.7 KB
/
taxi.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; The faster way to use clj-webdriver: take a taxi
(ns clj-webdriver.taxi
(:use [clj-webdriver.element :only [element-like?]]
[clj-webdriver.driver :only [driver?]])
(:require [clj-webdriver.core :as core]
[clj-webdriver.window :as win]
[clj-webdriver.util :as util]
[clj-webdriver.options :as options]
[clj-webdriver.wait :as wait])
(:import clj_webdriver.element.Element))
(declare css-finder)
(def ^:dynamic *driver*)
(def ^:dynamic *finder-fn* css-finder)
(def ^{:doc (str "Alias of clj-webdriver.core/new-driver:\n"
(:doc (meta #'clj-webdriver.core/new-driver)))
:arglists (:arglists (meta #'clj-webdriver.core/new-driver))}
new-driver clj-webdriver.core/new-driver)
(defn- set-driver*
"Given a `browser-spec`, instantiate a new Driver record and assign to `*driver*`."
[browser-spec]
(let [new-driver (if (driver? browser-spec)
browser-spec
(core/new-driver browser-spec))]
(alter-var-root (var *driver*)
(constantly new-driver)
(when (thread-bound? (var *driver*))
(set! *driver* new-driver)))))
(declare to)
(defn set-driver!
"Set a default `Driver` for this thread, optionally sending it to a starting `url`.
Available browsers are `:firefox`, `:chrome`, `:ie`, `:opera`, and `:htmlunit`.
Examples:
=========
;;
;; Simple example
;;
(set-driver! {:browser :firefox})
;;
;; Full example
;;
(set-driver! {:browser :firefox
:cache-spec {:strategy :basic,
:args [{}],
:include [ (fn [element] (= (attribute element :class) \"external\"))
{:css \"ol#pages\"}]}
;;
;; Use existing Driver record
;;
(set-driver! a-driver)"
([browser-spec] (set-driver* browser-spec))
([browser-spec url] (to (set-driver* browser-spec) url)))
(defn set-finder!
"Set a default finder function, which will be used with all `q` parameters in functions that require an Element.
Examples:
=========
;;
;; Simple example
;;
(set-finder! xpath-finder)
;;
;; Derivative finder function
;;
;; Takes the query string and always prepends \"div#container \", which would be
;; useful in situations where you know you're always inside that particular div.
;; (Note that this same functionality is provided by `find-element-under`, but
;; you get the idea.)
;;
(set-finder! (fn [q]
(if (element-like? q)
q
(css-finder (str \"div#container \" q)))))
;;
;; Custom finder function
;;
;; If you want to easily switch between using CSS and XPath (e.g., because
;; XPath has the text() function for which no CSS equivalent exists), then
;; you could write something like this, where `q` would become either the map
;; {:css \"query\"} or {:xpath \"query\"} instead of just a string.
;;
(set-finder! (fn [q]
(if (element-like? q)
q
(case (first (keys q))
:css (core/find-elements-by *driver* (by-css (first (values q))))
:xpath (core/find-elements-by *driver* (by-xpath (first (values q))))))))
;;
;; (Note: This last example is written to show how to use the lowest-level functions
;; `find-elements-by`, `by-css` and `by-xpath`. The maps `{:css \"query\"}` and
;; `{:xpath \"query\"}` are themselves acceptable arguments to the `find-elements`,
;; function, so that function could have been used instead without the `case` statement.)
;;"
[finder-fn]
(alter-var-root (var *finder-fn*)
(constantly finder-fn)
(when (thread-bound? (var *finder-fn*))
(set! *finder-fn* finder-fn))))
(declare quit)
(defmacro with-driver
"Given a `browser-spec` to start a browser, execute the forms in `body`, then call `quit` on the browser. Uses the default finder function.
Examples:
=========
;;
;; Log into Github
;;
(with-driver {:browser :firefox}
(to \"https://github.com\")
(click \"a[href*='login']\")
(input-text \"#login_field\" \"your_username\")
(-> \"#password\"
(input-text \"your_password\")
submit))"
[browser-spec & body]
`(binding [*driver* (core/new-driver ~browser-spec)]
(try
~@body
(finally
(quit)))))
(defmacro with-driver-fn
"Given a `browser-spec` to start a browser and a `finder-fn` to use as a finding function, execute the forms in `body`, then call `quit` on the browser.
Examples:
=========
;;
;; Log into Github
;;
(with-driver {:browser :firefox} xpath-finder
(to \"https://github.com\")
(click \"//a[text()='Login']\")
(input-text \"//input[@id='login_field']\" \"your_username\")
(-> \"//input[@id='password']\"
(input-text \"your_password\")
submit))"
[browser-spec finder-fn & body]
`(binding [*driver* (core/new-driver ~browser-spec)
*finder-fn* ~finder-fn]
(try
~@body
(finally
(quit)))))
(defn css-finder
"Given a CSS query `q`, return a lazy seq of the elements found by calling `find-elements` with `by-css`. If `q` is an `Element`, it is returned unchanged.
This function is used internally by the Taxi API as `*finder*`. See the documentation for `set-finder!` for examples of extending this function or creating your own custom finder function."
([q] (css-finder *driver* q))
([driver q]
(cond
(element-like? q) q
(map? q) (core/find-elements driver q)
:else (core/find-elements driver {:css q}))))
(set-finder! css-finder)
(defn xpath-finder
"Given a XPath query `q`, return a lazy seq of the elements found by calling `find-elements` with `by-xpath`. If `q` is an `Element`, it is returned unchanged.
This function is used internally by the Taxi API as `*finder*`. See the documentation for `set-finder!` for examples of extending this function or creating your own custom finder function."
([q] (xpath-finder *driver* q))
([driver q]
(cond
(element-like? q) q
(map? q) (core/find-elements driver q)
:else (core/find-elements driver {:xpath q}))))
;; Be able to get actual element/elements when needed
(defn element
"Given a query `q`, return the first element that the default finder function returns.
Examples:
=========
;;
;; Simple Example
;;
;; Create a var that points to an element for later use.
;;
(def login-link (element \"a[href*='login']\"))
;;
;; More useful example: composing actions on an element
;;
;; When threading actions together, it's more performant to thread an actual element,
;; than to thread simply the query string. Threading the query string makes clj-webdriver
;; locate the same element multiple times, while threading an actual element only
;; requires one lookup.
;;
(-> (element \"input#password\")
(input-text \"my-password\")
submit)"
([q] (element *driver* q))
([driver q]
(if (element-like? q)
q
(first (*finder-fn* driver q)))))
(defn elements
"Given a query `q`, return the elements that the default finder function returns.
Examples:
=========
;;
;; Simple Example
;;
;; Save a seq of anchor tags (links) for later.
;;
(def target-elements (elements \"a\"))"
([q] (elements *driver* q))
([driver q]
(if (element-like? q)
q
(*finder-fn* driver q))))
;; ## Driver functions ##
(defn to
"Navigate the browser to `url`.
Examples:
=========
;;
;; Simple Example
;;
(to \"https://github.com\")
;;
;; Custom function for building URL's from a base url
;;
(defn go
[path]
(let [base-url \"http://example.com/\"]
(to (str base-url path))))
;; (go \"test-page\") would navigate to \"http://example.com/test-page\""
([url]
(to *driver* url))
([driver url]
(core/to driver url)))
(defn back
"Navigate back in the browser history, optionally `n` times.
Examples:
=========
;;
;; Simple Example
;;
(back)
;;
;; Specify number of times to go back
;;
(back 2)"
([] (back *driver* 1))
([driver-or-n] (if (driver? driver-or-n)
(back driver-or-n 1)
(back *driver* driver-or-n)))
([driver n]
(dotimes [m n]
(core/back driver))
driver))
(defn close
"Close the browser. If multiple windows are open, this only closes the active window.
Examples:
=========
(close)"
([] (close *driver*))
([driver]
(core/close driver)))
(defn current-url
"Return the current url of the browser.
Examples:
=========
(current-url)"
([] (current-url *driver*))
([driver] (core/current-url driver)))
(defn forward
"Navigate forward in the browser history.
Examples:
=========
;;
;; Simple Example
;;
(forward)
;;
;; Specify number of times to go back
;;
(forward 2)"
([] (forward *driver* 1))
([driver-or-n] (if (driver? driver-or-n)
(forward driver-or-n 1)
(forward *driver* driver-or-n)))
([driver n]
(dotimes [m n]
(core/forward driver))
driver))
(defn get-url
"Navigate the browser to `url`.
Examples:
=========
;;
;; Simple Example
;;
(get-url \"https://github.com\")
;;
;; Custom function for building URL's from a base url
;;
(defn go
[path]
(let [base-url \"http://example.com/\"]
(get-url (str base-url path))))
;; (go \"test-page\") would navigate to \"http://example.com/test-page\""
([url] (get-url *driver* url))
([driver url]
(core/get-url driver url)))
(defn take-screenshot
"Take a screenshot of the browser's current page, optionally specifying the format (`:file`, `:base64`, or `:bytes`) and the `destination` (something that `clojure.java.io/file` will accept).
Examples:
=========
;;
;; Simple Example
;;
;; Return screenshot as file object
;;
(take-screenshot :file)
;;
;; Specify a default destination for the file object
;;
(take-screenshot :file \"/path/to/save/screenshot.png\")"
([] (core/get-screenshot *driver* :file))
([format] (core/get-screenshot *driver* format))
([format destination] (core/get-screenshot *driver* format destination))
([driver format destination] (core/get-screenshot driver format destination)))
(defn page-source
"Return the source code of the current page in the browser.
Examples:
=========
;;
;; Simple Example
;;
(page-source)
;;
;; Do something with the HTML
;;
;; Selenium-WebDriver will instantiate a Java object for every element you query
;; or interact with, so if you have huge pages or need to do heavy-duty DOM
;; inspection or traversal, it could be more performant to do that \"offline\".
;;
(let [source (page-source)]
;; do hard-core parsing and manipulation here
)"
([] (page-source *driver*))
([driver]
(core/page-source driver)))
(defn quit
"Quit the browser completely, including all open windows.
Examples:
=========
(quit)"
([] (quit *driver*))
([driver]
(core/quit driver)))
(defn refresh
"Refresh the current page in the browser. Note that all references to elements will become \"stale\" and unusable after a page refresh.
Examples:
=========
(refresh)"
([] (refresh *driver*))
([driver]
(core/refresh driver)))
(defn title
"Return the title of the current page in the browser.
Examples:
=========
(title)"
([] (title *driver*))
([driver]
(core/title driver)))
(defn window
"Return a `Window` that contains information about the active window and can be used for switching or resizing.
Examples:
=========
(window)"
([] (window *driver*))
([driver]
(core/window driver)))
(defn windows
"Return `Window` records as a seq for all open windows.
Examples:
=========
(windows)"
([] (windows *driver*))
([driver]
(core/windows driver)))
(defn other-windows
"Return a `Window` for all open windows except the active one.
Examples:
=========
(other-windows)"
([] (other-windows *driver*))
([driver]
(core/other-windows driver)))
;; TODO: test coverage
(defn switch-to-frame
"Switch focus to the frame specified by `frame-q`, which is a standard Taxi element query or an integer for the order (zero-based index) of the frame on the page.
Examples:
=========
(switch-to-frame \"#target-frame\")
(switch-to-frame 1)"
([frame-q] (switch-to-frame *driver* frame-q))
([driver frame-q]
(let [frame (if (number? frame-q)
frame-q
(element frame-q))]
(core/switch-to-frame driver frame))))
;; TODO: accept a `Window` record or an attr-val that would get the window
(defn switch-to-window
"Switch focus to the window for the given one of the following:
* A string representing the target window name (as seen in the application titlebar)
* A number representing the index (order) of the target window
* A `Window` record
Examples:
=========
;;
;; By name
;;
(switch-to-window \"Name Of Window\")
;;
;; By index (order), open the 3rd window
;;
(switch-to-window 2)
;;
;; Passing a `Window` record directly (as returned by the `window` function)
;;
(switch-to-window a-window-record)"
([window] (switch-to-window *driver* window))
([driver window]
(core/switch-to-window driver window)))
(defn switch-to-other-window
"If two windows are open, switch focus to the other.
Examples:
=========
(switch-to-other-window)"
([] (switch-to-other-window *driver*))
([driver]
(core/switch-to-other-window driver)))
(defn switch-to-default
"Switch focus to the first first frame of the page, or the main document if the page contains iframes.
Examples:
=========
(switch-to-default)"
([] (switch-to-default *driver*))
([driver]
(core/switch-to-default driver)))
(defn switch-to-active
"Switch to the page element that currently has focus, or to the body if this cannot be detected.
Examples:
=========
(switch-to-active)"
([] (switch-to-active *driver*))
([driver]
(core/switch-to-active driver)))
(defn add-cookie
"Add a cookie to the browser session. The `cookie-spec` is a map which must contain `:name` and `:value` keys, and can also optionally include `:domain`, `:path`, `:expiry`, and `:secure?` (a boolean).
Examples:
=========
;;
;; Simple example
;;
(add-cookie {:name \"foo\", :value \"bar\"})
;;
;; Full example
;;
(add-cookie {:name \"foo\", :value \"bar\",
:domain \"example.com\", :path \"a-path\",
:expiry (java.util.Date.), :secure? false}) "
([cookie-spec] (add-cookie *driver* cookie-spec))
([driver cookie-spec]
(options/add-cookie driver cookie-spec)))
(defn delete-cookie
"Provided the name of a cookie or a Cookie record itself, delete it from the browser session.
Examples:
=========
;;
;; By name
;;
(delete-cookie \"foo\")
;;
;; With `Cookie` record as returned by `cookies` or `cookie` functions
;;
(delete-cookie a-cookie)"
([name-or-obj] (delete-cookie *driver* name-or-obj))
([driver name-or-obj]
(if (string? name-or-obj)
(options/delete-cookie-named driver name-or-obj)
(options/delete-cookie driver name-or-obj))))
(defn delete-all-cookies
"Delete all cookies from the browser session.
Examples:
=========
(delete-all-cookies)"
([] (delete-all-cookies *driver*))
([driver]
(options/delete-all-cookies driver)))
(defn cookies
"Return a seq of all cookies in the browser session. Items are `Cookie` records, which themselves contain a `:cookie` field with the original Java objects.
Examples:
=========
(cookies)"
([] (cookies *driver*))
([driver]
(options/cookies driver)))
(defn cookie
"Return the cookie with name `cookie-name`. Returns a `Cookie` record which contains a `:cookie` field with the original Java object.
Examples:
=========
(cookie \"foo\")"
([cookie-name] (cookie *driver* cookie-name))
([driver cookie-name]
(options/cookie-named driver cookie-name)))
(defn execute-script
"Execute the JavaScript code `js` with arguments `js-args` which must be passed in as a vector (for arity reasons).
Within the script, use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.
If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:
* For an HTML element, this method returns a WebElement
* For a decimal, a Double is returned
* For a non-decimal number, a Long is returned
* For a boolean, a Boolean is returned
* For all other cases, a String is returned.
* For an array, return a List<Object> with each object following the rules above. We support nested lists.
* Unless the value is null or there is no return value, in which null is returned.
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the 'arguments' magic variable, as if the function were called via 'Function.apply'
See http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#executeScript(java.lang.String, java.lang.Object...) for full details.
Examples:
=========
;;
;; Set a global variable
;;
(execute-script \"window.document.title = 'asdf'\")
;;
;; Return an element. Note that this currently returns a raw WebElement Java object.
;;
(execute-script \"var myElement = document.getElementById('elementId'); return myElement;\")"
([js] (execute-script *driver* js))
([driver-or-js js-or-args] (if (driver? driver-or-js)
(execute-script driver-or-js js-or-args [])
(execute-script *driver* driver-or-js js-or-args)))
([driver js js-args]
(apply (partial core/execute-script driver js) js-args)))
(defn wait-until
"Make the browser wait until the predicate `pred` returns true, providing an optional `timeout` in milliseconds and an optional `interval` in milliseconds on which to attempt the predicate. If the timeout is exceeded, an exception is thrown.
The predicate is a function that accepts the browser `Driver` record as its single parameter, and should return a truthy/falsey value.
Examples:
=========
;;
;; Simple example (taken from unit tests)
;;
;; Wait until the title of the page is 'asdf'
;;
(execute-script \"setTimeout(function () { window.document.title = 'asdf'}, 3000)\")
(wait-until #(= (title) \"asdf\"))
;;
;; Wait until an element exists
;;
(... code to load page ...)
(wait-until #(exists? \"#foo\"))
(click \"#foo a.bar\")"
([pred] (wait/wait-until *driver* (fn [_] pred)))
([pred timeout] (wait/wait-until *driver* (fn [_] pred) timeout))
([pred timeout interval] (wait/wait-until *driver* (fn [_] pred) timeout interval))
([driver pred timeout interval] (wait/wait-until driver (fn [d] (pred d)) timeout interval)))
(defn implicit-wait
"Set the global `timeout` that the browser should wait when attempting to find elements on the page, before timing out with an exception.
Examples:
=========
;;
;; Simple example (taken from unit tests)
;;
;; Set implicit timeout (global) to 3 seconds, then execute JavaScript with a
;; noticeable delay to prove that it works
;;
(implicit-wait 3000)
(execute-script \"setTimeout(function () { window.document.body.innerHTML = '<div id='test'>hi!</div>'}, 1000)\")"
([timeout] (implicit-wait *driver* timeout))
([driver timeout]
(wait/implicit-wait driver timeout)))
(defn find-windows
"Return all `Window` records that match the given `attr-val` map.
Attributes can be anything in a `Window` record (`:title` or `:url`) or you can pass an `:index` key and a number value to select a window by its open order.
Examples:
=========
;;
;; By name
;;
(find-windows {:title \"Window Title\"})
;;
;; By URL
;;
(find-windows {:url \"http://example.com/test-page\"})
;;
;; By index
;;
(find-windows {:index 2})"
([attr-val] (find-windows *driver* attr-val))
([driver attr-val]
(core/find-windows driver attr-val)))
(defn find-window
"Return the first `Window` record that matches the given `attr-val` map.
Attributes can be anything in a `Window` record (`:title` or `:url`) or you can pass an `:index` key and a number value to select a window by its open order.
Examples:
=========
;;
;; By name
;;
(find-window {:title \"Window Title\"})
;;
;; By URL
;;
(find-window {:url \"http://example.com/test-page\"})
;;
;; By index
;;
(find-window {:index 2})"
([attr-val] (find-window *driver* attr-val))
([driver attr-val] (core/find-window driver attr-val)))
(defn find-table-cell
"Within the table found with query `table-q`, return the table cell at coordinates `coords`. The top-left cell has coordinates `[0 0]`.
Examples:
=========
;;
;; Simple example, find 2nd cell on 2nd row from top
;;
(find-table-cell \"table#my-table\" [1 1])"
([table-q coords] (find-table-cell *driver* table-q coords))
([driver table-q coords]
(core/find-table-cell driver (element table-q) coords)))
(defn find-table-row
"Within the table found with query `table-q`, return a seq of all cells at row number `row`. The top-most row is row `0` (zero-based index).
Examples:
=========
;;
;; Simple example, return cells in second row
;;
(find-table-row \"table#my-table\" 1)"
([table-q row] (find-table-row *driver* table-q row))
([driver table-q row]
(core/find-table-row driver (element table-q) row)))
;; Need to explain difference between element and find-element fn's
(defn find-elements
"Return `Element` records that match the given `attr-val`. Prefer the default behavior of `elements` when possible.
Whereas the `elements` function uses a query `q` with the default finder function, this function requires an `attr-val` parameter which is either a map or a vector of maps with special semantics for finding elements on the page.
The `attr-val` map can consist of one or more of the following:
* The key `:css` or `:xpath` and a query value (e.g., `{:css \"a.external\"}`)
* The key `:tag` and an HTML tag (e.g., `{:tag :a}`)
* An HTML element attribute and its value (e.g., `{:class \"external\"}`)
* A 'meta' tag `:button*`, `:radio`, `:checkbox`, `:textfield`, `:password`, `:filefield` (e.g., `{:tag :button*}`)
* The key `:index` and the zero-based index (order) of the target element on the page (e.g., `{:index 2}` retrieves the third element that matches)
* A vector of attr-val maps like the above, representing a hierarchical query (auto-generates XPath)
Examples:
=========
;;
;; Medley of possibilities
;;
(find-elements {:css \"a.foo\"})
(find-elements {:xpath \"//a[@class='foo']\"})
(find-elements {:tag :a, :text \"Login\"})
(find-elements {:tag :a, :index 4}) ;; 5th anchor tag
(find-elements {:tag :button*, :class \"foo\"})
(find-elements {:tag :radio, :class \"choice\"})
(find-elements [{:tag :div, :id \"container\"},
{:tag :a, :class \"external\"}])
"
([attr-val] (find-elements *driver* attr-val))
([driver attr-val]
(core/find-elements driver attr-val)))
(defn find-element
"Return first `Element` record that matches the given `attr-val`. Prefer the default behavior of `element` when possible.
Whereas the `element` function uses a query `q` with the default finder function, this function requires an `attr-val` parameter which is either a map or a vector of maps with special semantics for finding elements on the page.
The `attr-val` map can consist of one or more of the following:
* The key `:css` or `:xpath` and a query value (e.g., `{:css \"a.external\"}`)
* The key `:tag` and an HTML tag (e.g., `{:tag :a}`)
* An HTML element attribute and its value (e.g., `{:class \"external\"}`)
* A 'meta' tag `:button*`, `:radio`, `:checkbox`, `:textfield`, `:password`, `:filefield` (e.g., `{:tag :button*}`)
* The key `:index` and the zero-based index (order) of the target element on the page (e.g., `{:index 2}` retrieves the third element that matches)
* A vector of attr-val maps like the above, representing a hierarchical query (auto-generates XPath)
Examples:
=========
;;
;; Medley of possibilities
;;
(find-element {:css \"a.foo\"})
(find-element {:xpath \"//a[@class='foo']\"})
(find-element {:tag :a, :text \"Login\"})
(find-element {:tag :a, :index 4}) ;; 5th anchor tag
(find-element {:tag :button*, :class \"foo\"})
(find-element {:tag :radio, :class \"choice\"})
(find-element [{:tag :div, :id \"container\"},
{:tag :a, :class \"external\"}])
"
([attr-val] (find-element *driver* attr-val))
([driver attr-val]
(core/find-element driver attr-val)))
;; Element versions of find-element-by and find-elements-by
(defn find-elements-under
"Find the elements that are children of the element found with query `q-parent`, using the given `attr-val`. If `q-parent` is an `Element`, it will be used as-is. The `attr-val` can either be a find-element-style map of attributes and values, or a by-clause (`by-tag`, `by-class`, etc.)
Examples:
=========
;;
;; Example using a map
;;
(find-elements-under \"div#container\" {:tag :a, :id \"foo\"})
;;
;; Example using by-clause, find an element with id \"foo\" within a div with id \"container\"
;;
(find-elements-under \"div#container\" (core/by-id \"foo\")"
([q-parent attr-val] (find-elements-under *driver* q-parent attr-val))
([driver q-parent attr-val]
(if (element-like? q-parent)
(core/find-elements q-parent attr-val)
(core/find-elements (element driver q-parent) attr-val))))
(defn find-element-under
"Find the first element that is a child of the element found with query `q-parent`, using the given `attr-val`. If `q-parent` is an `Element`, it will be used as-is. The `attr-val` can either be a find-element-style map of attributes and values, or a by-clause (`by-tag`, `by-class`, etc.)
Examples:
=========
;;
;; Example using map, which generates a (by-xpath ...) form
;;
(find-element-under \"div#container\" {:tag :a, :id \"foo\"})
;;
;; Example using by-clause, find an element with id \"foo\" within a div with id \"container\"
;;
(find-element-under \"div#container\" (core/by-id \"foo\")"
([q-parent attr-val] (find-element-under *driver* q-parent attr-val))
([driver q-parent attr-val]
(if (element-like? q-parent)
(core/find-element q-parent attr-val)
(core/find-element (element driver q-parent) attr-val))))
;; ## Element functions ##
;;
;; Unlike their counterparts in core, you don't need to do a `(find-element ...)`
;; with these; just pass in a CSS query followed by other necessary parameters
;; and the first element that matches the query will be used automatically.
;;
;; If a CSS query string is not passed in, it's assumed you're trying to use these
;; functions like their core counterparts, in which case each function will default
;; back to core functionality (expecting that you're passing in an Element record)
(defn attribute
"For the first element found with query `q`, return the value of the given `attribute`.
Examples:
=========
;;
;; Example medley for an anchor tag with id \"foo\", class \"bar\", and target \"_blank\"
;;
(attribute \"a#foo\" :id) ;=> \"foo\"
(attribute \"a#foo\" :class) ;=> \"bar\"
(attribute \"a#foo\" :target) ;=> \"_blank\""
([q attr] (attribute *driver* q attr))
([driver q attr]
(core/attribute (element driver q) attr)))
(defn click
"Click the first element found with query `q`.
Examples:
=========
(click \"a#foo\")"
([q] (click *driver* q))
([driver q]
(core/click (element driver q))))
(defn displayed?
"Return true if the first element found with query `q` is visible on the page.
Examples:
=========
(displayed? \"div#container\") ;=> true
(displayed? \"a.hidden\") ;=> false"
([q] (displayed? *driver* q))
([driver q]
(core/displayed? (element driver q))))
(defn drag-and-drop
"Drag the first element found with query `qa` onto the first element found with query `qb`.
Examples:
=========
;;
;; Drag div with id \"draggable\" onto div with id \"droppable\"
;;
(drag-and-drop \"#draggable\" \"#droppable\")"
([qa qb] (drag-and-drop *driver* qa qb))
([driver qa qb]
(core/drag-and-drop driver (element driver qa) (element driver qb))))
(defn drag-and-drop-by
"Drag the first element found with query `q` by `:x` pixels to the right and `:y` pixels down, passed in as a map like `{:x 10, :y 10}`. Values default to zero if excluded. Use negative numbers for `:x` and `:y` to move left or up respectively.
Examples:
=========
;;
;; Drag a div with id \"draggable\" 20 pixels to the right
;;
(drag-and-drop-by \"#draggable\" {:x 20})
;;
;; Drag a div with id \"draggable\" 10 pixels down
;;
(drag-and-drop-by \"#draggable\" {:y 10})
;;
;; Drag a div with id \"draggable\" 15 pixels to the left and 5 pixels up
;;
(drag-and-drop-by \"#draggable\" {:x -15, :y -5})"
([q x-y-map] (drag-and-drop-by *driver* q x-y-map))
([driver q x-y-map]
(core/drag-and-drop-by driver (element driver q) x-y-map)))
(defn exists?
"Return true if the first element found with query `q` exists on the current page in the browser.
Examples:
=========
(exists? \"a#foo\") ;=> true
(exists? \"footer\") ;=> false"
([q] (exists? *driver* q))
([driver q]
(core/exists? (element driver q))))
(defn flash
"Flash the background color of the first element found with query `q`.
Examples:
=========
(flash \"a.hard-to-see\")"