Jump to content

Recommended Posts

Posted

As for now this UDF' has only 300 lines and is workable.
I am sure I can shorten it even more.
 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 9/5/2021 at 8:15 PM, Danp2 said:

I wonder if you could build this with the JSON UDF that is already part of the Webdriver UDF?

Expand  

I even try to do it some time later earlier, but I give up.
Currently I back to this project and start to think as a parsing string to get knowledge about JSON and WebDriver ... to organize it in my head.

I can again try to move to JSON UDF.
Maybe in the comming week.

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

so ... you ask about version which use JSON.au3 ....

this is first version:

#include <WinAPICom.au3>
#include "..\wd_helper.au3"

Global $Obj
Global $CURRENT_MATCH_IDX = -1
Global Enum _
        $_WD_CAPS__MATCH__Type, _
        $_WD_CAPS__MATCH__FirstIdx, _
        $_WD_CAPS__MATCH__CAPS_IDX, _
        $_WD_CAPS__MATCH__Specyfic, _
        $_WD_CAPS__MATCH__OPTS_IDX, _
        $_WD_CAPS__MATCH__ARGS_IDX, _
        $_WD_CAPS__MATCH__PREFS_IDX, _
        $_WD_CAPS__MATCH__LOG_IDX, _
        $_WD_CAPS__MATCH__ENV_IDX, _
        $_WD_CAPS__MATCH__COUNTER

Global $_WD_CAPS__API[0][$_WD_CAPS__MATCH__COUNTER]

_Example()
Exit

#Region - examples
Func _Example()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
;~  _Example_Danp2()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)

    If $IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", _
            "Do you want to test with running browsers ?") Then
        Local $b_Headless = ($IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", "Do you want to test with headless mode ?"))
        _Example_Chrome(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    Else ; only show $s_Desired_Capabilities in console
        _Example_Chrome(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    EndIf
EndFunc   ;==>_Example

Func _Example_Chrome($b_Run_Browser, $b_Headless = False)
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('always', 'chrome')
    _WD_Capabilities_Capability('browserName', 'chrome')
;~  _WD_Capabilities_Option('w3c', True)
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    _WD_Capabilities_Argument('start-maximized')
    _WD_Capabilities_Argument('disable-infobars')
    _WD_Capabilities_Argument('user-data-dir', 'C:\Users\' & @UserName & '\AppData\Local\Google\Chrome\User Data\Default')
    _WD_Capabilities_Preference('download.default_directory', @ScriptDir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_Chrome

Func _Example_FireFox($b_Run_Browser, $b_Headless = False)
    #forceref $b_Run_Browser, $b_Headless
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('always', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    #TODO timeouts
;~  _WD_Capabilities_Capability('timeouts', '{"implicit": 4, "script": 300, "pageLoad": 30000}')
    #TODO proxy
;~  _WD_Capabilities_Capability('proxy', '{"proxyType": 4, "script": 300, "pageLoad": 30000}')
    #TODO 'binary'
;~  _WD_Capabilities_Option('binary', 'c:\Program Files (x86)\Mozilla Firefox\firefox.exe')
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    Local $s_My_Profile_Dir = 'c:\Users\' & @UserName & '\AppData\Local\Mozilla\Firefox\Profiles\TESTING.default'
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_Preference('dom.ipc.processCount', 8)
    _WD_Capabilities_Preference('javascript.options.showInConsole', False)
    _WD_Capabilities_Preference('browser.toolbars.bookmarks.visibility', 'always') ; check    about:config
    _WD_Capabilities_Preference('app.update.download.attempts', 0) ; check    about:config
    _WD_Capabilities_Preference('browser.safebrowsing.downloads.enabled', False) ; check    about:config

    _WD_Capabilities_Log('level', 'trace')

    _WD_Capabilities_Env('MOZ_LOG', 'nsHttp:5')
    _WD_Capabilities_Env('MOZ_LOG_FILE', $s_My_Profile_Dir & '\log')

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_FireFox

Func _Example_Danp2()
    Local $s_My_Profile_Dir = @ScriptDir & '\WD_Testing_Profile'
;~  _WD_Capabilities_Startup()
;~  _WD_Capabilities_CreateObject('always', 'firefox')
;~  _WD_Capabilities_Capability('browserName', 'firefox')
;~  _WD_Capabilities_Capability('acceptInsecureCerts', True)
;~  _WD_Capabilities_Argument('-profile')
;~  _WD_Capabilities_Argument($s_My_Profile_Dir)
;~  Local $s_Desired_Capabilities = _WD_Capabilities_Build('firefox')

    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('always', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    Return

    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_Danp2
#EndRegion - examples

#Region - UDF Functions

Func _WD_Capabilities_Startup()
    $Obj = ''
EndFunc   ;==>_WD_Capabilities_Startup

Func _WD_Capabilities_CreateObject($s_MatchType, $s_Browser_type = '') ; $s_MatchType = 'always' Or 'first'
    #Region - parameters validation
    If $s_MatchType <> 'always' And $s_MatchType <> 'first' Then _
            Return SetError(1)

    Local $s_SpecificOption_KeyName = ''
    If StringInStr($s_Browser_type, 'chrome') Then
        $s_SpecificOption_KeyName = 'goog:chromeOptions'
    ElseIf StringInStr($s_Browser_type, 'firefox') Then
        $s_SpecificOption_KeyName = 'moz:firefoxOptions'
    ElseIf StringInStr($s_Browser_type, 'edge') Then
        $s_SpecificOption_KeyName = 'ms:edgeOptions'
    Else
        Return SetError(1)
    EndIf
    #EndRegion - parameters validation

    #Region - reindexing API
    Local $i_API_Recent_Size = UBound($_WD_CAPS__API)

    Local $i_API_New_Size = $i_API_Recent_Size + 1
    ReDim $_WD_CAPS__API[$i_API_New_Size][$_WD_CAPS__MATCH__COUNTER]
    Local $i_API_New_IDX = $i_API_New_Size - 1
    #EndRegion - reindexing API

    #Region - new "MATCH" Initialization
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Type] = $s_MatchType & 'Match'
    If $s_MatchType = 'first' Then
        Local Static $i_FirstMatch_Counter = -1
        $i_FirstMatch_Counter += 1
        $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__FirstIdx] = $i_FirstMatch_Counter
    EndIf
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__CAPS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Specyfic] = $s_SpecificOption_KeyName
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__OPTS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ARGS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__PREFS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__LOG_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ENV_IDX] = -1
    $CURRENT_MATCH_IDX = $i_API_New_IDX ; set API Size as current API IDX
    #EndRegion - new "MATCH" Initialization

    Return $CURRENT_MATCH_IDX ; return current API IDX
EndFunc   ;==>_WD_Capabilities_CreateObject

Func _WD_Capabilities_Capability($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__CAPS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($Obj, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Capability

Func _WD_Capabilities_Option($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__OPTS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($Obj, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Option

Func _WD_Capabilities_Argument($key, $value = '')
;~  If $key = '-profile' Then $value = StringReplace($value, '\', '/')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ARGS_IDX)
    If $value Then $key &= '=' & $value
    Json_Put($Obj, $s_Notation, $key)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Argument

Func _WD_Capabilities_Preference($key, $value = '')
;~  If $key = '-profile' Then $value = StringReplace($value, '\', '/')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__PREFS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($Obj, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Preference

Func _WD_Capabilities_Log($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__LOG_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($Obj, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Log

Func _WD_Capabilities_Env($key, $value = '')
;~  If $key = 'MOZ_LOG_FILE' Then $value = StringReplace($value, '\', '\\')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ENV_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($Obj, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Env

Func __WD_Capabilities_JSON_Notation($i_BUILDER_TYPE)
    Local $s_CurrentMatch_Type = '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Type] & ']'
    If $s_CurrentMatch_Type = '[firstMatch]' Then
        $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__FirstIdx] = +1
        $s_CurrentMatch_Type &= '["' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__FirstIdx] & '"]'
    EndIf

    Local $s_SpecificOption_KeyName = '["' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Specyfic] & '"]'

    Local $s_Notation = ''
    Switch $i_BUILDER_TYPE
        Case $_WD_CAPS__MATCH__CAPS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__PREFS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type
        Case $_WD_CAPS__MATCH__OPTS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__OPTS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName
        Case $_WD_CAPS__MATCH__ARGS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS_IDX] += 1
            Local $i_Current_Arg = $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS_IDX]
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[args][' & $i_Current_Arg & ']'
        Case $_WD_CAPS__MATCH__PREFS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__PREFS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[prefs]'
        Case $_WD_CAPS__MATCH__LOG_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__LOG_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[log]'
        Case $_WD_CAPS__MATCH__ENV_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ENV_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[env]'
    EndSwitch
;~  ConsoleWrite("- " & @ScriptLineNumber & ' $s_Notation =' & $s_Notation & @CRLF)
    Return $s_Notation
EndFunc   ;==>__WD_Capabilities_JSON_Notation

#EndRegion - UDF Functions

#Region - Helper

Func _WD_Capabilities_GetPretty()
    Local $Data2 = Json_Encode($Obj)
    Local $Data1 = Json_Decode($Data2)
    Local $Json2 = Json_Encode($Data1, $Json_UNQUOTED_STRING)
;~  ConsoleWrite("- " & $Json2 & @CRLF)
    Local $Data3 = Json_Decode($Json2)

    Local $Json3 = Json_Encode($Data3, $Json_PRETTY_PRINT, "    ", ",\n", ",\n", ":")
    Return $Json3
EndFunc   ;==>_WD_Capabilities_GetPretty

Func _WD_Capabilities_ToConsole($s_Comment)
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON START: ' & $s_Comment & @LF)
    ConsoleWrite(_WD_Capabilities_GetPretty())
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON END: ' & $s_Comment & @LF & @LF)
EndFunc   ;==>_WD_Capabilities_ToConsole
#EndRegion - Helper

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

now take a look on _Example_Danp2()
There is used "firstMatch"  twice !

#include <WinAPICom.au3>
#include "..\wd_helper.au3"

Global $_WD_CAPS__OBJECT
Global $CURRENT_MATCH_IDX = -1
Global Enum _
        $_WD_CAPS__MATCH__Type, _
        $_WD_CAPS__MATCH__FirstIdx, _
        $_WD_CAPS__MATCH__CAPS_IDX, _
        $_WD_CAPS__MATCH__Specyfic, _
        $_WD_CAPS__MATCH__OPTS_IDX, _
        $_WD_CAPS__MATCH__ARGS_IDX, _
        $_WD_CAPS__MATCH__PREFS_IDX, _
        $_WD_CAPS__MATCH__LOG_IDX, _
        $_WD_CAPS__MATCH__ENV_IDX, _
        $_WD_CAPS__MATCH__COUNTER

Global $_WD_CAPS__API[0][$_WD_CAPS__MATCH__COUNTER]

_Example()
Exit

#Region - examples
Func _Example()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    _Example_Danp2()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    Return

    If $IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", _
            "Do you want to test with running browsers ?") Then
        Local $b_Headless = ($IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", "Do you want to test with headless mode ?"))
        _Example_Chrome(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    Else ; only show $s_Desired_Capabilities in console
        _Example_Chrome(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    EndIf
EndFunc   ;==>_Example

Func _Example_Chrome($b_Run_Browser, $b_Headless = False)
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'chrome')
    _WD_Capabilities_Capability('browserName', 'chrome')
;~  _WD_Capabilities_Option('w3c', True)
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    _WD_Capabilities_Argument('start-maximized')
    _WD_Capabilities_Argument('disable-infobars')
    _WD_Capabilities_Argument('user-data-dir', 'C:\Users\' & @UserName & '\AppData\Local\Google\Chrome\User Data\Default')
    _WD_Capabilities_Preference('download.default_directory', @ScriptDir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_Chrome

Func _Example_FireFox($b_Run_Browser, $b_Headless = False)
    #forceref $b_Run_Browser, $b_Headless
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    #TODO timeouts
;~  _WD_Capabilities_Capability('timeouts', '{"implicit": 4, "script": 300, "pageLoad": 30000}')
    #TODO proxy
;~  _WD_Capabilities_Capability('proxy', '{"proxyType": 4, "script": 300, "pageLoad": 30000}')
    #TODO 'binary'
;~  _WD_Capabilities_Option('binary', 'c:\Program Files (x86)\Mozilla Firefox\firefox.exe')
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    Local $s_My_Profile_Dir = 'c:\Users\' & @UserName & '\AppData\Local\Mozilla\Firefox\Profiles\TESTING.default'
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_Preference('dom.ipc.processCount', 8)
    _WD_Capabilities_Preference('javascript.options.showInConsole', False)
    _WD_Capabilities_Preference('browser.toolbars.bookmarks.visibility', 'always') ; check    about:config
    _WD_Capabilities_Preference('app.update.download.attempts', 0) ; check    about:config
    _WD_Capabilities_Preference('browser.safebrowsing.downloads.enabled', False) ; check    about:config

    _WD_Capabilities_Log('level', 'trace')

    _WD_Capabilities_Env('MOZ_LOG', 'nsHttp:5')
    _WD_Capabilities_Env('MOZ_LOG_FILE', $s_My_Profile_Dir & '\log')

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_FireFox

Func _Example_Danp2()
    Local $s_My_Profile_Dir = @ScriptDir & '\WD_Testing_Profile'

    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_CreateObject('firstMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)

    _WD_Capabilities_CreateObject('firstMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    Return

    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Desired_Capabilities)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_Danp2
#EndRegion - examples

#Region - UDF Functions

Func _WD_Capabilities_Startup()
    $_WD_CAPS__OBJECT = ''
EndFunc   ;==>_WD_Capabilities_Startup

Func _WD_Capabilities_CreateObject($s_MatchType, $s_Browser_type = '') ; $s_MatchType = 'alwaysMatch' Or 'firstMatch'
    #Region - parameters validation
    If $s_MatchType <> 'alwaysMatch' And $s_MatchType <> 'firstMatch' Then _
            Return SetError(1)

    Local $s_SpecificOption_KeyName = ''
    If StringInStr($s_Browser_type, 'chrome') Then
        $s_SpecificOption_KeyName = 'goog:chromeOptions'
    ElseIf StringInStr($s_Browser_type, 'firefox') Then
        $s_SpecificOption_KeyName = 'moz:firefoxOptions'
    ElseIf StringInStr($s_Browser_type, 'edge') Then
        $s_SpecificOption_KeyName = 'ms:edgeOptions'
    Else
        Return SetError(1)
    EndIf
    #EndRegion - parameters validation

    #Region - reindexing API
    Local $i_API_Recent_Size = UBound($_WD_CAPS__API)

    Local $i_API_New_Size = $i_API_Recent_Size + 1
    ReDim $_WD_CAPS__API[$i_API_New_Size][$_WD_CAPS__MATCH__COUNTER]
    Local $i_API_New_IDX = $i_API_New_Size - 1
    #EndRegion - reindexing API

    #Region - new "MATCH" Initialization
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Type] = $s_MatchType
    If $s_MatchType = 'firstMatch' Then
        Local Static $i_FirstMatch_Counter = -1
        $i_FirstMatch_Counter += 1
        $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__FirstIdx] = $i_FirstMatch_Counter
    EndIf
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__CAPS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Specyfic] = $s_SpecificOption_KeyName
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__OPTS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ARGS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__PREFS_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__LOG_IDX] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ENV_IDX] = -1
    $CURRENT_MATCH_IDX = $i_API_New_IDX ; set API Size as current API IDX
    #EndRegion - new "MATCH" Initialization

    Return $CURRENT_MATCH_IDX ; return current API IDX
EndFunc   ;==>_WD_Capabilities_CreateObject

Func _WD_Capabilities_Capability($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__CAPS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Capability

Func _WD_Capabilities_Option($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__OPTS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Option

Func _WD_Capabilities_Argument($key, $value = '')
;~  If $key = '-profile' Then $value = StringReplace($value, '\', '/')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ARGS_IDX)
    If $value Then $key &= '=' & $value
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $key)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Argument

Func _WD_Capabilities_Preference($key, $value = '')
;~  If $key = '-profile' Then $value = StringReplace($value, '\', '/')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__PREFS_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Preference

Func _WD_Capabilities_Log($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__LOG_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Log

Func _WD_Capabilities_Env($key, $value = '')
;~  If $key = 'MOZ_LOG_FILE' Then $value = StringReplace($value, '\', '\\')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ENV_IDX)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
    ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Env

Func __WD_Capabilities_JSON_Notation($i_BUILDER_TYPE)
    Local $s_CurrentMatch_Type = '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Type] & ']'
    If $s_CurrentMatch_Type = '[firstMatch]' Then
        $s_CurrentMatch_Type &= '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__FirstIdx] & ']'
    EndIf

    Local $s_SpecificOption_KeyName = '["' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Specyfic] & '"]'

    Local $s_Notation = ''
    Switch $i_BUILDER_TYPE
        Case $_WD_CAPS__MATCH__CAPS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__PREFS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type
        Case $_WD_CAPS__MATCH__OPTS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__OPTS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName
        Case $_WD_CAPS__MATCH__ARGS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS_IDX] += 1
            Local $i_Current_Arg = $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS_IDX]
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[args][' & $i_Current_Arg & ']'
        Case $_WD_CAPS__MATCH__PREFS_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__PREFS_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[prefs]'
        Case $_WD_CAPS__MATCH__LOG_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__LOG_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[log]'
        Case $_WD_CAPS__MATCH__ENV_IDX
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ENV_IDX] += 1
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOption_KeyName & '[env]'
    EndSwitch
;~  ConsoleWrite("- " & @ScriptLineNumber & ' $s_Notation =' & $s_Notation & @CRLF)
    Return $s_Notation
EndFunc   ;==>__WD_Capabilities_JSON_Notation

#EndRegion - UDF Functions

#Region - Helper

Func _WD_Capabilities_GetPretty()
    Local $Data2 = Json_Encode($_WD_CAPS__OBJECT)
    Local $Data1 = Json_Decode($Data2)
    Local $Json2 = Json_Encode($Data1, $Json_UNQUOTED_STRING)
;~  ConsoleWrite("- " & $Json2 & @CRLF)
    Local $Data3 = Json_Decode($Json2)

    Local $Json3 = Json_Encode($Data3, $Json_PRETTY_PRINT, "    ", ",\n", ",\n", ":")
    Return $Json3
EndFunc   ;==>_WD_Capabilities_GetPretty

Func _WD_Capabilities_ToConsole($s_Comment)
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON START: ' & $s_Comment & @LF)
    ConsoleWrite(_WD_Capabilities_GetPretty())
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON END: ' & $s_Comment & @LF & @LF)
EndFunc   ;==>_WD_Capabilities_ToConsole
#EndRegion - Helper

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

using:

 

Func _Example_Danp2()
    Local $s_My_Profile_Dir = @ScriptDir & '\WD_Testing_Profile'

    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)

    _WD_Capabilities_CreateObject('firstMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()
    ....
    ....
    ....
EndFunc

I get such JSON:
 

{
    "capabilities":{
        "alwaysMatch":{
            "browserName":"firefox",
            "acceptInsecureCerts":true,
            "moz:firefoxOptions":{
                "args":[
                    "-profile",
                    "Z:\\!!!_SVN_AU3\\UDF_Forum\\Other_Members\\Test\\WD_Testing_Profile"
                ]
            }
        },
        "firstMatch":[
            {
                "browserName":"test",
                "acceptInsecureCerts":true
            }
        ]
    }
}

and such result:

  Quote

__WD_Post: StatusCode=400; ResponseText={"value":{"error":"invalid argument","message":"firstMatch key shadowed a value in alwaysMatch","sta...
__WD_Post ==> Invalid argument: {"value":{"error":"invalid argument","message":"firstMatch key shadowed a value in alwaysMatch","stacktrace":""}}
_WD_CreateSession: {"value":{"error":"invalid argument","message":"firstMatch key shadowed a value in alwaysMatch","stacktrace":""}}
 

Expand  

Do you have any thoughts in this regard?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 9/6/2021 at 11:16 AM, mLipok said:

Do you have any thoughts in this regard?

Expand  

You can't have the following in AlwaysMatch if you are also going to place it in FirstMatch --

"browserName":"firefox",
"acceptInsecureCerts":true

With the FirstMatch section calling for "browserName":"test", it can never combine with the AlwaysMatch settings of "browserName":"firefox". I haven't tested this, but I hope you will get the idea --

Func _Example_Danp2()
    Local $s_My_Profile_Dir = @ScriptDir & '\WD_Testing_Profile'

    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', '')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    
    _WD_Capabilities_CreateObject('firstMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($s_My_Profile_Dir)
 
     _WD_Capabilities_CreateObject('firstMatch', 'chrome')
    _WD_Capabilities_Capability('browserName', 'chrome')
    ....
    ....
    ....    
    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Desired_Capabilities = _WD_Capabilities_GetPretty()

EndFunc

 

Posted (edited)

I think now this is quite good version, slim, fast, small, workable.

#include <MsgBoxConstants.au3>
#include "..\wd_helper.au3"
Global Enum _
        $_WD_CAPS__MATCH__Type, _
        $_WD_CAPS__MATCH__FirstIdx, _
        $_WD_CAPS__MATCH__CAPS, _
        $_WD_CAPS__MATCH__Specyfic, _
        $_WD_CAPS__MATCH__OPTS, _
        $_WD_CAPS__MATCH__ARGS, _
        $_WD_CAPS__MATCH__PREFS, _
        $_WD_CAPS__MATCH__LOG, _
        $_WD_CAPS__MATCH__ENV, _
        $_WD_CAPS__MATCH__COUNTER
Global $_WD_CAPS__API[0][$_WD_CAPS__MATCH__COUNTER]
Global $_WD_CAPS__OBJECT
Global $CURRENT_MATCH_IDX = -1

#Region - _wd_Capabilities.au3 UDF - examples
Global Const $_FIREFOX_DRIVER = @ScriptDir & '\..\geckodriver.exe' ; CHANGE TO PROPER FILE FULL PATH
Global Const $_CHROME_DRIVER = @ScriptDir & '\..\chromedriver.exe' ; CHANGE TO PROPER FILE FULL PATH

Global Const $_FIREFOX_PROFILE = @LocalAppDataDir & '\Mozilla\Firefox\Profiles\WD_Testing_Profile' ; CHANGE TO PROPER DIRECTORY PATH
Global Const $_CHROME_PRFILE = @LocalAppDataDir & '\Google\Chrome\User Data\Default' ; CHANGE TO PROPER DIRECTORY PATH

_Example()
Exit

Func _Example()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    _Example_Danp2()
    ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)

    If $IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", _
            "Do you want to test with running browsers ?") Then
        Local $b_Headless = ($IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", "Do you want to test with headless mode ?"))
        _Example_Chrome(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(True, $b_Headless)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    Else ; only show $s_Capabilities_JSON in console
        _Example_Chrome(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
        _Example_FireFox(False)
        ConsoleWrite("! " & @ScriptLineNumber & " ===============" & @CRLF)
    EndIf
EndFunc   ;==>_Example

Func _Example_Chrome($b_Run_Browser, $b_Headless = False)
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'chrome')
    _WD_Capabilities_Capability('browserName', 'chrome')
;~  _WD_Capabilities_Option('w3c', True)
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    _WD_Capabilities_Argument('start-maximized')
    _WD_Capabilities_Argument('disable-infobars')
    _WD_Capabilities_Argument('user-data-dir', $_CHROME_PRFILE)
    _WD_Capabilities_Preference('download.default_directory', @ScriptDir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Capabilities_JSON = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', $_CHROME_DRIVER)
    _WD_Option('Port', 9515)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Capabilities_JSON)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_Chrome

Func _Example_FireFox($b_Run_Browser, $b_Headless = False)
    #forceref $b_Run_Browser, $b_Headless
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    #TODO timeouts
;~  _WD_Capabilities_Capability('timeouts', '{"implicit": 4, "script": 300, "pageLoad": 30000}')
    #TODO proxy
;~  _WD_Capabilities_Capability('proxy', '{"proxyType": 4, "script": 300, "pageLoad": 30000}')
    #TODO 'binary'
;~  _WD_Capabilities_Option('binary', 'c:\Program Files (x86)\Mozilla Firefox\firefox.exe')
    If $b_Headless Then _
            _WD_Capabilities_Argument('--headless')
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($_FIREFOX_PROFILE)

    _WD_Capabilities_Preference('dom.ipc.processCount', 8)
    _WD_Capabilities_Preference('javascript.options.showInConsole', False)
    _WD_Capabilities_Preference('browser.toolbars.bookmarks.visibility', 'always') ; check    about:config
    _WD_Capabilities_Preference('app.update.download.attempts', 0) ; check    about:config
    _WD_Capabilities_Preference('browser.safebrowsing.downloads.enabled', False) ; check    about:config

    _WD_Capabilities_Log('level', 'trace')

    _WD_Capabilities_Env('MOZ_LOG', 'nsHttp:5')
    _WD_Capabilities_Env('MOZ_LOG_FILE', @LocalAppDataDir & '\Mozilla\Firefox\Profiles\WD_Testing_Profile\log')

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Capabilities_JSON = _WD_Capabilities_GetPretty()
    If Not $b_Run_Browser Then Return

    _WD_Option('Driver', $_FIREFOX_DRIVER)
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Capabilities_JSON)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Example_FireFox

Func _Example_Danp2()
    _WD_Capabilities_Startup()
    _WD_Capabilities_CreateObject('alwaysMatch', 'firefox')
    _WD_Capabilities_Capability('browserName', 'firefox')
    _WD_Capabilities_Capability('acceptInsecureCerts', True)
    _WD_Capabilities_Argument('-profile')
    _WD_Capabilities_Argument($_FIREFOX_PROFILE)

    _WD_Capabilities_CreateObject('firstMatch', 'chrome')
    #TODO the next line if uncomented then fires error:   "firstMatch key shadowed a value in alwaysMatch"
;~  _WD_Capabilities_Capability('browserName', 'chrome')
    _WD_Capabilities_Argument('start-maximized')
    _WD_Capabilities_Argument('disable-infobars')
    _WD_Capabilities_Argument('user-data-dir', $_CHROME_PRFILE)
    _WD_Capabilities_Preference('download.default_directory', @ScriptDir)

    _WD_Capabilities_ToConsole(@ScriptLineNumber)
    Local $s_Capabilities_JSON = _WD_Capabilities_GetPretty()

    _WD_Option('Driver', $_FIREFOX_DRIVER)
    _WD_Option('Port', 4444)
    _WD_Startup()
    Local $WD_SESSION = _WD_CreateSession($s_Capabilities_JSON)
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
    _WD_DeleteSession($WD_SESSION)
    _WD_Shutdown()

    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "END OF _Example_Danp2()")
EndFunc   ;==>_Example_Danp2
#EndRegion - _wd_Capabilities.au3 UDF - examples

#Region - _wd_Capabilities.au3 UDF - core and internal functions

Func _WD_Capabilities_Startup()
    $_WD_CAPS__OBJECT = ''
EndFunc   ;==>_WD_Capabilities_Startup

Func _WD_Capabilities_CreateObject($s_MatchType, $s_Browser_type = '') ; $s_MatchType = 'alwaysMatch' Or 'firstMatch'
    #Region - parameters validation
    If $s_MatchType <> 'alwaysMatch' And $s_MatchType <> 'firstMatch' Then _
            Return SetError(1)

    Local $s_SpecificOptions_KeyName = ''
    If StringInStr($s_Browser_type, 'chrome') Then
        $s_SpecificOptions_KeyName = 'goog:chromeOptions'
    ElseIf StringInStr($s_Browser_type, 'firefox') Then
        $s_SpecificOptions_KeyName = 'moz:firefoxOptions'
    ElseIf StringInStr($s_Browser_type, 'edge') Then
        $s_SpecificOptions_KeyName = 'ms:edgeOptions'
    Else
        Return SetError(2)
    EndIf
    #EndRegion - parameters validation

    #Region - reindexing API
    Local $i_API_Recent_Size = UBound($_WD_CAPS__API), $i_API_New_Size = $i_API_Recent_Size + 1, $i_API_New_IDX = $i_API_New_Size - 1
    ReDim $_WD_CAPS__API[$i_API_New_Size][$_WD_CAPS__MATCH__COUNTER]
    #EndRegion - reindexing API

    #Region - new "MATCH" Initialization
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Type] = $s_MatchType
    If $s_MatchType = 'firstMatch' Then
        Local Static $i_FirstMatch_Counter = -1
        $i_FirstMatch_Counter += 1
        $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__FirstIdx] = $i_FirstMatch_Counter
    EndIf
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__CAPS] = Null
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__Specyfic] = $s_SpecificOptions_KeyName
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__OPTS] = Null
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ARGS] = -1
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__PREFS] = Null
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__LOG] = Null
    $_WD_CAPS__API[$i_API_New_IDX][$_WD_CAPS__MATCH__ENV] = Null
    $CURRENT_MATCH_IDX = $i_API_New_IDX ; set last API IDX as CURRENT API IDX
    #EndRegion - new "MATCH" Initialization

    Return $CURRENT_MATCH_IDX ; return current API IDX
EndFunc   ;==>_WD_Capabilities_CreateObject

Func _WD_Capabilities_Capability($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__CAPS)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Capability

Func _WD_Capabilities_Option($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__OPTS)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Option

Func _WD_Capabilities_Argument($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ARGS)
    If $value Then $key &= '=' & $value
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $key)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Argument

Func _WD_Capabilities_Preference($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__PREFS)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Preference

Func _WD_Capabilities_Log($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__LOG)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Log

Func _WD_Capabilities_Env($key, $value = '')
    Local $s_Notation = __WD_Capabilities_JSON_Notation($_WD_CAPS__MATCH__ENV)
    $s_Notation &= '[' & $key & ']'
    Json_Put($_WD_CAPS__OBJECT, $s_Notation, $value)
;~  If Not @compiled Then ConsoleWrite("> $s_Notation - " & $s_Notation & @CRLF)
EndFunc   ;==>_WD_Capabilities_Env

Func __WD_Capabilities_JSON_Notation($i_BUILDER_TYPE)
    Local $s_CurrentMatch_Type = '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Type] & ']'
    If $s_CurrentMatch_Type = '[firstMatch]' Then
        $s_CurrentMatch_Type &= '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__FirstIdx] & ']'
    EndIf

    Local $s_SpecificOptions_KeyName = '["' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Specyfic] & '"]'

    Local $s_Notation = ''
    Switch $i_BUILDER_TYPE
        Case $_WD_CAPS__MATCH__CAPS
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type
        Case $_WD_CAPS__MATCH__OPTS
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOptions_KeyName
        Case $_WD_CAPS__MATCH__ARGS
            $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS] += 1
            Local $i_Current_Arg = $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__ARGS]
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOptions_KeyName & '[args][' & $i_Current_Arg & ']'
        Case $_WD_CAPS__MATCH__PREFS
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOptions_KeyName & '[prefs]'
        Case $_WD_CAPS__MATCH__LOG
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOptions_KeyName & '[log]'
        Case $_WD_CAPS__MATCH__ENV
            $s_Notation = '[capabilities]' & $s_CurrentMatch_Type & $s_SpecificOptions_KeyName & '[env]'
    EndSwitch
;~  If Not @compiled Then ConsoleWrite("- IFNC: " & @ScriptLineNumber & ' $s_Notation =' & $s_Notation & @CRLF)
    Return $s_Notation
EndFunc   ;==>__WD_Capabilities_JSON_Notation

#EndRegion - _wd_Capabilities.au3 UDF - core and internal functions

#Region - _wd_Capabilities.au3 UDF - helper functions
Func _WD_Capabilities_GetPretty()
    Local $Data2 = Json_Encode($_WD_CAPS__OBJECT)
    Local $Data1 = Json_Decode($Data2)
    Local $Json2 = Json_Encode($Data1, $Json_UNQUOTED_STRING)
;~  ConsoleWrite("- " & $Json2 & @CRLF)
    Local $Data3 = Json_Decode($Json2)

    Local $Json3 = Json_Encode($Data3, $Json_PRETTY_PRINT, "    ", ",\n", ",\n", ":")
    Return $Json3
EndFunc   ;==>_WD_Capabilities_GetPretty

Func _WD_Capabilities_ToConsole($s_Comment)
    If @Compiled Then Return ; because of GDRP reason do not throw nothing to console when compiled script
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON START: ' & $s_Comment & @LF)
    ConsoleWrite(_WD_Capabilities_GetPretty())
    ConsoleWrite(@LF & '! _WD_Capabilities: JSON END: ' & $s_Comment & @LF & @LF)
EndFunc   ;==>_WD_Capabilities_ToConsole
#EndRegion - _wd_Capabilities.au3 UDF - helper functions

Of course, there are still some things to do that have been marked as #TODO
but....

If no one raises any concerns by the end of the weeks, the early beta phase can be considered complete.
Then I will bring the project to GitHub and can start documenting it through the function headers.
Before I do it, however, I will add an example for MS Edge.

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

This is causing a runtime error for me --

_WD_Capabilities_Startup()
   _WD_Capabilities_CreateObject('alwaysMatch')
   _WD_Capabilities_Capability('acceptInsecureCerts', True)

Console output --

@@ Debug(117) : $i_BUILDER_TYPE = 2
@@ Debug(118) : $_WD_CAPS__MATCH__Type = 0
@@ Debug(119) : $CURRENT_MATCH_IDX = -1
"C:\Users\DanPollak\Dropbox\webdriver\wd_capabilities.au3" (121) : ==> Variable subscript badly formatted.:
Local $s_CurrentMatch_Type = '[' & $_WD_CAPS__API[$CURRENT_MATCH_IDX][$_WD_CAPS__MATCH__Type] & ']'
Local $s_CurrentMatch_Type = '[' & $_WD_CAPS__API[^ ERROR
->09:02:31 AutoIt3.exe ended.rc:1

 

Posted
  On 9/3/2021 at 11:53 AM, Danp2 said:

How would you build something like this? 

  • {"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}

 

Expand  

ah... still the same issue:
I forgot about a case when you do not want to specify any Specific/Vendor capabilities
 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

new version under spoiler:

  Reveal hidden contents

 

Currently function:   _Example_Danp2()
Creates such Capabilities JSON string

{
    "capabilities":{
        "alwaysMatch":{
            "acceptInsecureCerts":true
        },
        "firstMatch":[
            {
                "browserName":"chrome",
                "goog:chromeOptions":{
                    "args":[
                        "start-maximized",
                        "disable-infobars",
                        "user-data-dir=C:\\Users\\Szef\\AppData\\Local\\Google\\Chrome\\User Data\\Default",
                        "--profile-directory=Default"
                    ],
                    "prefs":{
                        "download.default_directory":"Z:\\!!!_SVN_AU3\\UDF_Forum\\Other_Members\\Test"
                    }
                }
            },
            {
                "browserName":"firefox",
                "moz:firefoxOptions":{
                    "args":[
                        "-profile",
                        "C:\\Users\\Szef\\AppData\\Local\\Mozilla\\Firefox\\Profiles"
                    ]
                }
            }
        ]
    }
}

using this particular JSON string I was able to add new feature:
 

....
    ....
    ....
    Local $_CHOOSEN_DRIVER = ''
    If $IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", _
            "Wchich browser do you wan to use ?" & @CRLF & @CRLF & _
            "[YES] = FireFox    [NO] = Chrome") Then
        $_CHOOSEN_DRIVER = $_FIREFOX_DRIVER
    Else
        $_CHOOSEN_DRIVER = $_CHROME_DRIVER
    EndIf
    ....
    ....
    ....
    _WD_Option('Driver', $_CHOOSEN_DRIVER)
    If $_CHOOSEN_DRIVER = $_FIREFOX_DRIVER Then _WD_Option('Port', 4444)
    If $_CHOOSEN_DRIVER = $_CHROME_DRIVER Then _WD_Option('Port', 9515)
    _WD_Startup()
    ....
    ....

This shows how to use different Browsers with the same Capabilities JSON string.

:)

 

