WebDriver Capabilities: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
 
(32 intermediate revisions by 2 users not shown)
Line 3: Line 3:
= WebDriver Capabilities =
= WebDriver Capabilities =
What are Capabilities?<p>
What are Capabilities?<p>
A good description can be found [https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities here]:<br>
A good description can be found [https://www.selenium.dev/documentation/legacy/desired_capabilities/ ]:<br>
''Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user requests that a session support. If a session cannot support a capability that is requested in the desired capabilities, no error is thrown; a read-only capabilities object is returned that indicates the capabilities the session actually supports.''
''Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user requests that a session support. If a session cannot support a capability that is requested in the desired capabilities, no error is thrown; a read-only capabilities object is returned that indicates the capabilities the session actually supports.''


* Capabilities are options that you can use to customize and configure a Browser session
* Capabilities are options that you can use to customize and configure a Browser session
* The capabilities you want to set are passed as a formatted string to function _WD_CreateSession
* The capabilities you want to set are passed as a formatted string to function _WD_CreateSession
* The WebDriver UDF uses the Capabilities class to pass the capabilities string to the WebDriver Exe
* The WebDriver UDF uses the Capabilities class to pass the capabilities string to the WebDriver exe file
* Format and content of the capabilities string differ from Browser to Browser
* Format and content of the capabilities string differ from Browser to Browser
* Some capabilities are read-only, some are read-write
* Some capabilities are read-only, some are read-write
* There are [https://www.w3.org/TR/webdriver/#capabilities standard capabilities] each implementation must support. An implementation may define additional extension capabilities
* Some capabilities are [https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#legacy_capabilities deprecated] and should be avoided.


Take a look at remark about [https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#legacy_capabilities Legacy capabilities].
== Chrome related documentations ==
Link to the [https://chromedriver.chromium.org/capabilities Documentation of Capabilities for Chrome].


== Chrome ==
Link to the [https://peter.sh/experiments/chromium-command-line-switches/ List of Chromium Command Line Switches].
Link to the documentation of Capabilities for [https://chromedriver.chromium.org/capabilities Chrome].


Link to [https://peter.sh/experiments/chromium-command-line-switches/ List of Chromium Command Line Switches].
== Firefox related documentations ==
Link to the [https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions Documentation of Capabilities for Firefox].


=== Examples ===
== Edge related documentations ==
<syntaxhighlight lang="autoit">
Link to the [https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options Documentation of Capabilities for Edge].
$sCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"]}}}}'
 
</syntaxhighlight>
== Desired/Required capabilities ==
In many sources there are names such as: '''desiredCapabilities''' and '''requiredCapabilities'''.


== Firefox ==
These are old nomenclatures for old solutions.
Link to the documentation of Capabilities for [https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions Firefox].


=== Examples ===
`[https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#legacy_capabilities Some drivers support these legacy capabilities, but they are deprecated and should be avoided.]`
<syntaxhighlight lang="autoit">
$sCapabilities = '{"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}'
</syntaxhighlight>


== Edge ==
Currently '''Capabilities''' should be used.
Link to the documentation of Capabilities for [https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options Edge].


=== Examples ===
For more details take a look on:
<syntaxhighlight lang="autoit">
* [https://www.selenium.dev/documentation/legacy/desired_capabilities/ Legacy Selenium Desired Capabilities].
$sCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"excludeSwitches": [ "enable-automation"]}}}}'
* [https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#legacy_capabilities Legacy capabilities]
</syntaxhighlight>


= WD_Capabilities UDF =
= WD_Capabilities UDF =
examples with the full capabilities string and the WD_Capabilites functions to create it
Creating a correct Capabilities string can become quite complex. Debugging problems as well.<br>
That's why the WD_Capabilities.au3 UDF has been created. It makes sure the created Capabilities string is perfectly formatted all the time.
The WD_Capabilities.au3 UDF is part of [https://github.com/Danp2/WebDriver AutoIt WebDriver sets of UDF].


== Chrome Examples ==
== Chrome Examples ==
To get the following Capabilities string
<syntaxhighlight lang="autoit">
Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"]}}}}'
</syntaxhighlight>
you have to call this sequence of functions:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
_WD_CapabilitiesStartup()
_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd('alwaysMatch', 'chrome')
_WD_CapabilitiesAdd("alwaysMatch", "chrome")
_WD_CapabilitiesAdd('w3c', True)
_WD_CapabilitiesAdd("w3c", True)
_WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")
Local $sCapabilities = _WD_CapabilitiesGet()
Local $sCapabilities = _WD_CapabilitiesGet()
</syntaxhighlight>
</syntaxhighlight>


== Firefox Examples ==
== Firefox Examples ==
To get the following Capabilities string
<syntaxhighlight lang="autoit">
Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}'
</syntaxhighlight>
you have to call this sequence of functions:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
_WD_CapabilitiesStartup()
_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd('alwaysMatch', 'firefox')
_WD_CapabilitiesAdd("alwaysMatch", "firefox")
_WD_CapabilitiesAdd('browserName', 'firefox')
_WD_CapabilitiesAdd("browserName", "firefox")
_WD_CapabilitiesAdd('acceptInsecureCerts', True)
_WD_CapabilitiesAdd("acceptInsecureCerts", True)
Local $sCapabilities = _WD_CapabilitiesGet()
Local $sCapabilities = _WD_CapabilitiesGet()
</syntaxhighlight>
</syntaxhighlight>


== Edge Examples ==
== Edge Examples ==
To get the following Capabilities string
<syntaxhighlight lang="autoit">
Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"excludeSwitches": [ "enable-automation"]}}}}'
</syntaxhighlight>
you have to call this sequence of functions:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
_WD_CapabilitiesStartup()
_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd('alwaysMatch', 'edge')
_WD_CapabilitiesAdd("alwaysMatch", "edge")
_WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")
Local $sCapabilities = _WD_CapabilitiesGet()
Local $sCapabilities = _WD_CapabilitiesGet()
</syntaxhighlight>
</syntaxhighlight>


== Capabilities comlpex example ==
== A complete working example ==
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <MsgBoxConstants.au3>
#include <MsgBoxConstants.au3>
Line 77: Line 91:
#include "wd_helper.au3"
#include "wd_helper.au3"


_Example()
_Example_UsingCapabilitesWithChrome()
If @error Then ConsoleWrite("! ---> @error=" & @error & "  @extended=" & @extended & _
If @error Then ConsoleWrite("! ---> @error=" & @error & "  @extended=" & @extended & _
" : Error launching browser" & @CRLF)
" : Error launching browser" & @CRLF)


Func _Example_UsingCapabilitesWithChrome()
#Region - creating capabilities string
; startup - cleaning internal capabilities JSON object
_WD_CapabilitiesStartup()
; starting with "alwaysMatch" settings
_WD_CapabilitiesAdd("alwaysMatch")
_WD_CapabilitiesAdd("acceptInsecureCerts", True)


Func _Example()
; switching to "firstMatch" with "chrome" which will specify to use "goog:chromeOptions"
Local $WD_SESSION = _WD_SetupChrome()
_WD_CapabilitiesAdd("firstMatch", "chrome")
If @error Then Return SetError(@error, @extended, $WD_SESSION)


MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
; setting vendor specific "goog:chromeOptions" settings
_WD_DeleteSession($WD_SESSION)
_WD_CapabilitiesAdd("w3c", True)
_WD_Shutdown()
_WD_CapabilitiesAdd("prefs", "download.default_directory", @ScriptDir & "\Downloads")
EndFunc  ;==>_Example
_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")


; dump Capabilities string to Console - reason: only to check how it looks
_WD_CapabilitiesDump(@ScriptLineNumber & " Testing")


Func _WD_SetupChrome($s_DriverLocation = @ScriptDir & '\chromedriver.exe', $b_Headless = False, $s_Download_dir = Default, $_WD_DEBUG_LEVEL = Default, $s_Log_FileFullPath = Default)
; get/assign Capabilities JSON Object content to string
If $_WD_DEBUG_LEVEL = Default Then
Local $sCapabilities = _WD_CapabilitiesGet()
$_WD_DEBUG = $_WD_DEBUG_Error
#EndRegion - creating capabilities string
If Not @Compiled Then $_WD_DEBUG = $_WD_DEBUG_Info
EndIf


_WD_Option('Driver', $s_DriverLocation)
; setting WebDriver options
_WD_Option('Port', 9515)
_WD_Option("Driver", @ScriptDir & "\chromedriver.exe")
_WD_Option('DefaultTimeout', 1000)
_WD_Option("Port", 9515)
_WD_Option("Sleep", _My_Check_State)
_WD_Option("DefaultTimeout", 1000)
_WD_Option("DriverParams", "--log-path=" & Chr(34) & @ScriptDir & "\WebDriver_Testing.log" & Chr(34))


If $s_Download_dir = Default Then $s_Download_dir = @ScriptDir & '\WD_Download'
; starting WebDriver with specified options
$s_Download_dir = ''
_WD_Startup()
If @error Then Return SetError(@error, @extended, "")


Local $bTest = _WD_Validate_Browser_Driver('chrome', 'chromedriver.exe')
; trying to create Session - connecting to WebDriver with specified capabilities
If Not $bTest Then Return SetError(@error, @extended, $bTest)
Local $WD_SESSION = _WD_CreateSession($sCapabilities)
If @error Then Return SetError(@error, @extended, $WD_SESSION)


If $s_Log_FileFullPath = Default Then $s_Log_FileFullPath = @ScriptDir & '\Chrome-Testing.log'
; this MsgBox is placed here only to have possibility to manually check if all is fine with Chrome instance
If IsString($s_Log_FileFullPath) And StringLen(($s_Log_FileFullPath)) Then
If Not @Compiled Then MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")
If Not @Compiled Then ConsoleWrite(@ScriptLineNumber & ' WebDriver: Log Path = ' & $s_Log_FileFullPath & @CRLF)
_WD_Option('DriverParams', '--log-path=' & '"' & $s_Log_FileFullPath & '"')
EndIf


_WD_CapabilitiesStartup()
; delete/close session - disconnecting AutoIt script from running WebDriver process
_WD_CapabilitiesAdd('alwaysMatch')
_WD_DeleteSession($WD_SESSION)
_WD_CapabilitiesAdd('acceptInsecureCerts', True)
_WD_CapabilitiesAdd('platformName', 'windows')
_WD_CapabilitiesAdd('firstMatch', 'chrome')
_WD_CapabilitiesAdd('browserName', 'chrome')
_WD_CapabilitiesAdd('w3c', True)
_WD_CapabilitiesAdd('args', 'user-data-dir', 'C:\Users\' & @UserName & '\AppData\Local\Google\Chrome\User Data\WD_Testing')
_WD_CapabilitiesAdd('args', 'user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win' & StringReplace(@OSArch, 'X', '') & '; ' & @CPUArch & ') AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' & _WD_GetBrowserVersion('chrome') & ' Safari/537.36')
_WD_CapabilitiesAdd('args', '--profile-directory', Default)
_WD_CapabilitiesAdd('args', 'start-maximized')
_WD_CapabilitiesAdd('args', 'disable-infobars')
_WD_CapabilitiesAdd('args', '--no-sandbox')
_WD_CapabilitiesAdd('args', '--disable-blink-features=AutomationControlled')
_WD_CapabilitiesAdd('args', '--disable-web-security')
_WD_CapabilitiesAdd('args', '--allow-running-insecure-content')
_WD_CapabilitiesAdd('args', '--ignore-certificate-errors')
If $b_Headless Then _
_WD_CapabilitiesAdd('args', '--headless')
If $s_Download_dir Then _
_WD_CapabilitiesAdd('prefs', 'download.default_directory', $s_Download_dir)
_WD_CapabilitiesAdd('excludeSwitches', 'disable-popup-blocking')
_WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
_WD_CapabilitiesAdd('excludeSwitches', 'load-extension')


_WD_CapabilitiesDump(@ScriptLineNumber & ' test 1')
; close WebDriver process
Local $s_Capabilities = _WD_CapabilitiesGet()
_WD_Shutdown()
EndFunc  ;==>_Example_UsingCapabilitesWithChrome
</syntaxhighlight>


_WD_Startup()
This example generates and uses Capabilities string as follows:
If @error Then Return SetError(@error, @extended, '')
<syntaxhighlight lang="json">
If Not @Compiled Then ConsoleWrite(@ScriptLineNumber & ' WebDriver: StartUp via chrome' & @CRLF)
{
 
    "capabilities":{
Local $WD_SESSION = _WD_CreateSession($s_Capabilities)
        "alwaysMatch":{
Return SetError(@error, @extended, $WD_SESSION)
            "acceptInsecureCerts":true
EndFunc  ;==>_WD_SetupChrome
        },
        "firstMatch":[
            {
                "goog:chromeOptions":{
                    "w3c":true,
                    "prefs":{
                        "download.default_directory":"C:\\Testing\\WD_Download"
                    },
                    "excludeSwitches":[
                        "enable-automation"
                    ]
                }
            }
        ]
    }
}
</syntaxhighlight>


Func _WD_Validate_Browser_Driver($s_Browser_name, $s_Driver_name)
= Additional sources of information =
For Additional sources of information take a look on this following links:


Local $s_GetBrowserVersion = _WD_GetBrowserVersion($s_Browser_name)
* https://w3c.github.io/webdriver/#capabilities
If @error Then Return SetError(@error, @extended, $s_GetBrowserVersion)
* https://github.com/Danp2/au3WebDriver/blob/master/wd_capabilities.au3
If Not @Compiled Then ConsoleWrite("Browser version = " & $s_GetBrowserVersion & @CRLF)
* https://docs.yugabyte.com/preview/api/ysql/datatypes/type_json/primitive-and-compound-data-types
Local $s_GetBrowserVersion_main_number = Number(StringLeft($s_GetBrowserVersion, 2))
* https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities
 
* https://chromedriver.chromium.org/capabilities
Local $s_GetWebDriverVersion = _WD_GetWebDriverVersion(@ScriptDir, $s_Driver_name)
* https://www.selenium.dev/documentation/webdriver/capabilities/shared/
If @error Then Return SetError(@error, @extended, $s_GetWebDriverVersion)
* https://www.selenium.dev/documentation/webdriver/capabilities/internet_explorer/
If Not @Compiled Then ConsoleWrite("WEBDRIVER version = " & $s_GetWebDriverVersion & @CRLF)
* https://github.com/SeleniumHQ/seleniumhq.github.io/tree/dev/website_and_docs/content/documentation/webdriver/capabilities
Local $s_GetWebDriverVersion_main_number = Number(StringLeft($s_GetWebDriverVersion, 2))
*
 
Local $bTest = ($s_GetBrowserVersion_main_number = $s_GetWebDriverVersion_main_number)
Return SetError($s_GetBrowserVersion_main_number, $s_GetWebDriverVersion_main_number, $bTest)
EndFunc  ;==>_WD_Validate_Browser_Driver
 
Func _My_Check_State($iSleep)
If Not @Compiled Then ConsoleWrite('Sleeping ' & $iSleep & ' ms' & @CRLF)
Sleep($iSleep)
EndFunc  ;==>_My_Check_State
</syntaxhighlight>

Latest revision as of 23:17, 9 April 2022

This page is still a work in progress.

WebDriver Capabilities

What are Capabilities?

A good description can be found [1]:
Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user requests that a session support. If a session cannot support a capability that is requested in the desired capabilities, no error is thrown; a read-only capabilities object is returned that indicates the capabilities the session actually supports.

  • Capabilities are options that you can use to customize and configure a Browser session
  • The capabilities you want to set are passed as a formatted string to function _WD_CreateSession
  • The WebDriver UDF uses the Capabilities class to pass the capabilities string to the WebDriver exe file
  • Format and content of the capabilities string differ from Browser to Browser
  • Some capabilities are read-only, some are read-write
  • There are standard capabilities each implementation must support. An implementation may define additional extension capabilities
  • Some capabilities are deprecated and should be avoided.

Chrome related documentations

Link to the Documentation of Capabilities for Chrome.

Link to the List of Chromium Command Line Switches.

Firefox related documentations

Link to the Documentation of Capabilities for Firefox.

Edge related documentations

Link to the Documentation of Capabilities for Edge.

Desired/Required capabilities

In many sources there are names such as: desiredCapabilities and requiredCapabilities.

These are old nomenclatures for old solutions.

`Some drivers support these legacy capabilities, but they are deprecated and should be avoided.`

Currently Capabilities should be used.

For more details take a look on:

* Legacy Selenium Desired Capabilities.
* Legacy capabilities

WD_Capabilities UDF

Creating a correct Capabilities string can become quite complex. Debugging problems as well.
That's why the WD_Capabilities.au3 UDF has been created. It makes sure the created Capabilities string is perfectly formatted all the time. The WD_Capabilities.au3 UDF is part of AutoIt WebDriver sets of UDF.

Chrome Examples

To get the following Capabilities string

Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"]}}}}'

you have to call this sequence of functions:

_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd("alwaysMatch", "chrome")
_WD_CapabilitiesAdd("w3c", True)
_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")
Local $sCapabilities = _WD_CapabilitiesGet()

Firefox Examples

To get the following Capabilities string

Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}'

you have to call this sequence of functions:

_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd("alwaysMatch", "firefox")
_WD_CapabilitiesAdd("browserName", "firefox")
_WD_CapabilitiesAdd("acceptInsecureCerts", True)
Local $sCapabilities = _WD_CapabilitiesGet()

Edge Examples

To get the following Capabilities string

Local $sCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"excludeSwitches": [ "enable-automation"]}}}}'

you have to call this sequence of functions:

_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd("alwaysMatch", "edge")
_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")
Local $sCapabilities = _WD_CapabilitiesGet()

A complete working example

#include <MsgBoxConstants.au3>

#include "wd_capabilities.au3"
#include "wd_helper.au3"

_Example_UsingCapabilitesWithChrome()
If @error Then ConsoleWrite("! ---> @error=" & @error & "  @extended=" & @extended & _
		" : Error launching browser" & @CRLF)

Func _Example_UsingCapabilitesWithChrome()
	#Region - creating capabilities string
	; startup - cleaning internal capabilities JSON object
	_WD_CapabilitiesStartup()
	; starting with "alwaysMatch" settings
	_WD_CapabilitiesAdd("alwaysMatch")
	_WD_CapabilitiesAdd("acceptInsecureCerts", True)

	; switching to "firstMatch" with "chrome" which will specify to use "goog:chromeOptions"
	_WD_CapabilitiesAdd("firstMatch", "chrome")

	; setting vendor specific "goog:chromeOptions" settings
	_WD_CapabilitiesAdd("w3c", True)
	_WD_CapabilitiesAdd("prefs", "download.default_directory", @ScriptDir & "\Downloads")
	_WD_CapabilitiesAdd("excludeSwitches", "enable-automation")

	; dump Capabilities string to Console - reason: only to check how it looks
	_WD_CapabilitiesDump(@ScriptLineNumber & " Testing")

	; get/assign Capabilities JSON Object content to string
	Local $sCapabilities = _WD_CapabilitiesGet()
	#EndRegion - creating capabilities string

	; setting WebDriver options
	_WD_Option("Driver", @ScriptDir & "\chromedriver.exe")
	_WD_Option("Port", 9515)
	_WD_Option("DefaultTimeout", 1000)
	_WD_Option("DriverParams", "--log-path=" & Chr(34) & @ScriptDir & "\WebDriver_Testing.log" & Chr(34))

	; starting WebDriver with specified options
	_WD_Startup()
	If @error Then Return SetError(@error, @extended, "")

	; trying to create Session - connecting to WebDriver with specified capabilities
	Local $WD_SESSION = _WD_CreateSession($sCapabilities)
	If @error Then Return SetError(@error, @extended, $WD_SESSION)

	; this MsgBox is placed here only to have possibility to manually check if all is fine with Chrome instance
	If Not @Compiled Then MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, "Information #" & @ScriptLineNumber, "Waiting before _WD_Shutdown()")

	; delete/close session - disconnecting AutoIt script from running WebDriver process
	_WD_DeleteSession($WD_SESSION)

	; close WebDriver process
	_WD_Shutdown()
EndFunc   ;==>_Example_UsingCapabilitesWithChrome

This example generates and uses Capabilities string as follows:

{
    "capabilities":{
        "alwaysMatch":{
            "acceptInsecureCerts":true
        },
        "firstMatch":[
            {
                "goog:chromeOptions":{
                    "w3c":true,
                    "prefs":{
                        "download.default_directory":"C:\\Testing\\WD_Download"
                    },
                    "excludeSwitches":[
                        "enable-automation"
                    ]
                }
            }
        ]
    }
}

Additional sources of information

For Additional sources of information take a look on this following links: