Skip to content

Commit 3a7921c

Browse files
authored
_WD_FrameEnter($sSession, 'null/2/0') - path support (#364)
1 parent 62260cc commit 3a7921c

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

wd_helper.au3

+50-14
Original file line numberDiff line numberDiff line change
@@ -538,36 +538,70 @@ EndFunc ;==>_WD_IsWindowTop
538538
; Description ...: Enter the specified frame.
539539
; Syntax ........: _WD_FrameEnter($sSession, $vIdentifier)
540540
; Parameters ....: $sSession - Session ID from _WD_CreateSession
541-
; $vIdentifier - Index (as 0-based Integer) or Element ID (as String) or Null (Keyword)
541+
; $vIdentifier - Target frame identifier. Can be any of the following:
542+
; |Null - Return to top-most browsing context
543+
; |String - Element ID from _WD_FindElement or path like 'null/2/0'
544+
; |Integer - 0-based index of frames
542545
; Return values .: Success - True.
543-
; Failure - WD Response error message (E.g. "no such frame") and sets @error to $_WD_ERROR_Exception
546+
; Failure - WD Response error message (E.g. "no such frame") and sets @error to one of the following values:
547+
; - $_WD_ERROR_Exception
548+
; - $_WD_ERROR_InvalidArgue
544549
; Author ........: Decibel
545-
; Modified ......: mLipok
546-
; Remarks .......: You can drill-down into nested frames by calling this function repeatedly with the correct parameters.
550+
; Modified ......: Danp2, mLipok, jchd
551+
; Remarks .......: You can drill-down into nested frames by calling this function repeatedly or use identifier like 'null/2/0'
547552
; Related .......: _WD_Window, _WD_LastHTTPResult
548553
; Link ..........:
549554
; Example .......: No
550555
; ===============================================================================================================================
551556
Func _WD_FrameEnter($sSession, $vIdentifier)
552557
Local Const $sFuncName = "_WD_FrameEnter"
553-
Local Const $sParameters = 'Parameters: Identifier=' & $vIdentifier
554-
Local $sOption, $sValue, $sResponse, $oJSON
558+
If String($vIdentifier) = 'null' Then $vIdentifier = Null ; String must be used because checking 0 = 'null' is True
559+
Local Const $bIsIdentifierNull = (IsKeyword($vIdentifier) = $KEYWORD_NULL)
560+
Local Const $sParameters = 'Parameters: Identifier=' & ($bIsIdentifierNull ? ("Null") : ($vIdentifier))
561+
Local $sValue, $sMessage = '', $sOption, $sResponse, $oJSON
555562
Local $iErr = $_WD_ERROR_Success
556563

557-
;*** Encapsulate the value if it's an integer, assuming that it's supposed to be an Index, not ID attrib value.
558-
If (IsKeyword($vIdentifier) = $KEYWORD_NULL) Then
564+
; must start with null or digit, must have at least one slash (may have many slashes but should not be followed one per other), must end with digit
565+
Local Const $bIdentifierAsPath = StringRegExp($vIdentifier, "(?i)\A(?:Null|\d+)(?:\/\d+)+\Z", $STR_REGEXPMATCH)
566+
567+
If $bIdentifierAsPath Then
568+
; will be processed below
569+
ElseIf $bIsIdentifierNull Then
559570
$sOption = '{"id":null}'
560571
ElseIf IsInt($vIdentifier) Then
561572
$sOption = '{"id":' & $vIdentifier & '}'
562573
Else
563-
$sOption = '{"id":' & __WD_JsonElement($vIdentifier) & '}'
574+
_WinAPI_GUIDFromString("{" & $vIdentifier & "}")
575+
If @error Then
576+
$iErr = $_WD_ERROR_InvalidArgue
577+
Else
578+
$sOption = '{"id":' & __WD_JsonElement($vIdentifier) & '}'
579+
EndIf
564580
EndIf
565581

566-
$sResponse = _WD_Window($sSession, "frame", $sOption)
582+
If $iErr = $_WD_ERROR_Success Then ; check if $vIdentifier was succesfully validated
583+
If Not $bIdentifierAsPath Then
584+
$sResponse = _WD_Window($sSession, "frame", $sOption)
585+
$iErr = @error
586+
Else
587+
Local $aIdentifiers = StringSplit($vIdentifier, '/')
588+
For $i = 1 To $aIdentifiers[0]
589+
If String($aIdentifiers[$i]) = 'null' Then
590+
$aIdentifiers[$i] = '{"id":null}'
591+
Else
592+
$aIdentifiers[$i] = '{"id":' & $aIdentifiers[$i] & '}'
593+
EndIf
594+
$sResponse = _WD_Window($sSession, "frame", $aIdentifiers[$i])
595+
If Not @error Then ContinueLoop
596+
597+
$iErr = @error
598+
$sMessage = ' Error on ID#' & $i & ' > ' & $aIdentifiers[$i]
599+
ExitLoop
600+
Next
601+
EndIf
602+
EndIf
567603

568-
If @error <> $_WD_ERROR_Success Then
569-
$iErr = $_WD_ERROR_Exception
570-
Else
604+
If $iErr = $_WD_ERROR_Success Then
571605
$oJSON = Json_Decode($sResponse)
572606
$sValue = Json_Get($oJSON, $_WD_JSON_Value)
573607

@@ -577,9 +611,11 @@ Func _WD_FrameEnter($sSession, $vIdentifier)
577611
Else
578612
$sValue = True
579613
EndIf
614+
ElseIf $iErr <> $_WD_ERROR_InvalidArgue Then
615+
$iErr = $_WD_ERROR_Exception
580616
EndIf
581617

582-
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters), 0, $sValue)
618+
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters & $sMessage), 0, $sValue)
583619
EndFunc ;==>_WD_FrameEnter
584620

585621
; #FUNCTION# ====================================================================================================================

0 commit comments

Comments
 (0)