Edited by mLipok
typo + wording

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

I am thinking of changing the function name _WD_Capabilities_CreateObject() to _WD_Capabilities_CreateGroup()  as or just _WD_Capabilities_Group() eventually _WD_Capabilities_Type()

Because IMHO Capabilites must be grouped in one of two group of capabilites settings "alwaysMatch" or "firstMatch".
"alwaysMatch" may contain only one JSON Capabilities object
"alwaysMatch" contains JSON ARRAY which can contain many JSON Capabilities object

This is the way how currently I understand the Capabilities.
Please correct me if I'm wrong.

 

Edited by mLipok
wording

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

Reading here:

https://www.w3.org/TR/webdriver/#dfn-table-of-standard-capabilities

About "timeouts" and "proxy" capability settings I propose to do something like this:
 

; https://www.w3.org/TR/webdriver/#dfn-proxy-configuration
    _WD_Capabilities_CapabilityProxy('proxyType', ...)
    _WD_Capabilities_CapabilityProxy('proxyAutoconfigUrl', ...)
    _WD_Capabilities_CapabilityProxy('ftpProxy', ...)
    _WD_Capabilities_CapabilityProxy('noProxy', "...|...|...|...")
    _WD_Capabilities_CapabilityProxy('sslProxy', ...)
    _WD_Capabilities_CapabilityProxy('socksProxy', ...)
    _WD_Capabilities_CapabilityProxy('socksVersion', ...)

and 

; https://www.w3.org/TR/webdriver/#timeouts
    _WD_Capabilities_Capability('timeouts', 'implicit|4|script|300|pageLoad|30000')

What you think about ?

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

As to using excludeswitches or any prefs with list of strings I prospose

    _WD_Capabilities_Capability('excludeSwitches', 'disable-popup-blocking')
    _WD_Capabilities_Capability('excludeSwitches', 'enable-automation')
    _WD_Capabilities_Capability('excludeSwitches', 'load-extension')


References:
https://chromedriver.chromium.org/capabilities
https://stackoverflow.com/a/58694548/5314940
 

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

new version in the following spoiler:
 

  Reveal hidden contents

Here is function list

Core functions:
    _WD_Capabilities_Startup()
    _WD_Capabilities_Add()
    _WD_Capabilities_GetJson()

    Yep, there is no more need to use _WD_Capabilities_Argument()  _WD_Capabilities_Preference() etc.....

    Internal functions:
    __WD_Capabilities_CreateObject()
    __WD_Capabilities_Switch_Parameters()
    __WD_Capabilities_JSON_Notation()

    Helper Functions:
    _WD_Capabilities_GetPrettyJson()
    _WD_Capabilities_JsonToConsole()
    _WD_Capabilities_ApiToConsole()
    _WD_Capabilities_ApiDisplay()

 

What I add?


Support for:

  • MS Edge
  • 'timeouts'
  • 'proxy'
  • 'excludeSwitches'

There is also one better configurable example.

Take a look and have fun.
I am waiting for your feedback.

btw.
We are getting closer to the first version without beta stage.
 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

On the way to the official release:
https://github.com/mlipok/_wd_capabilities.au3

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 9/8/2021 at 2:53 PM, mLipok said:

Here is function list

Expand  

Here are a few small suggestions --

  • Standardize capitalization of acronyms. Is it "JSON" or "Json"? My recommendation is all caps, including "API" instead of "Api"
  • Develop a consistent naming convention. You have a tendency to throw in extra underscores in some instances. For example, __WD_Capabilities_Switch_Parameters should be __WD_Capabilities_SwitchParameters so that it matches the format of __WD_Capabilities_CreateObject.
  • Removing the underscore after _WD_Capabilities would better match the naming convention used in the main WD UDF 😉
Posted (edited)
  On 9/14/2021 at 12:13 PM, Danp2 said:
  • Standardize capitalization of acronyms. Is it "JSON" or "Json"? My recommendation is all caps, including "API" instead of "Api"
  • Develop a consistent naming convention. You have a tendency to throw in extra underscores in some instances. For example, __WD_Capabilities_Switch_Parameters should be __WD_Capabilities_SwitchParameters so that it matches the format of __WD_Capabilities_CreateObject.
Expand  

Done.

  On 9/14/2021 at 12:13 PM, Danp2 said:

Removing the underscore after _WD_Capabilities would better match the naming convention used in the main WD UDF

Expand  

My approach is like Objective programing:
      wd.capabilities.startup  >>> _WD_Capabilities_Startup

In  my current versionFetching info...  I'm working on , there are:

Func _WD_Capabilities_Startup()
Func _WD_Capabilities_Add($key, $value1 = '', $value2 = '')
Func _WD_Capabilities_GetJSON()
Func _WD_Capabilities_GetPrettyJSON()
Func _WD_Capabilities_JSONToConsole($s_Comment)
Func _WD_Capabilities_APIToConsole($s_Comment)
Func _WD_Capabilities_APIDisplay($s_Comment)

Func __WD_Capabilities_CreateObject($s_MatchType, $s_Browser_type = '') ; $s_MatchType = 'alwaysMatch' Or 'firstMatch'
Func __WD_Capabilities_SwitchParameters(ByRef $key, ByRef $value1, ByRef $value2)
Func __WD_Capabilities_JSONNotation($i_BUILDER_TYPE)


This is intentionally naming convention to separate this functions as a specific subgroup of function in relation to _WD_ ***** functions, because they are only used to initialize parameters called "Capabilities" in WebDrive terminology. Also for this reason, they are in seeparate UDF.

Considering my explanations, does this somehow hinder the consistency of the entire WebDrive UDF package?

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I want to understand you well, so I will ask the question:
What you mean saying:

  Quote

However, my opinion on the final underscore hasn't changed.

Expand  

especially "final underscore" .

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Guest
This topic is now closed to further replies.
×
×
  • Create New...