-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwd_demo.au3
1328 lines (1041 loc) · 55.6 KB
/
wd_demo.au3
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
#Region - include files
; standard UDF's
#include <ButtonConstants.au3>
#include <ColorConstants.au3>
#include <Date.au3>
#include <Debug.au3>
#include <GuiComboBoxEx.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; non standard UDF's
#include "wd_helper.au3"
#include "wd_capabilities.au3"
#EndRegion - include files
#Tidy_Parameters=/tcb=-1
#Region - Global's declarations
Global Const $sElementSelector = "//input[@name='q']"
Global Const $aBrowsers[][2] = _
[ _
["Firefox", SetupGecko], _
["Chrome", SetupChrome], _
["Chrome_Legacy", SetupChrome], _
["MSEdge", SetupEdge], _
["Opera", SetupOpera], _
["MSEdgeIE", SetupEdgeIEMode] _
]
; Column 0 - Function Name
; Column 1 - Selected at start or selected manually by user
; Column 2 - Pass browser name as parameter to called function
Global $aDemoSuite[][3] = _
[ _
["DemoTimeouts", False, False], _
["DemoNavigation", True, False], _
["DemoElements", False, False], _
["DemoScript", False, False], _
["DemoCookies", False, False], _
["DemoAlerts", False, False], _
["DemoFrames", False, False], _
["DemoActions", False, False], _
["DemoDownload", False, False], _
["DemoWindows", False, False], _
["DemoUpload", False, False], _
["DemoPrint", False, True], _
["DemoSleep", False, False], _
["DemoSelectOptions", False, False], _
["DemoStyles", False, False], _
["UserTesting", False, False], _
["UserFile", False, False] _
]
Global Const $aDebugLevel[][2] = _
[ _
["None", $_WD_DEBUG_None], _
["Error", $_WD_DEBUG_Error], _
["Info", $_WD_DEBUG_Info], _
["Full", $_WD_DEBUG_Full] _
]
Global $sSession
Global $__g_idButton_Abort
Global $_VAR[50] ; used in UserFile(), __SetVAR()
#EndRegion - Global's declarations
_WD_Demo()
Exit
Func _WD_Demo()
Local $nMsg
Local $iSpacing = 25
Local $iPos
Local $iCount = UBound($aDemoSuite)
Local $aButtons[$iCount]
Local $hGUI = GUICreate("Webdriver Demo", 200, 100, 100, 200, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX))
GUISetBkColor($CLR_SILVER)
#Region - browsers
$iPos += $iSpacing
GUICtrlCreateLabel("Browser", 15, $iPos + 2)
Local $idBrowsers = GUICtrlCreateCombo("", 75, $iPos, 100, 20, $CBS_DROPDOWNLIST)
Local $sData = _ArrayToString($aBrowsers, Default, Default, Default, "|", 0, 0)
GUICtrlSetData($idBrowsers, $sData)
GUICtrlSetData($idBrowsers, $aBrowsers[0][0])
#EndRegion - browsers
#Region - debug
$iPos += $iSpacing
GUICtrlCreateLabel("Debug", 15, $iPos + 2)
Local $idDebugging = GUICtrlCreateCombo("", 75, $iPos, 100, 20, $CBS_DROPDOWNLIST)
$sData = _ArrayToString($aDebugLevel, Default, Default, Default, "|", 0, 0)
GUICtrlSetData($idDebugging, $sData)
GUICtrlSetData($idDebugging, "Info")
#EndRegion - debug
#Region - update
$iPos += $iSpacing
GUICtrlCreateLabel("Update", 15, $iPos + 2)
Local $idUpdate = GUICtrlCreateCombo("Report only", 75, $iPos, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData($idUpdate, "Current|32bit|32bit+Force|64Bit|64Bit+Force", "Report only")
#EndRegion - update
#Region - Headless
$iPos += $iSpacing
GUICtrlCreateLabel("Headless", 15, $iPos + 2)
Local $idHeadless = GUICtrlCreateCombo("No", 75, $iPos, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData($idHeadless, "Yes", "No")
#EndRegion - Headless
#Region - Output
$iPos += $iSpacing
GUICtrlCreateLabel("ConsoleOut", 15, $iPos + 2)
Local $idOutput = GUICtrlCreateCombo("ConsoleWrite", 75, $iPos, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData($idOutput, "WD_Console.log|_DebugOut|Null", "ConsoleWrite")
#EndRegion - Output
#Region - demos
$iPos += $iSpacing
GUICtrlCreateLabel("Demos", 15, $iPos + $iSpacing + 2)
For $i = 0 To $iCount - 1
$iPos += $iSpacing
$aButtons[$i] = GUICtrlCreateButton($aDemoSuite[$i][0], 75, $iPos, 100, 20)
If $aDemoSuite[$i][1] Then GUICtrlSetBkColor($aButtons[$i], $COLOR_AQUA)
Next
#EndRegion - demos
#Region - run / abort
$iPos += $iSpacing * 2
Local $idButton_Run = GUICtrlCreateButton("Run Demo!", 10, $iPos, 85, 25)
$__g_idButton_Abort = GUICtrlCreateButton("Abort", 100, $iPos, 85, 25)
GUICtrlSetState($__g_idButton_Abort, $GUI_DISABLE)
#EndRegion - run / abort
; Resize window
WinMove($hGUI, "", 100, 200, 200, $iPos + 3 * $iSpacing)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_NONE
; do nothing
Case $GUI_EVENT_CLOSE
ExitLoop
Case $idBrowsers
Case $idDebugging
Case $idButton_Run
_RunDemo_GUISwitcher($GUI_DISABLE, $idBrowsers, $idDebugging, $idUpdate, $idHeadless, $idOutput, $idButton_Run, $aButtons)
RunDemo($idDebugging, $idBrowsers, $idUpdate, $idHeadless, $idOutput)
_RunDemo_GUISwitcher($GUI_ENABLE, $idBrowsers, $idDebugging, $idUpdate, $idHeadless, $idOutput, $idButton_Run, $aButtons)
Case Else
For $i = 0 To $iCount - 1
If $aButtons[$i] = $nMsg Then
$aDemoSuite[$i][1] = Not $aDemoSuite[$i][1]
GUICtrlSetBkColor($aButtons[$i], $aDemoSuite[$i][1] ? $COLOR_AQUA : $COLOR_WHITE)
_ArraySearch($aDemoSuite, True, Default, Default, Default, Default, Default, 1)
GUICtrlSetState($idButton_Run, @error ? $GUI_DISABLE : $GUI_ENABLE)
EndIf
Next
EndSwitch
WEnd
GUIDelete($hGUI)
EndFunc ;==>_WD_Demo
Func RunDemo($idDebugging, $idBrowsers, $idUpdate, $idHeadless, $idOutput)
; Check selected debugging option and set desired debug level
$_WD_DEBUG = $aDebugLevel[_GUICtrlComboBox_GetCurSel($idDebugging)][1]
; Get selected browser
Local $sBrowserName = $aBrowsers[_GUICtrlComboBox_GetCurSel($idBrowsers)][0]
; Check and set desired output for __WD_ConsoleWrite()
_RunDemo_Output($idOutput)
; This following 2 options setting is for "compability" with au3WebDriver UDF "0.7.0", because in later versions this feature was disabled by default
_WD_Option("errormsgbox", (@Compiled = 1))
_WD_Option("OutputDebug", (@Compiled = 1))
; Check & update WebDriver per user setting
_RunDemo_Update($idUpdate, $sBrowserName)
If @error Then Return SetError(@error, @extended, 0)
; Check and set desired headless mode
Local $bHeadless = _RunDemo_Headless($idHeadless)
; Execute browser setup routine for user's browser selection
Local $sCapabilities = Call($aBrowsers[_GUICtrlComboBox_GetCurSel($idBrowsers)][1], $bHeadless)
If @error Then Return SetError(@error, @extended, 0)
ConsoleWrite("> wd_demo.au3: _WD_Startup" & @CRLF)
Local $iWebDriver_PID = _WD_Startup()
If _RunDemo_ErrorHander((@error <> $_WD_ERROR_Success), @error, @extended, $iWebDriver_PID, $sSession) Then Return
ConsoleWrite("> wd_demo.au3: _WD_CreateSession" & @CRLF)
$sSession = _WD_CreateSession($sCapabilities)
If _RunDemo_ErrorHander((@error <> $_WD_ERROR_Success), @error, @extended, $iWebDriver_PID, $sSession) Then Return
Local $iError, $sDemoName
For $iIndex = 0 To UBound($aDemoSuite, $UBOUND_ROWS) - 1
$sDemoName = $aDemoSuite[$iIndex][0]
If Not $aDemoSuite[$iIndex][1] Then
ConsoleWrite("> wd_demo.au3: Bypass: " & $sDemoName & @CRLF)
ContinueLoop
EndIf
_WD_CheckContext($sSession, False)
$iError = @error
If @error Then ExitLoop ; return if session is NOT OK
ConsoleWrite("+ wd_demo.au3: Running: " & $sDemoName & @CRLF)
If $aDemoSuite[$iIndex][2] Then
Call($sDemoName, $sBrowserName)
Else
Call($sDemoName)
EndIf
$iError = @error
If $iError <> $_WD_ERROR_Success Then ExitLoop
ConsoleWrite("+ wd_demo.au3: Finished: " & $sDemoName & @CRLF)
Next
_RunDemo_ErrorHander(True, $iError, @extended, $iWebDriver_PID, $sSession, $sDemoName)
EndFunc ;==>RunDemo
Func _RunDemo_Update($idUpdate, $sBrowserName)
Local $sUpdate
_GUICtrlComboBox_GetLBText($idUpdate, _GUICtrlComboBox_GetCurSel($idUpdate), $sUpdate)
Local $bFlag64 = (StringInStr($sUpdate, '64') > 0)
If StringInStr($sUpdate, 'Current') Then $bFlag64 = Default
Local $bForce = (StringInStr($sUpdate, 'Force') > 0)
If $sUpdate = 'Report only' Then $bForce = Null
Local $bUpdateResult = _WD_UpdateDriver($sBrowserName, @ScriptDir, $bFlag64, $bForce)
Local $iErr = @error, $iExt = @extended
ConsoleWrite('> UpdateResult = ' & $bUpdateResult & @CRLF)
Return SetError($iErr, $iExt, $bUpdateResult)
EndFunc ;==>_RunDemo_Update
Func _RunDemo_Headless($idHeadless)
Local $sHeadless
_GUICtrlComboBox_GetLBText($idHeadless, _GUICtrlComboBox_GetCurSel($idHeadless), $sHeadless)
Return ($sHeadless = 'Yes')
EndFunc ;==>_RunDemo_Headless
Func _RunDemo_Output($idOutput)
Local $sOutput
_GUICtrlComboBox_GetLBText($idOutput, _GUICtrlComboBox_GetCurSel($idOutput), $sOutput)
Switch $sOutput
Case 'ConsoleWrite'
_WD_Option('console', ConsoleWrite)
Case 'WD_Console.log'
_WD_Option('console', @ScriptDir & '\WD_Console.log')
Case '_DebugOut'
_DebugSetup('wd_demo - console log output')
_WD_Option('console', _DebugOut)
Case 'Null'
_WD_Option('console', Null)
EndSwitch
Return $sOutput
EndFunc ;==>_RunDemo_Output
Func _RunDemo_GUISwitcher($iState, $idBrowsers, $idDebugging, $idUpdate, $idHeadless, $idOutput, $idButton_Run, $aCheckboxes)
GUICtrlSetState($idBrowsers, $iState)
GUICtrlSetState($idDebugging, $iState)
GUICtrlSetState($idUpdate, $iState)
GUICtrlSetState($idHeadless, $iState)
GUICtrlSetState($idOutput, $iState)
GUICtrlSetState($idButton_Run, $iState)
For $i = 0 To UBound($aCheckboxes, $UBOUND_ROWS) - 1 Step 1
GUICtrlSetState($aCheckboxes[$i], $iState)
Next
EndFunc ;==>_RunDemo_GUISwitcher
Func _RunDemo_ErrorHander($bForceDispose, $iError, $iExtended, $iWebDriver_PID, $sSession, $sDemoName = 'Demo')
If Not $bForceDispose Then Return SetError($iError, $iExtended, $bForceDispose)
Switch $iError
Case $_WD_ERROR_Success
MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, 'Demo complete!', 'Click "Ok" button to shutdown the browser and console')
Case $_WD_ERROR_UserAbort
ConsoleWrite("! wd_demo.au3: (" & @ScriptLineNumber & ") : Aborted: " & $sDemoName & @CRLF)
MsgBox($MB_ICONINFORMATION, $sDemoName & ' aborted!', 'Click "Ok" button to shutdown the browser and console')
Case Else
ConsoleWrite("! Error = " & $iError & " occurred on: " & $sDemoName & @CRLF)
ConsoleWrite("! _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
ConsoleWrite("! _WD_LastHTTPResponse = " & _WD_LastHTTPResponse() & @CRLF)
ConsoleWrite("! _WD_GetSession = " & _WD_GetSession($sSession) & @CRLF)
MsgBox($MB_ICONERROR + $MB_TOPMOST, $sDemoName & ' error!', 'Check logs')
EndSwitch
If $sSession Then _WD_DeleteSession($sSession)
If $iWebDriver_PID Then _WD_Shutdown()
If FuncName(_WD_Option('console')) = '_DebugOut' Then
; Close debug window and reset environment for next run
Local $hWndReportWindow = WinGetHandle($__g_sReportTitle_Debug, $__g_sReportWindowText_Debug)
GUIDelete($hWndReportWindow)
$__g_bReportWindowWaitClose_Debug = True
$__g_bReportWindowClosed_Debug = True
$__g_iReportType_Debug = 2 ; Prevents window from appearing during script exit
EndIf
Return SetError($iError, $iExtended, $bForceDispose)
EndFunc ;==>_RunDemo_ErrorHander
Func DemoTimeouts()
; Retrieve current settings and save
Local $sResponse = _WD_Timeouts($sSession)
Local $oJSON = Json_Decode($sResponse)
Local $sTimouts = Json_Encode(Json_Get($oJSON, "[value]"))
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
; Set page load timeout
_WD_Timeouts($sSession, '{"pageLoad":2000}')
; Retrieve current settings
_WD_Timeouts($sSession)
; This should timeout
_Demo_NavigateCheckBanner($sSession, "https://yahoo.com", '//body[contains(@class, "blur-preview-tpl")]')
If @error Then Return SetError(@error, @extended)
; Restore initial settings
_WD_Timeouts($sSession, $sTimouts)
EndFunc ;==>DemoTimeouts
Func DemoNavigation()
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : URL=" & _WD_Action($sSession, 'url') & @CRLF)
_WD_NewTab($sSession)
_Demo_NavigateCheckBanner($sSession, "https://yahoo.com", '//body[contains(@class, "blur-preview-tpl")]')
If @error Then Return SetError(@error, @extended)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : URL=" & _WD_Action($sSession, 'url') & @CRLF)
_WD_NewTab($sSession, True, Default, 'https://bing.com', 'width=200,height=200')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : URL=" & _WD_Action($sSession, 'url') & @CRLF)
_WD_Attach($sSession, "google.com", "URL")
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : URL=" & _WD_Action($sSession, 'url') & @CRLF)
_WD_Attach($sSession, "yahoo.com", "URL")
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : URL=" & _WD_Action($sSession, 'url') & @CRLF)
EndFunc ;==>DemoNavigation
Func DemoElements()
Local $sElement, $aElements, $sValue, $sButton, $sResponse, $bDecode, $hFileOpen
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
; Locate a single element
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector)
; Get element's coordinates
Local $oERect = _WD_ElementAction($sSession, $sElement, 'rect')
If IsObj($oERect) Then
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Element Coords = " & $oERect.Item('x') & " / " & $oERect.Item('y') & " / " & $oERect.Item('width') & " / " & $oERect.Item('height') & @CRLF)
EndIf
; Locate multiple matching elements
$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", Default, True)
_ArrayDisplay($aElements, "Found Elements")
; Set element's contents
_WD_ElementAction($sSession, $sElement, 'value', "testing 123")
Sleep(500)
; Retrieve then clear contents
$sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')
_WD_ElementAction($sSession, $sElement, 'clear')
Sleep(500)
_WD_ElementAction($sSession, $sElement, 'value', "abc xyz")
Sleep(500)
$sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')
_WD_ElementAction($sSession, $sElement, 'clear')
Sleep(500)
_WD_ElementAction($sSession, $sElement, 'value', "fujimo")
Sleep(500)
Local $sValue1 = _WD_ElementAction($sSession, $sElement, 'property', 'value')
Local $sValue2 = _WD_ElementAction($sSession, $sElement, 'value')
MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, 'result #' & @ScriptLineNumber, $sValue1 & " / " & $sValue2)
; Click input element
_WD_ElementAction($sSession, $sElement, 'click')
; Click search button
$sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='btnK']")
_WD_ElementAction($sSession, $sButton, 'click')
_WD_LoadWait($sSession, 2000)
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector)
$sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & @CRLF)
; Take element screenshot
$sResponse = _WD_ElementAction($sSession, $sElement, 'screenshot')
$bDecode = __WD_Base64Decode($sResponse)
$hFileOpen = FileOpen("Element.png", $FO_BINARY + $FO_OVERWRITE)
FileWrite($hFileOpen, $bDecode)
FileClose($hFileOpen)
_Demo_NavigateCheckBanner($sSession, "https://demo.guru99.com/test/simple_context_menu.html", '//div[@id="gdpr-consent-tool-wrapper" and @aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
Sleep(2000)
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[contains(text(),'Double-Click Me To See Alert')]")
If @error = $_WD_ERROR_Success Then
_WD_ElementActionEx($sSession, $sElement, "doubleclick")
EndIf
Sleep(2000)
_WD_Alert($sSession, 'accept')
_WD_ElementActionEx($sSession, $sElement, "hide")
Sleep(5000)
_WD_ElementActionEx($sSession, $sElement, "show")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//span[@class='context-menu-one btn btn-neutral']")
If @error = $_WD_ERROR_Success Then
_WD_ElementActionEx($sSession, $sElement, "rightclick")
EndIf
EndFunc ;==>DemoElements
Func DemoScript()
Local $sValue
; JavaScript example with arguments
$sValue = _WD_ExecuteScript($sSession, "return arguments[0].second;", '{"first": "1st", "second": "2nd", "third": "3rd"}', Default, $_WD_JSON_Value)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & " _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
; JavaScript example that fires error because of unknown function
$sValue = _WD_ExecuteScript($sSession, "dslfkjsdklfj;", Default, Default, $_WD_JSON_Value)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & " _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
; JavaScript example that writes to BrowserConsole
$sValue = _WD_ExecuteScript($sSession, "console.log('Hello world! (from DemoScript: Line #" & @ScriptLineNumber & ")');", Default, Default, $_WD_JSON_Value)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & " _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
; JavaScript example with SyntaxError: string literal contains an unescaped line break
$sValue = _WD_ExecuteScript($sSession, "console.log('Hello world", Default, Default, $_WD_JSON_Value)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & " _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
; 2022-03-23 This website no longer exists
;$sValue = _WD_ExecuteScript($sSession, "return $.ajax({url:'https://hosting105782.a2f0c.netcup.net/test.php',type:'post',dataType: 'text', data:'getaccount=1',success : function(text){return text;}});", Default, $_WD_JSON_Value)
;ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : ERROR=" & @error & " $sValue = " & $sValue & " _WD_LastHTTPResult = " & _WD_LastHTTPResult() & @CRLF)
EndFunc ;==>DemoScript
Func DemoCookies()
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Navigating:" & @CRLF)
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Get all cookies:" & @CRLF)
Local $sAllCookies = _WD_Cookies($sSession, 'getall')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookies (obtained at start after navigate) : " & $sAllCookies & @CRLF)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Get 'NID' cookie:" & @CRLF)
Local $hTimer = TimerInit()
Local $sNID
While 1
$sNID = _WD_Cookies($sSession, 'Get', 'NID')
If Not @error Or TimerDiff($hTimer) > 5 * 1000 Then ExitLoop
Sleep(100) ; this cookie may not exist at start, may appear later when website will be estabilished, so there is need to wait on @error and try again
WEnd
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookie obtained 'NID' : " & $sNID & @CRLF)
Local $sName = "TestName"
Local $sValue = "TestValue"
; calculate UNIX EPOCH time
Local $sNowPlus2Years = _DateAdd('Y', 2, _NowCalc())
Local $iDateCalc = Int(_DateDiff('s', "1970/01/01 00:00:00", $sNowPlus2Years))
; create JSON string for cookie
Local $sCookie = _WD_JsonCookie($sName, $sValue, Default, 'www.google.com', True, False, $iDateCalc, "None")
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Add cookie:" & @CRLF)
_WD_Cookies($sSession, 'add', $sCookie)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Check cookie:" & @CRLF)
Local $sResult = _WD_Cookies($sSession, 'get', $sName)
; compare results in console
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookie added : " & $sCookie & @CRLF)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookie obtained : " & $sResult & @CRLF)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Get all cookies:" & @CRLF)
$sAllCookies = _WD_Cookies($sSession, 'getall')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookies (obtained before 'deleteall') : " & $sAllCookies & @CRLF)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Delete all cookies:" & @CRLF)
_WD_Cookies($sSession, 'deleteall')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : WD: Get all cookies:" & @CRLF)
$sAllCookies = _WD_Cookies($sSession, 'getall')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Cookies (obtained after 'deleteall') : " & $sAllCookies & @CRLF)
EndFunc ;==>DemoCookies
Func DemoAlerts()
Local $sStatus, $sText
; check status before displaying Alert
$sStatus = _WD_Alert($sSession, 'status')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : " & 'Alert Detected => ' & $sStatus & @CRLF)
; show Alert for testing
_WD_ExecuteScript($sSession, "alert('testing 123')")
; get/check Alert status and text
$sStatus = _WD_Alert($sSession, 'status')
$sText = _WD_Alert($sSession, 'gettext')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : " & 'Alert Detected => ' & $sStatus & @CRLF)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : " & 'Text Detected => ' & $sText & @CRLF)
Sleep(5000)
; close Alert by rejection
_WD_Alert($sSession, 'Dismiss')
; show Prompt for testing
_WD_ExecuteScript($sSession, "prompt('User Prompt 1', 'Default value')")
Sleep(2000)
; Set value of text field
_WD_Alert($sSession, 'sendtext', 'new text')
Sleep(5000)
; close Alert by acceptance
_WD_Alert($sSession, 'Accept')
Sleep(1000)
; show Prompt for testing
_WD_ExecuteScript($sSession, "prompt('User Prompt 2', 'Default value')")
Sleep(5000)
; close Alert by rejection
_WD_Alert($sSession, 'Dismiss')
_WD_Navigate($sSession, 'https://www.quanzhanketang.com/jsref/tryjsref_prompt.html?filename=tryjsref_prompt')
_WD_LoadWait($sSession)
_WD_FrameEnter($sSession, 0)
Local $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "button[onclick='myFunction()']")
_WD_ElementAction($sSession, $sButton, 'CLICK')
Sleep(2000)
; Set value of text field
_WD_Alert($sSession, 'sendtext', 'AutoIt user')
Sleep(2000)
; close Alert by acceptance
_WD_Alert($sSession, 'Accept')
; Validate if prompt was properly filled up by _WD_Alert()
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information", "Check website resposne")
EndFunc ;==>DemoAlerts
Func DemoFrames()
Local $sElement, $bIsWindowTop
Local Const $sArrayHeader = 'Absolute Identifiers > _WD_FrameEnter|Relative Identifiers > _WD_FrameEnter|FRAME attributes|URL|Body ElementID|IsHidden|MatchedElements'
#Region - Testing how to manage frames
_Demo_NavigateCheckBanner($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe", '//*[@id="snigel-cmp-framework" and @class="snigel-cmp-framework"]')
If @error Then Return SetError(@error, @extended)
Local $iFrameCount = _WD_GetFrameCount($sSession)
If @error Then Return SetError(@error, @extended)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Frames=" & $iFrameCount & @CRLF)
$bIsWindowTop = _WD_IsWindowTop($sSession)
If @error Then Return SetError(@error, @extended)
; just after navigate current context should be on top level Window
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF)
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe[@id='iframeResult']")
; changing context to first frame
_WD_FrameEnter($sSession, $sElement)
If @error Then Return SetError(@error, @extended)
$bIsWindowTop = _WD_IsWindowTop($sSession)
; after changing context to first frame the current context is not on top level Window
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF)
; changing context to first sub frame using iframe element specified ByXPath
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe")
_WD_FrameEnter($sSession, $sElement)
If @error Then Return SetError(@error, @extended)
; Leaving sub frame
_WD_FrameLeave($sSession)
If @error Then Return SetError(@error, @extended)
$bIsWindowTop = _WD_IsWindowTop($sSession)
; after leaving sub frame, the current context is back to first frame but still is not on top level Window
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF)
; Leaving first frame
_WD_FrameLeave($sSession)
If @error Then Return SetError(@error, @extended)
#EndRegion - Testing how to manage frames
#Region - Testing _WD_FrameList() usage
#Region - Example 1 ; from 'https://www.w3schools.com' get frame list as string
$bIsWindowTop = _WD_IsWindowTop($sSession)
; after leaving first frame, the current context should back on top level Window
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF)
; now lets try to check frame list and using locations as path 'null/0'
; firstly go to website
_WD_Navigate($sSession, 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe')
_WD_LoadWait($sSession)
Local $sResult = _WD_FrameList($sSession, False)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 1" & @CRLF)
ConsoleWrite($sResult & @CRLF)
#EndRegion - Example 1 ; from 'https://www.w3schools.com' get frame list as string
#Region - Example 2 ; from 'https://www.w3schools.com' get frame list as array
Local $aFrameList = _WD_FrameList($sSession, True)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 2" & @CRLF)
_ArrayDisplay($aFrameList, 'Example 2 - w3schools.com - get frame list as array', 0, 0, Default, $sArrayHeader)
#EndRegion - Example 2 ; from 'https://www.w3schools.com' get frame list as array
#Region - Example 3 ; from 'https://www.w3schools.com' get frame list as array, while current location is "null/0"
; check if document context location is Top Window - should be as we are after navigation
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
; change document context location by path 'null/0'
_WD_FrameEnter($sSession, 'null/0')
; check if document context location is Top Window - should not be as we enter to frame 'null/0'
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
$aFrameList = _WD_FrameList($sSession, True)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 3" & @CRLF)
_ArrayDisplay($aFrameList, 'Example 3 - w3schools.com - relative to "null/0"', 0, 0, Default, $sArrayHeader)
#EndRegion - Example 3 ; from 'https://www.w3schools.com' get frame list as array, while current location is "null/0"
#Region - Example 4 ; from 'https://stackoverflow.com' get frame list as string
; go to another website
_WD_Navigate($sSession, 'https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom')
_WD_LoadWait($sSession)
$sResult = _WD_FrameList($sSession, False)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 4" & @CRLF)
ConsoleWrite($sResult & @CRLF)
#EndRegion - Example 4 ; from 'https://stackoverflow.com' get frame list as string
#Region - Example 5 ; from 'https://stackoverflow.com' get frame list as array
$aFrameList = _WD_FrameList($sSession, True)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 5" & @CRLF)
_ArrayDisplay($aFrameList, 'Example 5 - stackoverflow.com - get frame list as array', 0, 0, Default, $sArrayHeader)
#EndRegion - Example 5 ; from 'https://stackoverflow.com' get frame list as array
#Region - Example 6v1 ; from 'https://stackoverflow.com' get frame list as array, while is current location is "null/2"
; check if document context location is Top Window - should be as we are after navigation
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
; change document context location by path 'null/2'
_WD_FrameEnter($sSession, 'null/2')
; check if document context location is Top Window - should not be as we enter to frame 'null/2'
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
$aFrameList = _WD_FrameList($sSession, True)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 6v1" & @CRLF)
_ArrayDisplay($aFrameList, 'Example 6v1 - stackoverflow.com - relative to "null/2"', 0, 0, Default, $sArrayHeader)
#EndRegion - Example 6v1 ; from 'https://stackoverflow.com' get frame list as array, while is current location is "null/2"
#Region - Example 6v2 ; from 'https://stackoverflow.com' get frame list as array, check if it is still relative to the same location as it was before recent _WD_FrameList() was used - still should be "null/2"
; check if document context location is Top Window
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
$aFrameList = _WD_FrameList($sSession, True)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 6v2" & @CRLF)
_ArrayDisplay($aFrameList, 'Example 6v2 - stackoverflow.com - check if it is still relative to "null/2"', 0, 0, Default, $sArrayHeader)
#EndRegion - Example 6v2 ; from 'https://stackoverflow.com' get frame list as array, check if it is still relative to the same location as it was before recent _WD_FrameList() was used - still should be "null/2"
#EndRegion - Testing _WD_FrameList() usage
#Region - Testing element location in frame set and iframe collecion
; go to website
_WD_Navigate($sSession, 'https://www.tutorialspoint.com/html/html_frames.htm#')
_WD_LoadWait($sSession)
; check if document context location is Top Window
ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF)
MsgBox($MB_TOPMOST, "", 'Before checking location of multiple elements on multiple frames' & @CRLF & 'Try the same example with and without waiting about 30 seconds in order to see that many frames should be fully loaded, and to check the differences')
$aFrameList = _WD_FrameList($sSession, True, 5000, Default)
ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example : Testing element location in frame set - after pre-checking list of frames" & @CRLF)
_ArrayDisplay($aFrameList, @ScriptLineNumber & ' Before _WD_FrameListFindElement - www.tutorialspoint.com - get frame list as array', 0, 0, Default, $sArrayHeader)
Local $aLocationOfElement = _WD_FrameListFindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "li.nav-item[data-bs-original-title='Home Page'] a.nav-link[href='https://www.tutorialspoint.com/index.htm']")
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : $aLocationOfElement (" & UBound($aLocationOfElement) & ")=" & @CRLF & _ArrayToString($aLocationOfElement) & @CRLF)
_ArrayDisplay($aLocationOfElement, @ScriptLineNumber & ' $aLocationOfElement', 0, 0, Default, $sArrayHeader)
#EndRegion - Testing element location in frame set and iframe collecion
EndFunc ;==>DemoFrames
Func DemoActions()
Local $sElement, $sAction
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector)
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : $sElement = " & $sElement & @CRLF)
$sAction = StringReplace( _
'{' & _
' "actions":[' & _
' {' & _
' "id":"default mouse",' & _
' "type":"pointer",' & _
' "parameters":{"pointerType":"mouse"},' & _
' "actions":[' & _
_WD_JsonActionPointer("pointerMove", Default, $sElement, 0, 0, 100) & ',' & _
_WD_JsonActionPointer("pointerDown", $_WD_BUTTON_Right) & ',' & _
_WD_JsonActionPointer("pointerUp", $_WD_BUTTON_Right) & _
' ]' & _
' }' & _
' ]' & _
'}' & _
'', @TAB, '')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : $sAction = " & $sAction & @CRLF)
; perform Action
_WD_Action($sSession, "actions", $sAction)
Sleep(2000)
Send("Q")
Sleep(2000)
_WD_Action($sSession, "actions")
Sleep(2000)
EndFunc ;==>DemoActions
Func DemoDownload()
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
; Get the website's URL
Local $sURL = _WD_Action($sSession, 'url')
; Find the element
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//img[@alt='Google']")
If @error <> $_WD_ERROR_Success Then
; Try alternate element
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='hplogo']//img")
EndIf
If @error = $_WD_ERROR_Success Then
; Retrieve it's source attribute
Local $sSource = _WD_ElementAction($sSession, $sElement, "Attribute", "src")
; Combine the URL and element link
$sURL = _WinAPI_UrlCombine($sURL, $sSource)
; Download the file
_WD_DownloadFile($sURL, @ScriptDir & "\testimage.png")
_WD_DownloadFile("https://www.google.com/notexisting.jpg", @ScriptDir & "\testimage2.jpg")
EndIf
EndFunc ;==>DemoDownload
Func DemoWindows()
Local $sResponse, $hFileOpen, $sHnd1, $sHnd2, $bDecode, $oWRect
_Demo_NavigateCheckBanner($sSession, "https://google.com", '//body/div[1][@aria-hidden="true"]')
If @error Then Return SetError(@error, @extended)
$sHnd1 = '{"handle":"' & _WD_Window($sSession, "window") & '"}'
_WD_NewTab($sSession)
$sHnd2 = '{"handle":"' & _WD_Window($sSession, "window") & '"}'
_Demo_NavigateCheckBanner($sSession, "https://yahoo.com", '//body[contains(@class, "blur-preview-tpl")]')
If @error Then Return SetError(@error, @extended)
; Get window coordinates
$oWRect = _WD_Window($sSession, 'rect')
ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Window Coords = " & $oWRect.Item('x') & " / " & $oWRect.Item('y') & " / " & $oWRect.Item('width') & " / " & $oWRect.Item('height') & @CRLF)
; Take screenshot
_WD_Window($sSession, "switch", $sHnd1)
$sResponse = _WD_Window($sSession, 'screenshot')
$bDecode = __WD_Base64Decode($sResponse)
$hFileOpen = FileOpen("Screen1.png", $FO_BINARY + $FO_OVERWRITE)
FileWrite($hFileOpen, $bDecode)
FileClose($hFileOpen)
; show the result in default viewer
ShellExecute("Screen1.png")
; Take another one
_WD_Window($sSession, "switch", $sHnd2)
$sResponse = _WD_Window($sSession, 'screenshot')
$bDecode = __WD_Base64Decode($sResponse)
$hFileOpen = FileOpen("Screen2.png", $FO_BINARY + $FO_OVERWRITE)
FileWrite($hFileOpen, $bDecode)
FileClose($hFileOpen)
; show the result in default viewer
ShellExecute("Screen2.png")
EndFunc ;==>DemoWindows
Func DemoUpload()
; REMARK This example uses PNG files created in DemoWindows
; navigate to "file storing" website
_WD_Navigate($sSession, "https://www.htmlquick.com/reference/tags/input-file.html")
; select single file
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//section[@id='examples']//input[@name='uploadedfile']", @ScriptDir & "\Screen1.png")
; select two files at once
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[@name='uploadedfiles[]']", @ScriptDir & "\Screen1.png" & @LF & @ScriptDir & "\Screen2.png")
; accept/start uploading
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[2]")
_WD_ElementAction($sSession, $sElement, 'click')
EndFunc ;==>DemoUpload
Func DemoPrint($sBrowser)
; navigate to website
_WD_Navigate($sSession, "https://www.w3.org/TR/webdriver/#print-page")
; Wait for page will be fully load - max 10 seconds
_WD_LoadWait($sSession, Default, 10 * 1000)
; create Print Options
Local $sOptions = StringReplace( _
'{' & _
' "page": {' & _
' "width": 29.70' & _
' ,"height": 42.00' & _
' }' & _
' ,"margin": {' & _
' "top": 2' & _
' ,"bottom": 2' & _
' ,"left": 2' & _
' ,"right": 2' & _
' }' & _
' ,"scale": 0.5' & _
' ,"orientation": "landscape"' & _
' ,"shrinkToFit": true' & _
' ,"background": true' & _
' ,"pageRanges": ["1", "10-20"]' & _
'}', @TAB, '')
; print WebSite content to PDF as Binary
Local $dBinaryData = _WD_PrintToPdf($sSession, $sOptions)
If @error Then Return SetError(@error, @extended, $dBinaryData)
; save PDF to file
Local $sPDFFileFullPath = @ScriptDir & "\" & $sBrowser & " - DemoPrint.pdf"
Local $hFile = FileOpen($sPDFFileFullPath, $FO_OVERWRITE + $FO_BINARY)
FileWrite($hFile, $dBinaryData)
FileClose($hFile)
; open PDF in default viewer configured in Windows
ShellExecute($sPDFFileFullPath)
EndFunc ;==>DemoPrint
Func DemoSleep()
; enable Abort button
GUICtrlSetState($__g_idButton_Abort, $GUI_ENABLE)
; set up outer/user specific sleep function to take control
_WD_Option("Sleep", _USER_WD_Sleep)
_WD_Navigate($sSession, "https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win/")
Local $iError = @error
If Not $iError Then
; it can take a long time to load full content of this webpage
; this following function is waiting to the progress spinner will hide
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, '//img[@class="loader-spinner ng-hide" and @ng-show="loading"]', Default, 3 * 60 * 1000)
$iError = @error
; normaly it will wait as webpage will load full content (hidden spinner) or will end with TimeOut
; but thanks to using _WD_Option("Sleep", _USER_WD_Sleep) you can abort waiting by clicking scecial Abourt button or by clicking X closing button on the "Webdriver Demo" GUI window
EndIf
; disable Abort button
GUICtrlSetState($__g_idButton_Abort, $GUI_DISABLE)
; set up internal sleep function - back to standard route
_WD_Option("Sleep", Sleep)
Return SetError($iError)
EndFunc ;==>DemoSleep
Func DemoSelectOptions()
_WD_Navigate($sSession, 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#advanced_select_with_multiple_features')
If @error Then Return SetError(@error, @extended, '')
_WD_LoadWait($sSession)
If @error Then Return SetError(@error, @extended, '')
Local $sFrame = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, '#frame_advanced_select_with_multiple_features')
If @error Then Return SetError(@error, @extended, '')
; change the attributes of the frame to improve the visibility of the <select> element, on which the options will be indicated
Local $sJavaScript = _
"var element = arguments[0];" & _
"element.setAttribute('height', '500');" & _
"element.setAttribute('padding', '0');"
_WD_ExecuteScript($sSession, $sJavaScript, __WD_JsonElement($sFrame), Default, Default)
If @error Then Return SetError(@error, @extended, '')
; entering to the frame
_WD_FrameEnter($sSession, $sFrame)
If @error Then Return SetError(@error, @extended, '')
; get <select> element by it's name
Local $sSelectElement = _WD_GetElementByName($sSession, 'pets')
If @error Then Return SetError(@error, @extended, '')
; change <select> element size, to see all <option> at once
$sJavaScript = _
"var element = arguments[0];" & _
"element.setAttribute('size', '10')"
_WD_ExecuteScript($sSession, $sJavaScript, __WD_JsonElement($sSelectElement), Default, Default)
; select ALL options
_WD_ElementSelectAction($sSession, $sSelectElement, 'SELECTALL')
If @error Then Return SetError(@error, @extended, '')
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information", "After SELECTALL")
; deselect ALL options
_WD_ElementSelectAction($sSession, $sSelectElement, 'DESELECTALL')
If @error Then Return SetError(@error, @extended, '')
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information", "After DESELECTALL")
; select desired options
Local $aOptionsToSelect[] = ['Cat', 'HAMSTER', 'parrot', 'albatroSS']
_WD_ElementSelectAction($sSession, $sSelectElement, 'MULTISELECT', $aOptionsToSelect)
If @error Then Return SetError(@error, @extended, '')
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information", "After MULTISELECT: Cat / HAMSTER / parrot / albatroSS")
; retrieves selected <option> elements as 2D array
Local $aSelectedOptions = _WD_ElementSelectAction($sSession, $sSelectElement, 'selectedOptions')
If @error Then Return SetError(@error, @extended, '')
_ArrayDisplay($aSelectedOptions, '$aSelectedOptions')
_WD_ElementSelectAction($sSession, $sSelectElement, 'SINGLESELECT', 'PARROT')
If @error Then Return SetError(@error, @extended, '')
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information", "After SINGLESELECT: PARROT (Parrot)")
; retrieves all <option> elements as 2D array
Local $aAllOptions = _WD_ElementSelectAction($sSession, $sSelectElement, 'OPTIONS')
If @error Then Return SetError(@error, @extended, '')
_ArrayDisplay($aAllOptions, '$aAllOptions')
; deselect ALL options
_WD_ElementSelectAction($sSession, $sSelectElement, 'DESELECTALL')
If @error Then Return SetError(@error, @extended, '')
; disable 'multiple' attribute for <select> element
$sJavaScript = _
"var element = arguments[0];" & _
"element.multiple = false"