Skip to content

Commit 028dd08

Browse files
mlipokDanp2
andauthored
_WD_ElementSelectAction() - 'options' speed up (#165)
* options using JavaScript * Correct initial error handling Co-authored-by: Dan Pollak <danpollak2@gmail.com>
1 parent 1bb4088 commit 028dd08

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

wd_helper.au3

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ EndFunc ;==>_WD_ElementOptionSelect
980980
; $sSelectElement - Element ID of Select element from _WD_FindElement
981981
; $sCommand - Action to be performed. Can be one of the following:
982982
; |OPTIONS - Retrieve array containing value / label attributes from the Select element's options
983-
; |VALUE - Retrieve current value
983+
; |VALUE - Retrieve current value
984984
; Return values .: Success - Requested data returned by web driver.
985985
; Failure - "" (empty string) and sets @error to one of the following values:
986986
; - $_WD_ERROR_NoMatch
@@ -997,44 +997,37 @@ EndFunc ;==>_WD_ElementOptionSelect
997997
; ===============================================================================================================================
998998
Func _WD_ElementSelectAction($sSession, $sSelectElement, $sCommand)
999999
Local Const $sFuncName = "_WD_ElementSelectAction"
1000-
Local $sNodeName, $sJsonElement, $vResult
1001-
Local $sText, $aOptions
1000+
Local $sNodeName, $vResult
10021001

10031002
$sNodeName = _WD_ElementAction($sSession, $sSelectElement, 'property', 'nodeName')
10041003
Local $iErr = @error
10051004

1006-
If $iErr = $_WD_ERROR_Success And $sNodeName = 'select' Then
1007-
1008-
Switch $sCommand
1009-
Case 'value'
1010-
; Retrieve current value of designated Select element
1011-
$sJsonElement = __WD_JsonElement($sSelectElement)
1012-
$vResult = _WD_ExecuteScript($sSession, "return arguments[0].value", $sJsonElement, Default, $_WD_JSON_Value)
1013-
$iErr = @error
1014-
1015-
Case 'options'
1016-
; Retrieve array containing value / label attributes from the Select element's options
1017-
$aOptions = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "./option", $sSelectElement, True)
1018-
$iErr = @error
1019-
1020-
If $iErr = $_WD_ERROR_Success Then
1021-
$sText = ""
1022-
For $sElement In $aOptions
1023-
$sJsonElement = __WD_JsonElement($sElement)
1024-
$sText &= (($sText <> "") ? @CRLF : "") & _WD_ExecuteScript($sSession, "return arguments[0].value + '|' + arguments[0].label", $sJsonElement, Default, $_WD_JSON_Value)
1025-
$iErr = @error
1026-
Next
1027-
1028-
Local $aOut[0][2]
1029-
_ArrayAdd($aOut, $sText, 0, Default, @CRLF, 1)
1030-
$vResult = $aOut
1031-
EndIf
1032-
Case Else
1033-
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_InvalidDataType, "(Value|Options) $sCommand=>" & $sCommand), 0, "")
1005+
If $iErr = $_WD_ERROR_Success Then
1006+
If $sNodeName = 'select' Then
1007+
Switch $sCommand
1008+
Case 'value'
1009+
; Retrieve current value of designated Select element
1010+
$vResult = _WD_ExecuteScript($sSession, "return arguments[0].value", __WD_JsonElement($sSelectElement), Default, $_WD_JSON_Value)
1011+
$iErr = @error
1012+
1013+
Case 'options'
1014+
; Retrieve array containing value / label attributes from the Select element's options
1015+
Local $sScript = "var result =''; var options = arguments[0].options; for (let i = 0; i < options.length; i++) {result += options[i].value + '|' + options[i].label + '\n'} return result;"
1016+
$vResult = _WD_ExecuteScript($sSession, $sScript, __WD_JsonElement($sSelectElement), Default, $_WD_JSON_Value)
1017+
$iErr = @error
1018+
If $iErr = $_WD_ERROR_Success Then
1019+
Local $sText = StringStripWS($vResult, $STR_STRIPTRAILING)
1020+
Local $aOut[0][2]
1021+
_ArrayAdd($aOut, $sText, 0, Default, @LF, 1)
1022+
$vResult = $aOut
1023+
EndIf
1024+
Case Else
1025+
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_InvalidDataType, "(Value|Options) $sCommand=>" & $sCommand), 0, "")
10341026

1035-
EndSwitch
1036-
Else
1037-
$iErr = $_WD_ERROR_InvalidArgue
1027+
EndSwitch
1028+
Else
1029+
$iErr = $_WD_ERROR_InvalidArgue
1030+
EndIf
10381031
EndIf
10391032

10401033
If $_WD_DEBUG = $_WD_DEBUG_Info Then

0 commit comments

Comments
 (0)