WebDriver: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
Line 63: Line 63:
#include "WD_Helper.au3"
#include "WD_Helper.au3"


Local Enum $eFireFox = 0, _
Local $eFireFox = 0, $eChrome = 1
            $eChrome
Local $_TestType = $eFireFox
 
Local $aTestSuite[5][2] = [["TestTimeouts", False], ["TestNavigation", True], ["TestElements", False], ["TestScript", False], ["TestCookies", False]]
 
Local Const $_TestType = $eFireFox
Local $sDesiredCapabilities
Local $sDesiredCapabilities
Local $iIndex
Do
$_TestType = number(InputBox("WebDriver Installation Test", "Please select the Browser to use: " & $eFireFox & " = FireFox, " & $eChrome & " = Chrome",Default, "", 400, 130))
If @error Then Exit
Until $_TestType = $eFireFox Or $_TestType = $eChrome


$_WD_DEBUG = True
$_WD_DEBUG = True


Switch $_TestType
Switch $_TestType
    Case $eFireFox
Case $eFireFox
        SetupGecko()
SetupGecko()
 
Case $eChrome
    Case $eChrome
SetupChrome()
        SetupChrome()
 
EndSwitch
EndSwitch


_WD_Startup()
_WD_Startup()
ConsoleWrite("*** _WD_Startup: @error = " & @error & ", @extended = " & @extended & @CRLF)
ConsoleWrite("DesiredCapabilities: " & $sDesiredCapabilities & @CRLF)
$sSession = _WD_CreateSession($sDesiredCapabilities)
$sSession = _WD_CreateSession($sDesiredCapabilities)
ConsoleWrite("*** _WD_CreateSession: SessdionID = " & $sSession & ", @error = " & @error & ", @extended = " & @extended & @CRLF)
$sStatus = _WD_Status()
$sStatus = _WD_Status()
ConsoleWrite("*** _WD_Status: @error = " & @error & ", @extended = " & @extended & @CRLF)
_WD_Navigate($sSession, "http://google.com")
 
MsgBox(0, "WebDriver Installation", "Please check that a new Browser session has been started and google.com is being displayed!")
For $iIndex = 0 To UBound($aTestSuite, $UBOUND_ROWS) - 1
    If $aTestSuite[$iIndex][1] Then
        ConsoleWrite("Running: " & $aTestSuite[$iIndex][0] & @CRLF)
        Call($aTestSuite[$iIndex][0])
MsgBox("", 0, "Done ...")
    Else
;        ConsoleWrite("Bypass: " & $aTestSuite[$iIndex][0] & @CRLF)
    EndIf
Next
 
_WD_DeleteSession($sSession)
_WD_DeleteSession($sSession)
_WD_Shutdown()
_WD_Shutdown()
 
Exit
 
 
Func TestTimeouts()
    _WD_Timeouts($sSession)
    _WD_Timeouts($sSession, '{"script":10000,"pageLoad":200000,"implicit":30}')
    _WD_Timeouts($sSession)
EndFunc
 
Func TestNavigation()
    _WD_Navigate($sSession, "http://google.com")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    _WD_Action($sSession, "back")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    _WD_Action($sSession, "forward")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    ConsoleWrite("Title=" & _WD_Action($sSession, 'title') & @CRLF)
EndFunc
 
;_WD_Window($sSession, 'frame', '{"id":null}')
 
Func TestElements()
    _WD_Navigate($sSession, "http://google.com")
    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib1']")
 
    If @error = $_WD_ERROR_NoMatch Then
        $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
    EndIf
 
    $sElement2 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", '', True)
 
    _WD_ElementAction($sSession, $sElement, 'value', "testing 123")
    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'clear')
    _WD_ElementAction($sSession, $sElement, 'value', "abc xyz")
    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'clear')
    _WD_ElementAction($sSession, $sElement, 'value', "fujimo")
    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'click')
 
    _WD_ElementAction($sSession, $sElement, 'Attribute', 'test')
 
    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
    $sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')
 
    ConsoleWrite('value = ' & $sValue & @CRLF)
EndFunc
 
Func TestScript()
    _WD_ExecuteScript($sSession, 'alert()', "")
    _WD_Alert($sSession, 'Dismiss')
EndFunc
 
Func TestCookies()
    _WD_Navigate($sSession, "http://google.com")
    _WD_Cookies($sSession, 'Get', 'NID')
EndFunc
 


Func SetupGecko()
Func SetupGecko()
_WD_Option('Driver', 'geckodriver.exe')
_WD_Option('Driver', 'geckodriver.exe')
_WD_Option('DriverParams', '--log trace')
_WD_Option('DriverParams', '--log trace')
_WD_Option('Port', 4444)
_WD_Option('Port', 4444)
 
$sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
$sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko
EndFunc


Func SetupChrome()
Func SetupChrome()
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')
_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')
 
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
EndFunc   ;==>SetupChrome
EndFunc
</syntaxhighlight>
</syntaxhighlight>
The result displayed in the DOS window should look like:
The result displayed in the DOS window should look like:

Revision as of 18:44, 28 December 2019

This page is still a work in progress.

The W3C WebDriver API is a platform and language-neutral interface and wire protocol allowing programs or scripts to control the behavior of a web browser.

Introduction

WebDriver API

WebDriver enables developers to create automated tests that simulate user interaction. This is different from JavaScript unit tests because WebDriver has access to functionality and information that JavaScript running in the browser doesn't, and it can more accurately simulate user events or OS-level events. WebDriver can also manage testing across multiple windows, tabs and webpages in a single test session.

WebDriver UDF

The WebDriver UDF allows to interact with any browser that supports the W3C WebDriver specifications. Supporting multiple browsers via the same code base is now possible with just a few configuration settings.

Requirements

(Last modified: 2019/12/28)

The following UDFs need to be installed - independant of the Browser you try to automate:

One of the following Drivers needs to be installed - depending on the Browser type and version you try to automate:

Browser Download Link Latest Version / Date Comments
Chrome Google 80.0.3987.16 / 2019.12.19 Follow this link to select the correct version depending on the Chrome version you run!
Edge Microsoft 81.0.370.0
FireFox github 0.26 / 2019.10.12 Firefox version ≥ 60 is recommended

Note: You must still have the Microsoft Visual Studio redistributable runtime installed on your system for the binary to run. This is a known bug which the authors weren't able fix for this release.

Internet Explorer Microsoft

Limitations

Not all WebDriver functions have been implemented by each browser. To check the status, click on the below link for each respective browser:

Big Picture

How the browser independant and browser dependant parts fit together:

Big Picture - How everything fits together

Installation

(Last modified: 2019/12/28)

The following steps are needed to install everything you need to automate your browser:

  • Download the files listed in section "Requirements"
  • Move the UDFs to a directory where SciTE and Autoit can find them:
    • Json.au3 and BinaryCall.au3 from the JSON UDF
    • wd_Core.au3 and wd_helper.au3 from the WebDriver UDF
    • WinHttp.au3 and WinHttpConstants.au3 from the WinHttp UDF
  • Move the browser dependant WebDriver to the same directory:
    • chromedriver.exe for Chrome
    • geckodriver.exe for Firefox
    • msedgedriver.exe for Edge (Chromium) or MicrosoftWebDriver.exe for Edge (EdgeHTML)
  • Run the installation test script (WD_Installation_Test.au3) as described in the next section

Installation Test Script

Run the following script to validate the installation:

#include "WD_Core.au3"
#include "WD_Helper.au3"

Local $eFireFox = 0, $eChrome = 1
Local $_TestType = $eFireFox
Local $sDesiredCapabilities
Do
	$_TestType = number(InputBox("WebDriver Installation Test", "Please select the Browser to use: " & $eFireFox & " = FireFox, " & $eChrome & " = Chrome",Default, "", 400, 130))
	If @error Then Exit
Until $_TestType = $eFireFox Or $_TestType = $eChrome

$_WD_DEBUG = True

Switch $_TestType
	Case $eFireFox
		SetupGecko()
	Case $eChrome
		SetupChrome()
EndSwitch

_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
$sStatus = _WD_Status()
_WD_Navigate($sSession, "http://google.com")
MsgBox(0, "WebDriver Installation", "Please check that a new Browser session has been started and google.com is being displayed!")
_WD_DeleteSession($sSession)
_WD_Shutdown()
Exit

Func SetupGecko()
	_WD_Option('Driver', 'geckodriver.exe')
	_WD_Option('DriverParams', '--log trace')
	_WD_Option('Port', 4444)
	$sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko

Func SetupChrome()
	_WD_Option('Driver', 'chromedriver.exe')
	_WD_Option('Port', 9515)
	_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')
	$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
EndFunc   ;==>SetupChrome

The result displayed in the DOS window should look like:

1577552970329   geckodriver     DEBUG   Listening on 127.0.0.1:4444
1577552970703   webdriver::server       DEBUG   -> POST /session {"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}
1577552970704   webdriver::command      WARN    You are using deprecated legacy session negotiation patterns (desiredCapabilities/requiredCapabilities), see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#Legacy
1577552970763   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\xy\\AppData\\Local\\Temp\\rust_mozprofileFvQ4K8"
1577552971027   geckodriver::marionette DEBUG   Waiting 60s to connect to browser on 127.0.0.1:62360
...

Debugging

Debug the WebDriver setup

FireFox

(Last modified: 2019/12/28)

Problem Solution Reference
The installation testing script (_WD_Installation_Test.au3) does not start up FireFox and does not display the DOS window for geckodriver.
When you manually run the geckodriver in a DOS window you get message "geckodriver: error: An invalid argument was supplied. (os error 10022)"
Run the gecko driver from a local HDD. Stackoverflow

Debug your Script

References

(Last modified: 2019/12/28)

Further information sources: