Skip to content

Commit 9e0a32b

Browse files
authored
Merge pull request #5 from Danp2/dp_updatecord
Update _WD_FindElement
2 parents 2a8ec87 + 38402c8 commit 9e0a32b

File tree

3 files changed

+84
-69
lines changed

3 files changed

+84
-69
lines changed

wd_core.au3

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#cs
2020
V0.1.0.4
2121
- Changed: Renamed core UDF functions
22+
- Changed: _WD_FindElement now returns multiple elements as an array instead of raw JSON
2223
2324
V0.1.0.3
2425
- Fixed: Error constants
@@ -520,7 +521,8 @@ EndFunc
520521
; ===============================================================================================================================
521522
Func _WD_FindElement($sSession, $sStrategy, $sSelector, $sStartElement = "", $lMultiple = False)
522523
Local Const $sFuncName = "_WD_FindElement"
523-
Local $sCmd, $sElement, $sResponse, $sResult, $iErr, $Obj, $Obj2, $sKey, $sErr
524+
Local $sCmd, $sElement, $sResponse, $sResult, $iErr, $Obj2, $sErr
525+
Local $oJson, $oValues, $sKey, $iRow, $aElements[0]
524526

525527
$sCmd = ($lMultiple) ? 'elements' : 'element'
526528
$sElement = ($sStartElement == "") ? "" : "/element/" & $sStartElement
@@ -530,13 +532,23 @@ Func _WD_FindElement($sSession, $sStrategy, $sSelector, $sStartElement = "", $lM
530532

531533
If $iErr = $_WD_ERROR_Success Then
532534
If $lMultiple Then
533-
$sResult = $sResponse
535+
536+
$oJson = Json_Decode($sResponse)
537+
$oValues = Json_Get($oJson, '[value]')
538+
$sKey = "[" & Json_ObjGetKeys($oValues[0])[0] & "]"
539+
540+
Dim $aElements[UBound($oValues)]
541+
542+
For $oValue In $oValues
543+
$aElements[$iRow] = Json_Get($oValue, $sKey)
544+
$iRow += 1
545+
Next
534546
Else
535-
$Obj = Json_Decode($sResponse)
536-
$Obj2 = Json_Get($Obj, "[value]")
537-
$sKey = Json_ObjGetKeys($Obj2)[0]
547+
$oJson = Json_Decode($sResponse)
548+
$Obj2 = Json_Get($oJson, "[value]")
549+
$sKey = Json_ObjGetKeys($oJson)[0]
538550

539-
$sResult = Json_Get($Obj, "[value][" & $sKey & "]")
551+
$sResult = Json_Get($oJson, "[value][" & $sKey & "]")
540552
EndIf
541553
EndIf
542554

@@ -546,16 +558,16 @@ Func _WD_FindElement($sSession, $sStrategy, $sSelector, $sStartElement = "", $lM
546558

547559
If $iErr Then
548560
If $_WD_HTTPRESULT = 404 Then
549-
$Obj = Json_Decode($sResponse)
550-
$sErr = Json_Get($Obj, "[value][error]")
561+
$oJson = Json_Decode($sResponse)
562+
$sErr = Json_Get($oJson, "[value][error]")
551563

552564
SetError(__WD_Error($sFuncName, $_WD_ERROR_NoMatch, $sErr), $_WD_HTTPRESULT)
553565
Else
554566
SetError(__WD_Error($sFuncName, $_WD_ERROR_Exception, "HTTP status = " & $_WD_HTTPRESULT), $_WD_HTTPRESULT)
555567
EndIf
556568
EndIf
557569

558-
Return $sResult
570+
Return ($lMultiple) ? $aElements : $sResult
559571
EndFunc ;==>_WDFindElement
560572

561573

wd_helper.au3

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ Func _WD_NewTab($sSession, $lSwitch = True)
5656
Local Const $sFuncName = "_WD_NewTab"
5757
Local $sTabHandle = ''
5858

59-
_WDExecuteScript($sSession, 'window.open()', '{}')
59+
_WD_ExecuteScript($sSession, 'window.open()', '{}')
6060

6161
If @error = $_WD_ERROR_Success Then
62-
Local $aHandles = _WDWindow($sSession, 'handles', '')
62+
Local $aHandles = _WD_Window($sSession, 'handles', '')
6363

6464
$sTabHandle = $aHandles[UBound($aHandles) - 1]
6565

6666
If $lSwitch Then
67-
_WDWindow($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
67+
_WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
6868
EndIf
6969
EndIf
7070

@@ -95,25 +95,25 @@ Func _WD_Attach($sSession, $sString, $sMode = 'title')
9595
Local Const $sFuncName = "_WD_Attach"
9696
Local $sTabHandle = '', $lFound = False
9797

98-
Local $sCurrentTab = _WDWindow($sSession, 'window')
99-
Local $aHandles = _WDWindow($sSession, 'handles')
98+
Local $sCurrentTab = _WD_Window($sSession, 'window')
99+
Local $aHandles = _WD_Window($sSession, 'handles')
100100

101101
$sMode = StringLower($sMode)
102102

103103
For $sHandle In $aHandles
104104

105-
_WDWindow($sSession, 'Switch', '{"handle":"' & $sHandle & '"}')
105+
_WD_Window($sSession, 'Switch', '{"handle":"' & $sHandle & '"}')
106106

107107
Switch $sMode
108108
Case "title", "url"
109-
If StringInStr(_WDAction($sSession, $sMode), $sString) > 0 Then
109+
If StringInStr(_WD_Action($sSession, $sMode), $sString) > 0 Then
110110
$lFound = True
111111
$sTabHandle = $sHandle
112112
ExitLoop
113113
EndIf
114114

115115
Case 'html'
116-
If StringInStr(_WDGetSource($sSession), $sString) > 0 Then
116+
If StringInStr(_WD_GetSource($sSession), $sString) > 0 Then
117117
$lFound = True
118118
$sTabHandle = $sHandle
119119
ExitLoop
@@ -127,7 +127,7 @@ Func _WD_Attach($sSession, $sString, $sMode = 'title')
127127

128128
If Not $lFound Then
129129
; Restore prior active tab
130-
_WDWindow($sSession, 'Switch', '{"handle":"' & $sCurrentTab & '"}')
130+
_WD_Window($sSession, 'Switch', '{"handle":"' & $sCurrentTab & '"}')
131131
SetError(__WD_Error($sFuncName, $_WD_ERROR_NoMatch))
132132
EndIf
133133

wd_test.au3

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
Local Enum $eFireFox = 0, _
55
$eChrome
66

7-
Local $aTestSuite[][2] = [["TestTimeouts", False], ["TestNavigation", False], ["TestElements", False], ["TestScript", True], ["TestCookies", False], ["TestAlerts", True]]
7+
Local $aTestSuite[][2] = [["TestTimeouts", False], ["TestNavigation", False], ["TestElements", True], ["TestScript", False], ["TestCookies", False], ["TestAlerts", False]]
88

9-
Local Const $_TestType = $eFireFox
9+
Local Const $_TestType = $eChrome
1010
Local $sDesiredCapabilities
1111
Local $iIndex
1212
Local $sSession
@@ -22,9 +22,10 @@ Switch $_TestType
2222

2323
EndSwitch
2424

25-
_WDStartup()
25+
_WD_Startup()
26+
27+
$sSession = _WD_CreateSession($sDesiredCapabilities)
2628

27-
$sSession = _WDCreateSession($sDesiredCapabilities)
2829

2930
For $iIndex = 0 To UBound($aTestSuite, $UBOUND_ROWS) - 1
3031
If $aTestSuite[$iIndex][1] Then
@@ -35,90 +36,92 @@ For $iIndex = 0 To UBound($aTestSuite, $UBOUND_ROWS) - 1
3536
EndIf
3637
Next
3738

38-
_WDDeleteSession($sSession)
39-
_WDShutdown()
39+
_WD_DeleteSession($sSession)
40+
_WD_Shutdown()
4041

4142

4243
Func TestTimeouts()
43-
_WDTimeouts($sSession)
44-
_WDTimeouts($sSession, '{"pageLoad":2000}')
45-
_WDTimeouts($sSession)
44+
_WD_Timeouts($sSession)
45+
_WD_Timeouts($sSession, '{"pageLoad":2000}')
46+
_WD_Timeouts($sSession)
4647
EndFunc
4748

4849
Func TestNavigation()
49-
_WDNavigate($sSession, "http://google.com")
50-
ConsoleWrite("URL=" & _WDAction($sSession, 'url') & @CRLF)
51-
_WDAction($sSession, "back")
52-
ConsoleWrite("URL=" & _WDAction($sSession, 'url') & @CRLF)
53-
_WDAction($sSession, "forward")
54-
ConsoleWrite("URL=" & _WDAction($sSession, 'url') & @CRLF)
55-
ConsoleWrite("Title=" & _WDAction($sSession, 'title') & @CRLF)
50+
_WD_Navigate($sSession, "http://google.com")
51+
ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
52+
_WD_Action($sSession, "back")
53+
ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
54+
_WD_Action($sSession, "forward")
55+
ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
56+
ConsoleWrite("Title=" & _WD_Action($sSession, 'title') & @CRLF)
5657
EndFunc
5758

58-
;_WDWindow($sSession, 'frame', '{"id":null}')
59-
59+
;_WDWindow($sSession, 'frame', '{"id":nullelse
6060
Func TestElements()
61-
_WDNavigate($sSession, "http://google.com")
62-
$sElement = _WDFindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib1']")
61+
_WD_Navigate($sSession, "http://google.com")
62+
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib1']")
6363

6464
If @error = $_WD_ERROR_NoMatch Then
65-
$sElement = _WDFindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
65+
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
6666
EndIf
6767

68-
$sElement2 = _WDFindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", '', True)
68+
$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", '', True)
6969

70-
_WDElementAction($sSession, $sElement, 'value', "testing 123")
71-
_WDElementAction($sSession, $sElement, 'text')
72-
_WDElementAction($sSession, $sElement, 'clear')
73-
_WDElementAction($sSession, $sElement, 'value', "abc xyz")
74-
_WDElementAction($sSession, $sElement, 'text')
75-
_WDElementAction($sSession, $sElement, 'clear')
76-
_WDElementAction($sSession, $sElement, 'value', "fujimo")
77-
_WDElementAction($sSession, $sElement, 'text')
78-
_WDElementAction($sSession, $sElement, 'click')
70+
_ArrayDisplay($aElements)
7971

80-
_WDElementAction($sSession, $sElement, 'Attribute', 'test')
72+
_WD_ElementAction($sSession, $sElement, 'value', "testing 123")
73+
_WD_ElementAction($sSession, $sElement, 'text')
74+
_WD_ElementAction($sSession, $sElement, 'clear')
75+
_WD_ElementAction($sSession, $sElement, 'value', "abc xyz")
76+
_WD_ElementAction($sSession, $sElement, 'text')
77+
_WD_ElementAction($sSession, $sElement, 'clear')
78+
_WD_ElementAction($sSession, $sElement, 'value', "fujimo")
79+
_WD_ElementAction($sSession, $sElement, 'text')
80+
_WD_ElementAction($sSession, $sElement, 'click')
8181

82-
$sElement = _WDFindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
83-
$sValue = _WDElementAction($sSession, $sElement, 'property', 'value')
82+
_WD_ElementAction($sSession, $sElement, 'Attribute', 'text')
83+
84+
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
85+
$sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')
8486

8587
ConsoleWrite('value = ' & $sValue & @CRLF)
88+
8689
EndFunc
8790

8891
Func TestScript()
89-
_WDExecuteScript($sSession, "return arguments[0].second;", '{"first": "1st", "second": "2nd", "third": "3rd"}')
90-
_WDAlert($sSession, 'Dismiss')
92+
_WD_ExecuteScript($sSession, "return arguments[0].second;", '{"first": "1st", "second": "2nd", "third": "3rd"}')
93+
_WD_Alert($sSession, 'Dismiss')
9194
EndFunc
9295

9396
Func TestCookies()
94-
_WDNavigate($sSession, "http://google.com")
95-
_WDCookies($sSession, 'Get', 'NID')
97+
_WD_Navigate($sSession, "http://google.com")
98+
_WD_Cookies($sSession, 'Get', 'NID')
9699
EndFunc
97100

98101
Func TestAlerts()
99-
ConsoleWrite('Alert Detected => ' & _WDAlert($sSession, 'status') & @CRLF)
100-
_WDExecuteScript($sSession, "alert('testing 123')")
101-
ConsoleWrite('Alert Detected => ' & _WDAlert($sSession, 'status') & @CRLF)
102-
ConsoleWrite('Text Detected => ' & _WDAlert($sSession, 'gettext') & @CRLF)
103-
_WDAlert($sSession, 'sendtext', 'new text')
104-
ConsoleWrite('Text Detected => ' & _WDAlert($sSession, 'gettext') & @CRLF)
105-
_WDAlert($sSession, 'Dismiss')
102+
ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
103+
_WD_ExecuteScript($sSession, "alert('testing 123')")
104+
ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
105+
ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
106+
_WD_Alert($sSession, 'sendtext', 'new text')
107+
ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
108+
_WD_Alert($sSession, 'Dismiss')
106109

107110
EndFunc
108111

109112

110113
Func SetupGecko()
111-
_WDOption('Driver', 'geckodriver.exe')
112-
_WDOption('DriverParams', '--log trace')
113-
_WDOption('Port', 4444)
114+
_WD_Option('Driver', 'geckodriver.exe')
115+
_WD_Option('DriverParams', '--log trace')
116+
_WD_Option('Port', 4444)
114117

115118
$sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
116119
EndFunc
117120

118121
Func SetupChrome()
119-
_WDOption('Driver', 'chromedriver.exe')
120-
_WDOption('Port', 9515)
121-
_WDOption('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')
122+
_WD_Option('Driver', 'chromedriver.exe')
123+
_WD_Option('Port', 9515)
124+
_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')
122125

123126
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
124127
EndFunc

0 commit comments

Comments
 (0)