-
Notifications
You must be signed in to change notification settings - Fork 1
/
webdriver.tcl
executable file
·1135 lines (904 loc) · 36.6 KB
/
webdriver.tcl
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
package require selenium::utils::port
package require selenium::utils::json
package require selenium::utils::base64
namespace eval ::selenium {
namespace export WebDriver
set JAVASCRIPT_RETURNS_ELEMENT 0
set JAVASCRIPT_RETURNS_ELEMENTS 1
oo::class create WebDriver {
# Controls a browser by sending commands to a remote server.
# This server is expected to be running the WebDriver wire protocol as define
# here: http://code.google.com/p/selenium/wiki/JsonWireProtocol
# :Attributes:
# - remote_connection - The CommandExecutor object used to execute commands.
# - error_handler - errorhandler.ErrorHandler object used to verify that the server did not return an error.
# - session_ID - The session ID to send with every $Command::
# - capabilities - A dictionary of capabilities of the underlying browser for this instance's session.
mixin ::selenium::Mixin_For_Element_Retrieval ::selenium::Mixin_For_Scrolling ::selenium::Mixin_For_Mouse_Interaction ::selenium::Mixin_Action_Chains
variable driver remote_connection error_handler current_capabilities session_ID Command Mouse_Button Exception StatusCache By w3c_compliant JAVASCRIPT_RETURNS_ELEMENT JAVASCRIPT_RETURNS_ELEMENTS
constructor {service_url desired_capabilities {required_capabilities {}}} {
# Create a new driver that will issue commands using the wire protocol.
#
# :Args:
# - service_url - The URL where the service is provided
# - desired_capabilities - Dictionary holding predefined values for starting a browser
# - required_capabilities (Optional) - A dictionary of required capabilities of the underlying browser for this instance's session.
set driver [self]
namespace import ::selenium::utils::json::compile_to_json \
::selenium::webelement::WebElement \
::selenium::container_of_webelements::Container_Of_WebElements
namespace eval [self] {
namespace upvar ::selenium Command Command Exception Exception Mouse_Button Mouse_Button StatusCache StatusCache By By
}
set remote_connection [::selenium::Remote_Connection new $service_url]
set session_ID ""
set current_capabilities [dict create]
set error_handler [::selenium::ErrorHandler new]
my start_client
my start_session $desired_capabilities $required_capabilities
}
forward wait_until_connectable ::selenium::utils::port::wait_until_connectable
forward wait_until_not_connectable ::selenium::utils::port::wait_until_not_connectable
method session_ID {} {
return $session_ID
}
method name {} {
# Returns the name of the underlying browser for this instance.
#
# :Usage:
# $driver name
if {[dict exists $current_capabilities browserName]} {
return [dict get $current_capabilities browserName]
} else {
throw $Exception(WebdriverException) {browserName not specified in session capabilities}
}
}
method start_client {} {
# Called before starting a new session. This method may be overridden to define custom startup behavior.
}
method stop_client {} {
# Called after executing a quit command. This method may be overridden to define custom shutdown behavior.
}
method start_session {desired_capabilities required_capabilities} {
# Creates a new session with the desired capabilities.
#
# :Args:
# - browser_name - The name of the browser to request.
# - version - Which browser version to request.
# - platform - Which platform to request the browser on.
# - javascript_enabled - Whether the new session should support JavaScript.
# - browser_profile - A selenium.driver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.
if {$required_capabilities eq ""} {
set json_response [my execute $Command(NEW_SESSION) desiredCapabilities $desired_capabilities]
} else {
set json_response [my execute $Command(NEW_SESSION) desiredCapabilities $desired_capabilities requiredCapabilities $required_capabilities]
}
# Test if it's new JSON whichis wrapped in brace called "value" and extract if so.
if {![dict exists $json_response sessionId]} {
set json_response [dict get $json_response value]
}
set session_ID [dict get $json_response sessionId]
set current_capabilities [dict get $json_response capabilities]
# Quick check to see if we have a W3C Compliant browser.
# According to SauceLabs "If that line begins with 'desiredCapabilities', you are running the non-W3C version. If it begins with 'capabilities', you are running the new W3C-compliant version."
# Refactor to proper test https://wiki.saucelabs.com/display/DOCS/Selenium+W3C+Capabilities+Support
if {[dict exists $json_response value desiredCapabilities]} {
set w3c_compliant 0
} else {
set w3c_compliant 1
}
}
method execute {command_name args} {
# Sends a command to be executed by a command executor.
#
# :Args:
# - command_name: The name of the command to execute as a string.
# - parameters_var: Variable name containing a dict object of named parameters to send with the $Command::
#
# :Returns:
# The command's JSON response loaded into a dict object.
set response [$remote_connection dispatch $session_ID $command_name $args]
set json_response [$error_handler check_response $session_ID $command_name $args $response]
return $json_response
}
method execute_and_get_value {command_name args} {
if {[llength $args] != 0} {
set json_response [my execute $command_name {*}$args]
} else {
set json_response [my execute $command_name]
}
return [dict get $json_response value]
}
method get {url} {
# Loads a web page in the current browser session.
my execute $Command(GET) url $url
}
method title {} {
# Returns the title of the current page.
#
# :Usage:
# driver title
return [my execute_and_get_value $Command(GET_TITLE)]
}
method execute_javascript {script args} {
# Executes JavaScript in the current window/frame synchronously or asynchronously.
#
# :Args:
# - script: The JavaScript to execute.
#
# :Options:
# -arguments spec1? arg1? spec2? arg2?...: Pairs of json specifications and tcl values as arguments for your JavaScript.
# -async: Execute javascript asyncronously
# -command_var: Variable to store the new command for the element/s
# -returns_element: Flag indicating that the script returns an element
# -returns_elements: Flag indicating that the script returns a list of elements
#
# :Usage:
# driver execute_javascript {return document.title}
set script_arguments [list]
set async 0
set level 1
set i 0
set options_len [llength $args]
while {$i < $options_len} {
set optionName [lindex $args $i]
switch $optionName {
-arguments {
incr i
if {$i == $options_len } {
return -code error "No value for -arguments"
}
foreach {argument_type argument_value} [lindex $args $i] {
lappend script_arguments [my _convert_to_script_argument $argument_type $argument_value]
}
}
-argument {
incr i
if {$i == $options_len} {
return -code error "No value for argument type"
}
set argument_type [lindex $args $i]
incr i
if {$i == $options_len} {
return -code error "No value for argument name"
}
set argument_value [lindex $args $i]
lappend script_arguments [my _convert_to_script_argument $argument_type $argument_value]
}
-async {
set async 1
}
-returns_element {
set return_value $JAVASCRIPT_RETURNS_ELEMENT
}
-returns_elements {
set return_value $JAVASCRIPT_RETURNS_ELEMENTS
}
-command_var {
incr i
if {$i == $options_len} {
return -code error "No value for -commnad_var"
}
set command_var [lindex $args $i]
}
-level {
incr i
if {$i == $options_len} {
return -code error "No value for -level"
}
set level [lindex $args $i]
}
}
incr i
}
if {[info exists command_var] && ![info exists return_value]} {
return -code error "option -command_var is only allowed when we expect element/s"
}
if {$async} {
set selenium_command $Command(EXECUTE_ASYNC_SCRIPT)
} else {
set selenium_command $Command(EXECUTE_SCRIPT)
}
set value [my execute_and_get_value $selenium_command script $script args $script_arguments]
if {[info exists return_value]} {
if {$return_value == $JAVASCRIPT_RETURNS_ELEMENT} {
set selenium_ID [lindex $value 1]
if {[info exists command_var]} {
return [my create_webelement $selenium_ID $command_var [expr $level+1]]
} else {
return $selenium_ID
}
} else {
set list_of_selenium_IDs [list]
foreach element $value {
set selenium_ID [lindex $element 1]
lappend list_of_selenium_IDs $selenium_ID
}
if {[info exists command_var]} {
return [my create_container_of_webelements $list_of_selenium_IDs $command_var [expr $level+1]]
} else {
return $list_of_selenium_IDs
}
}
} else {
return $value
}
}
forward execute_script my execute_javascript
method _convert_to_script_argument {argument_type argument_value} {
switch -exact $argument_type {
string -
number -
boolean -
bool {
return [compile_to_json $argument_type $argument_value]
}
element {
return [compile_to_json dict [dict create ELEMENT $argument_value]]
}
default {
throw throw $Exception(WebDriver) "Invalid type: $argument_type"
}
}
}
method remove_webelement_from_DOM {webelement} {
# It remove a webelement from DOM
#
# :Args:
# - webelement: The element ID of the webelement to remove
my execute_javascript {
var element = arguments[0];
element.parentNode.removeChild(element);
} -arguments [list element $webelement]
}
method get_base64_image {image_webelement} {
return [my execute_javascript {
var HTMLImageElement = arguments[0]
var canvas = document.createElement("canvas");
canvas.width = HTMLImageElement.width;
canvas.height = HTMLImageElement.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
} -arguments [list element $image_webelement]]
}
method active_webelement {{command_var ""} {level 1}} {
# Returns the element with focus, or BODY if nothing has focus.
#
# :Usage:
# set element [driver active_element]
set selenium_ID [lindex [$driver execute_and_get_value $Command(GET_ACTIVE_ELEMENT)] 1]
return $selenium_ID
}
method create_webelement {selenium_ID {command_var ""} {level 1}} {
# Creates a web element with the specified element_id.
if {$command_var ne ""} {
upvar $level $command_var element
set element [WebElement new $driver $selenium_ID]
trace add variable element unset [list ::apply [list args [list $element destroy]]]
} else {
upvar $level $::selenium::PACKAGE_SIGNATURE signature
set element [WebElement new $driver $selenium_ID]
trace add variable signature unset [list ::apply [list args [list $element destroy]]]
}
return $element
}
method create_container_of_webelements {list_of_selenium_IDs {command_var ""} {level 1}} {
if {$command_var ne ""} {
upvar $level $command_var container
set container [Container_Of_WebElements new $driver $list_of_selenium_IDs]
trace add variable container unset [list ::apply [list args [list $container destroy]]]
} else {
upvar $level $::selenium::PACKAGE_SIGNATURE signature
set container [Container_Of_WebElements new $driver $list_of_selenium_IDs]
trace add variable signature unset [list ::apply [list args [list $container destroy]]]
}
return $container
}
method is_select_element {element_ID} {
set tag_name_of_element [my tag_name $element_ID]
if {[string tolower $tag_name_of_element] eq "select"} {
return 1
} else {
return 0
}
}
method is_displayed {element_ID} {
# Whether the element would be visible to a user
return [my execute_and_get_value $Command(IS_ELEMENT_DISPLAYED) id $element_ID]
}
method tag_name {element_ID} {
# Gets this element's tagName property.
return [my execute_and_get_value $Command(GET_ELEMENT_TAG_NAME) id $element_ID]
}
method get_visible_text {element_ID} {
# Gets the visible text of the element.
return [my execute_and_get_value $Command(GET_ELEMENT_TEXT) id $element_ID]
}
method get_text {element_ID} {
# Gets the text of the element.
if {[dict get [my current_capabilities] browserName] eq "internet explorer"} {
if {$w3c_compliant} {
return [my execute_and_get_value $Command(GET_ELEMENT_PROPERTY) name innerText id $element_ID]
} else {
return [my execute_and_get_value $Command(GET_ELEMENT_ATTRIBUTE) name innerText id $element_ID]
}
} else {
if {$w3c_compliant} {
return [my execute_and_get_value $Command(GET_ELEMENT_PROPERTY) name textContent id $element_ID]
} else {
return [my execute_and_get_value $Command(GET_ELEMENT_ATTRIBUTE) name textContent id $element_ID]
}
}
}
method submit_form {element_ID} {
# Submits a form.
if {$w3c_compliant} {
set form [my find_element -xpath ./ancestor-or-self::form -root $element_ID]
my execute_script {
var e = arguments[0].ownerDocument.createEvent('Event');
e.initEvent('submit', true, true);
if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }} -arguments [list element $form]
} else {
my execute $Command(SUBMIT_ELEMENT) id $element_ID
}
}
method clear_text {element_ID} {
# Clears the text if it's a text entry element.
my execute $Command(CLEAR_ELEMENT) id $element_ID
}
method get_attribute {element_ID attribute_name} {
# Gets the given attribute or property of the element.
#
# This method will return the value of the given property if this is set,
# otherwise it returns the value of the attribute with the same name if
# that exists, or None.
#
# :Args:
# - name - Name of the attribute/property to retrieve.
#
# Example::
#
# # Check if the "active" CSS class is applied to an element.
# set class_attribute [$driver get_element_attribute $target_element class]
# set is_active [expr {[string first $class_attribute active]!=-1}]
return [my execute_and_get_value $Command(GET_ELEMENT_ATTRIBUTE) name $attribute_name id $element_ID]
}
method is_selected {element_ID} {
# Whether the element is selected.
#
# Can be used to check if a checkbox or radio button is selected.
return [my execute_and_get_value $Command(IS_ELEMENT_SELECTED) id $element_ID]
}
method is_enabled {element_ID} {
# Whether the element is enabled.
return [my execute_and_get_value $Command(IS_ELEMENT_ENABLED) id $element_ID]
}
method location_once_scrolled_into_view {element_ID} {
# CONSIDERED LIABLE TO CHANGE WITHOUT WARNING. Use this to discover where on the screen an
# element is so that we can click it. This method should cause the element to be scrolled
# into view.
#
# Returns the top lefthand corner location on the screen, or None if the element is not visible
return [my execute_and_get_value $Command(GET_ELEMENT_LOCATION_IN_VIEW) id $element_ID]
}
method size {element_ID} {
# Returns the size of the element
return [my execute_and_get_value $Command(GET_ELEMENT_SIZE) id $element_ID]
}
method css_property {element_ID property_name} {
# Returns the value of a CSS property
return [my execute_and_get_value $Command(GET_VALUE_OF_CSS_PROPERTY) propertyName $property_name id $element_ID]
}
method get_location {element_ID} {
# Returns the location of the element in the renderable canvas
return [my execute_and_get_value $Command(GET_ELEMENT_LOCATION) id $element_ID]
}
method get_rect {element_ID} {
# Returns a dictionary with the size and location of the element
return [my execute_and_get_value $Command(GET_ELEMENT_RECT) id $element_ID]
}
method send_keys {element_ID string_of_keys} {
# Simulates typing into the element.
#
# :Args:
# - value - A string for typing, or setting form fields. For setting
# file inputs, this could be a local file path.
#
# Use this to send simple key events or to fill out form fields::
#
# set form_textfield [$driver find_element -name username]
# $driver send_keys -el $form_textfield -string admin
# $driver send_keys admin $form_textfield
my execute $Command(SEND_KEYS_TO_ELEMENT) id $element_ID value [split $string_of_keys ""] text $string_of_keys
}
method typewrite {string_of_keys} {
my execute $Command(TYPEWRITE) value [split $string_of_keys ""]
}
method select_element {element_ID command_var} {
upvar $command_var select_element
set select_element [Select_Element [self] $element_ID]
trace add variable select_element unset "$select_element destroy"
}
method freeze {varname} {
# freeze element or container of webelements
uplevel $varname obj
trace remove variable obj unset [list [self namespace]::destroy_obj $obj]
}
method current_url {} {
# Gets the URL of the current page.
#
# :Usage:
# driver current_url
return [my execute_and_get_value $Command(GET_CURRENT_URL)]
}
method page_source {} {
# Gets the source of the current page.
#
# :Usage:
# driver page_source
return [my execute_and_get_value $Command(GET_PAGE_SOURCE)]
}
method close {} {
# Closes the current window.
#
# :Usage:
# driver close
my execute $Command(CLOSE)
}
method quit {} {
# Quits the driver and closes every associated window.
#
# :Usage:
# driver quit
try {
my execute $Command(QUIT) sessionId $session_ID
} finally {
my stop_client
}
}
method current_window_handle {} {
# Returns the handle of the current window.
#
# :Usage:
# driver current_window_handle
return [my execute_and_get_value $Command(GET_CURRENT_WINDOW_HANDLE)]
}
method window_handles {} {
# Returns the handles of all windows within the current session.
#
# :Usage:
# driver window_handles
return [my execute_and_get_value $Command(GET_WINDOW_HANDLESS)]
}
method maximize_window {} {
# Maximizes the current window that driver is using
if {$w3c_compliant} {
my execute $Command(W3C_MAXIMIZE_WINDOW)
} else {
my execute $Command(MAXIMIZE_WINDOW) windowHandle current
}
}
#Navigation
method back {} {
# Goes one step backward in the browser history.
#
# :Usage:
# driver back
my execute $Command(GO_BACK)
}
method forward {} {
# Goes one step forward in the browser history.
#
# :Usage:
# driver forward
my execute $Command(GO_FORWARD)
}
method refresh {} {
# Refreshes the current page.
my execute $Command(REFRESH)
}
# Options
method get_cookies {} {
# Returns a set of dictionaries, corresponding to cookies visible in the current session.
#
# :Usage:
# driver get_cookies
return [my execute_and_get_value $Command(GET_ALL_COOKIES)]
}
method get_cookie {name} {
# Get a single cookie by name. Returns the cookie if found, None if not.
#
# :Usage:
# driver get_cookie 'my_cookie'
set cookies [my get_cookies]
foreach cookie $cookies {
if {[dict get $cookie name] eq $name} { return $cookie }
}
return
}
method delete_cookie {name} {
# Deletes a single cookie with the given name.
my execute $Command(DELETE_COOKIE) name $name
}
method delete_all_cookies {} {
# Delete all cookies in the scope of the session.
my execute $Command(DELETE_ALL_COOKIES)
}
method add_cookie {args} {
# Adds a cookie to your current session.
#
# :Args:
# - cookie_dict: A dict object, with required keys - "name" and "value";
# optional keys - "path", "domain", "secure", "expiry"
#
# Key Type Description
# name string The name of the cookie.
# value string The cookie value.
# path string (Optional) The cookie path.1
# domain string (Optional) The domain the cookie is visible to.1
# secure boolean (Optional) Whether the cookie is a secure cookie.1
# httpOnly boolean (Optional) Whether the cookie is an httpOnly cookie.1
# expiry number (Optional) When the cookie expires, specified in seconds since midnight, January 1, 1970 UTC.1
#
# Usage:
# driver add_cookie name foo value bar
# driver add_cookie name foo value bar path /
# driver add_cookie name foo value bar path / secure true
set cookie [dict create]
for {parameter_name value} $args {
switch -exact $parameter_name {
-name {
dict set cookie name $value
}
-value {
dict set cookie value $value
}
-path {
dict set cookie path $value
}
-domain {
dict set cookie domain $value
}
-secure {
dict set cookie secure $value
}
-domain {
dict set cookie domain $value
}
-httpOnly {
dict set cookie httpOnly $value
}
-expiry {
dict set cookie expiry $value
}
default {
error "Invalid parameter for cookie: $parameter_name"
}
}
}
if {![dict exists $cookie name] || ![dict exists $cookie value]} {
error "'-name' and '-value' are mandatory cookie parameters"
}
my execute $Command(ADD_COOKIE) cookie $cookie
}
# Timeouts
method implicitly_wait {time_to_wait} {
# Sets a sticky timeout to implicitly wait for an element to be found,
# or a command to complete. This method only needs to be called one
# time per session. To set the timeout for calls to
# execute_async_script, see set_script_timeout.
#
# :Args:
# - time_to_wait: Amount of time to wait (in seconds)
#
# :Usage:
# driver implicitly_wait 30
if {$w3c_compliant} {
my execute $Command(SET_TIMEOUTS) ms [expr $time_to_wait * 1000] type implicit
} else {
my execute $Command(IMPLICIT_WAIT) ms [expr $time_to_wait * 1000]
}
}
method set_script_timeout {time_to_wait} {
# Set the amount of time that the script should wait during an
# execute_async_script call before throwing an error.
#
# :Args:
# - time_to_wait: The amount of time to wait (in seconds)
if {$w3c_compliant} {
my execute $Command(SET_TIMEOUTS) ms [expr $time_to_wait * 1000] type script
} else {
my execute $Command(SET_SCRIPT_TIMEOUT) ms [expr $time_to_wait * 1000]
}
}
method set_page_load_timeout {time_to_wait} {
# Set the amount of time to wait for a page load to complete
# before throwing an error.
#
# :Args:
# - time_to_wait: The amount of time to wait
#
# :Usage:
# driver set_page_load_timeout 30
my execute $Command(SET_TIMEOUTS) ms [expr $time_to_wait * 1000] type {page load}
}
method current_capabilities {} {
# returns the drivers current desired capabilities being used
return $current_capabilities
}
method get_screenshot_as_file {filename {element_ID ""}} {
# Gets the screenshot of the current window. Returns False if there is
# any IOError, else returns True. Use full paths in your filename.
#
# :Args:
# - filename: The full path you wish to save your screenshot to.
#
# :Usage:
# driver get_screenshot_as_file /Screenshots/foo.png
set png [my get_screenshot_as_png $element_ID]
if { [catch {open $filename wb} fileId] } {
return false
} else {
puts -nonewline $fileId $png
close $fileId
return true
}
}
forward save_screenshot get_screenshot_as_file
method get_screenshot_as_png {{element_ID ""}} {
# Gets the screenshot of the current window as a binary data.
return [::base64::decode [my get_screenshot_as_base64 $element_ID]]
}
method get_screenshot_as_base64 {{element_ID ""}} {
# Gets the screenshot of the current window as a base64 encoded string
# which is useful in embedded images in HTML.
#
# :Usage:
# driver get_screenshot_as_base64
if {$element_ID eq ""} {
return [my execute_and_get_value $Command(SCREENSHOT)]
} else {
return [my execute_and_get_value $Command(ELEMENT_SCREENSHOT) id $element_ID]
}
}
method set_window_size {width height {windowHandle current}} {
# Sets the width and height of the current window. (window.resizeTo)
#
# :Args:
# - width: the width in pixels to set the window to
# - height: the height in pixels to set the window to
#
# :Usage:
# driver set_window_size 800 600
my execute $Command(SET_WINDOW_SIZE) width $width height $height windowHandle $windowHandle
}
method get_window_size { {windowHandle current}} {
# Gets the width and height of the current window.
#
# :Usage:
# driver get_window_size
if {$w3c_compliant} {
return [my execute_and_get_value $Command(W3C_GET_WINDOW_SIZE)]
} else {
return [my execute_and_get_value $Command(GET_WINDOW_SIZE) windowHandle $windowHandle]
}
}
method set_window_position {x y {windowHandle current}} {
# Sets the x,y position of the current window. (window.moveTo)
#
# :Args:
# - x: the x-coordinate in pixels to set the window position
# - y: the y-coordinate in pixels to set the window position
#
# :Usage:
# driver set_window_position 0 0
my execute $Command(SET_WINDOW_POSITION) x $x y $y windowHandle $windowHandle
}
method get_window_position {windowHandle current} {
# Gets the x,y position of the current window.
return [my execute_and_get_value $Command(GET_WINDOW_POSITION) windowHandle $windowHandle]
}
method get_screen_orientation {} {
# Gets the current orientation of the device
return [my execute_and_get_value $Command(GET_SCREEN_ORIENTATION)]
}
method set_screen_orientation {value} {
# Sets the current orientation of the device
#
# :Args:
# - value: orientation to set it to.
#
# :Usage:
# driver set_orientation "landscape"
set value [string toupper value]
if { $value in [list LANDSCAPE PORTRAIT]} {
my execute $Command(SET_SCREEN_ORIENTATION) orientation $value
} else {
throw $Exception(WebdriverException) "You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'"
}
}
method available_log_types {} {
# Gets a list of the available log types
#
# :Usage:
# driver log_types
return [my execute_and_get_value $Command(GET_AVAILABLE_LOG_TYPES)]
}
method get_log {log_type} {
# Gets the log for a given log type
#
#:Args:
# - log_type: type of log that which will be returned
#
#:Usage:
# driver get_log browser
# driver get_log driver
# driver get_log client
# driver get_log server
return [my execute_and_get_value $Command(GET_LOG) type $log_type]
}
method alert_text {} {
# Gets the text of the Alert.
return [my execute_and_get_value $Command(GET_ALERT_TEXT)]
}
method dismiss_alert {} {
# Dismisses the alert available.
my execute $Command(DISMISS_ALERT))
}
method accept_alert {} {
# Accepts the alert available.
#
# Usage::
# driver accept_alert # Confirm a alert dialog.
my execute $Command(ACCEPT_ALERT))
}
method send_keys_to_alert {keysToSend} {
# Send Keys to the Alert.
#
# :Args:
# - keysToSend: The text to be sent to Alert.
my execute $Command(SET_ALERT_VALUE) text $keysToSend
}
method get_app_cache_status {} {
# Returns a current status of application cache.
set status [my execute_and_get_value $Command(GET_APP_CACHE_STATUS)]
return $StatusCache($status)
}
method switch_to_default_frame {} {
# Switch focus to the default frame.
#
# :Usage:
# driver switch_to_default_frame
my execute $Command(SWITCH_TO_FRAME) id null
}
method switch_to_frame {by frame_reference} {
# Switches focus to the specified frame, by index, name, or webelement.
#
# :Args:
# - frame_reference: The name of the window to switch to, an integer representing the index,
# or a webelement that is an (i)frame to switch to.
#
# :Usage:
# driver switch_to_frame -name $frame_name
# driver switch_to_frame -index 1
# driver switch_to_frame -element [[driver find_elements_by_tag_name iframe] index 0]
switch -exact -- $by {
-index {
set parameters [list id [compile_to_json number $frame_reference]]
}
-name {