Difference between revisions of "WebDriver"

From AutoIt Wiki
Jump to navigation Jump to search
(FAQ)
m (FAQ)
 
Line 98: Line 98:
  
 
== FAQ ==
 
== FAQ ==
<small>(Last modified: 2020/01/08)</small><br>
+
<small>(Last modified: 2020/01/16)</small><br>
  
 
<div class="usermessage mw-customtoggle-FAQ1">How to connect to a running browser instance</div>
 
<div class="usermessage mw-customtoggle-FAQ1">How to connect to a running browser instance</div>
Line 104: Line 104:
 
<div class="toccolours mw-collapsible-content">Q: How can I connect to a running browser instance?<br>A: That's described (for Firefox, but should work similar for other browsers) in this [https://www.autoitscript.com/forum/topic/201106-webdriver-udf-help-support-ii/?do=findComment&comment=1444173 post] or in the [https://github.com/Danp2/WebDriver/wiki/Connecting-to-existing-browser-instance Github wiki].</div>
 
<div class="toccolours mw-collapsible-content">Q: How can I connect to a running browser instance?<br>A: That's described (for Firefox, but should work similar for other browsers) in this [https://www.autoitscript.com/forum/topic/201106-webdriver-udf-help-support-ii/?do=findComment&comment=1444173 post] or in the [https://github.com/Danp2/WebDriver/wiki/Connecting-to-existing-browser-instance Github wiki].</div>
 
</div>
 
</div>
 +
 
<div class="usermessage mw-customtoggle-FAQ2">How to hide the webdriver console</div>
 
<div class="usermessage mw-customtoggle-FAQ2">How to hide the webdriver console</div>
 
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-FAQ2">
 
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-FAQ2">
 
<div class="toccolours mw-collapsible-content">Q: How can I hide the webdriver console?<br>A: The console can be completely hidden from the start by adding the following line near the beginning of your script:<br><syntaxhighlight lang="autoit">$_WD_DEBUG = $_WD_DEBUG_None ; You could also use $_WD_DEBUG_Error</syntaxhighlight>You can also control the visibility of the console with the function _WD_ConsoleVisible.</div>
 
<div class="toccolours mw-collapsible-content">Q: How can I hide the webdriver console?<br>A: The console can be completely hidden from the start by adding the following line near the beginning of your script:<br><syntaxhighlight lang="autoit">$_WD_DEBUG = $_WD_DEBUG_None ; You could also use $_WD_DEBUG_Error</syntaxhighlight>You can also control the visibility of the console with the function _WD_ConsoleVisible.</div>
 
</div>
 
</div>
 +
 
<div class="usermessage mw-customtoggle-FAQ3">How to utilize an existing user profile</div>
 
<div class="usermessage mw-customtoggle-FAQ3">How to utilize an existing user profile</div>
 
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-FAQ3">
 
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-FAQ3">
 
<div class="toccolours mw-collapsible-content">Q: Can I use an existing user profile instead of the default behavior of using a new one?<br>A: This is controlled by your "capabilities" declaration, with each browser using a different method to implement. Here are some examples --<br>
 
<div class="toccolours mw-collapsible-content">Q: Can I use an existing user profile instead of the default behavior of using a new one?<br>A: This is controlled by your "capabilities" declaration, with each browser using a different method to implement. Here are some examples --<br>
<br>'''Chrome'''<syntaxhighlight lang="autoit">$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}'</syntaxhighlight>'''Firefox'''<syntaxhighlight lang="autoit">$sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "C:/Users/AppData/Roaming/Mozilla/Firefox/Profiles/gbjaumrnd.default"],"log": {"level": "trace"}}}}}'</syntaxhighlight>You can also control the visibility of the console with the function _WD_ConsoleVisible.</div>
+
<br>'''Chrome'''<syntaxhighlight lang="autoit">$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}'</syntaxhighlight>'''Firefox'''<syntaxhighlight lang="autoit">$sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "C:/Users/AppData/Roaming/Mozilla/Firefox/Profiles/gbjaumrnd.default"],"log": {"level": "trace"}}}}}'</syntaxhighlight></div>
 
</div>
 
</div>
  

Latest revision as of 11:42, 16 January 2020

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/29)

The following UDFs need to be installed - independent 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 in version 0.26 which the authors weren't able fix for this release.

Internet Explorer Microsoft
Opera github 78.0.3904.87 - 2019.11.14 The versioning of OperaDriver matches the Chromium version on which Opera browser is based on.

Limitations

Not all WebDriver functions have been implemented by each browser. To check the status goto the corresponding website below:

Big Picture

How the browser independent and browser dependent parts fit together:

Big Picture - How everything fits together

Used Terms

(Last modified: 2020/01/03)

You will find the following terms when using WebDriver. We try to shed some light onto this subject here:

Firefox

Marionette
Marionette is an automation driver for Mozilla’s Gecko engine. It can remotely control either the UI or the internal JavaScript of a Gecko platform, such as Firefox. It can control both the chrome (i.e. menus and functions) or the content (the webpage loaded inside the browsing context), giving a high level of control and ability to replicate user actions. In addition to performing actions on the browser, Marionette can also read the properties and attributes of the DOM.
Marionette consists of two parts: a server which takes requests and executes them in Gecko (the Marionette server ships with Firefox), and a client (the Marionette client ships with the GeckoDriver exe). The client sends commands to the server and the server executes the command inside the browser.
For details please visit this site.

Installation

(Last modified: 2020/01/04)

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 dependent WebDriver to the same directory:
    • chromedriver.exe (Chrome)
    • geckodriver.exe (Firefox)
    • msedgedriver.exe (Edge - Chromium) or MicrosoftWebDriver.exe (Edge - EdgeHTML)
  • Run the installation test script (WD_Demo.au3) and select "DemoNavigation" to validate the installation.
    The result (for Firefox) displayed in the DOS window should be similar to the following:
1577745813519   geckodriver     DEBUG   Listening on 127.0.0.1:4444
1577745813744   webdriver::server       DEBUG   -> POST /session {"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}
1577745813746   geckodriver::capabilities       DEBUG   Trying to read firefox version from ini files
1577745813747   geckodriver::capabilities       DEBUG   Found version 71.0
1577745813757   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\xy\\AppData\\Local\\Temp\\rust_mozprofileaWURpD"
1577745813783   geckodriver::marionette DEBUG   Waiting 60s to connect to browser on 127.0.0.1:55184
1577745817392   geckodriver::marionette DEBUG   Connection to Marionette established on 127.0.0.1:55184.
1577745817464   webdriver::server       DEBUG   <- 200 OK {"value":{"sessionId":"925641bf-6c5d-4fe2-a985-02de9b1c7c74","capabilities":{"acceptInsecureCerts":true,"browserName":"firefox","browserVersion":"71.0","moz:accessibilityChecks":false,"moz:buildID":"20191202093317","moz:geckodriverVersion":"0.26.0","moz:headless":false,"moz:processID":50220,"moz:profile":"C:\\Users\\xy\\AppData\\Local\\Temp\\rust_mozprofileaWURpD","moz:shutdownTimeout":60000,"moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true,"pageLoadStrategy":"normal","platformName":"windows","platformVersion":"10.0","rotatable":false,"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify"}}}

Debugging

Debug the WebDriver setup

Firefox

(Last modified: 2019/12/28)

Problem Solution Reference
The installation testing script 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

FAQ

(Last modified: 2020/01/16)

How to connect to a running browser instance
Q: How can I connect to a running browser instance?
A: That's described (for Firefox, but should work similar for other browsers) in this post or in the Github wiki.
How to hide the webdriver console
Q: How can I hide the webdriver console?
A: The console can be completely hidden from the start by adding the following line near the beginning of your script:
$_WD_DEBUG = $_WD_DEBUG_None ; You could also use $_WD_DEBUG_Error
You can also control the visibility of the console with the function _WD_ConsoleVisible.
How to utilize an existing user profile
Q: Can I use an existing user profile instead of the default behavior of using a new one?
A: This is controlled by your "capabilities" declaration, with each browser using a different method to implement. Here are some examples --

Chrome
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}'
Firefox
$sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "C:/Users/AppData/Roaming/Mozilla/Firefox/Profiles/gbjaumrnd.default"],"log": {"level": "trace"}}}}}'

Tools

The following tools will help you to automate your browser:

  • ChroPath plugin: Makes finding an element by XPath, ID or CSS incredibly easy (Chrome, Firefox, Opera)

References

(Last modified: 2019/12/31)

Further information sources: