TA Basics: Selenium Webdrivers

Intro 

In one of my previous blog posts, I talked about a high level view of the whole test automation setup. It happens quiet often that people that begin with test automation forget to install the webdriver or are working with an older version of the webdriver. So in this post I wanted to go over this specific part and show some examples when testing local, with selenium-grid or via appium.


What are webdrivers?

In short I'd say: "Webdrivers are the bridge between your test automation framework and the browser. It can mimic user behavior on the browser so that your test script perform actions like a user would do". 

How does it work? 

The high level view of the flow is very straight forward. Your test scripts talk to Selenium, Selenium talks to the webdriver of your needs, the specific webdriver talks to the browser. So this means you need all components. The browser, the browser specific webdriver, selenium and a program language that is supported (see SeleniumHQ documentation). 

Where to find all needed components?

Program language and browsers

I'm going to keep this one short since installing a program on your favorite OS should not be an issue. I'm using Ruby version 2.5, installing is pretty straight forward on Windows, just make sure you add Ruby to your PATH variable. Safari is already preinstalled on OSX. IE & Edge are already preïnstalled on MS Windows. So the only browsers you might need are any additional browser you want to test on. Chrome or Firefox are the most used ones. 

Selenium

As you might know, I'm using the lapis_lazuli ruby gem, which installs all needed gems to get started straight away (besides the webdrivers part below). So it will install the selenium-webdriver gem, the cucumber gem, the watir gem and some other gems that this framework needs. You can also only use selenium-webdriver, but in the examples below I'll use a lapis_lazuli example, so make sure you've installed that gem and created a test project to get started. See the lapis_lazuli documentation for more info. And keep in mind that you start the IRB session while you are in the project folder because of a bug in the current version which will throw you a "SystemStackError: stack level too deep" error otherwise.

Webdrivers

Each browser has it's own webdriver. You can find them all via the SeleniumHQ downloads. But you can also find them seperately. Below I'll mention the ones I'm using for my setup.
Make sure the webdrivers are in your PATH variable, if you use Ruby on Windows it's as easy as putting them in the Ruby/bin folder. But if you want to store them somewhere else, you can also add this custom folder to your PATH variable.
Keep in mind that the webdriver (except for the Safari one) is a seperate program from the browser. So when the browser gets an update (which usually happens via automatic updates) you manually need to update the webdriver when it's too old, otherwise your tests might fail at some point.

If you find this to much trouble, I totally recommend the ruby webdrivers gem,

When you're missing the webdriver or when the webdriver is not accessible by selenium, the message is pretty clear about the cause and solution.
  • "Selenium::WebDriver::Error::WebDriverError:  Unable to find chromedriver. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver."
If you use selenium-grid, make sure each node has the needed webdriver and browser, ruby does not need to be installed there and even your code does not need to be on the nodes, but I'll cover that in a later topic.

If you use Appium, there is no need to download any webdrivers since this comes with Appium. For a bit more information about an Appium setup, read my previous post.

Webdrivers gem for Ruby

The webdrivers gem for Ruby takes away all the trouble of downloading the webdrivers and keeping your webdrivers up to date. To use the webdrivers gem in your test suite make sure you include this gem before you start the browser session. If you start a test via IRB it's as easy as:
require 'webdrivers'

Side note: keep in mind that this is not working for selenium-grid machines (nodes) as far as I'm aware. This because you run the session on a different machine than the node that needs to have the webdriver. And for Appium it's not needed.

Examples of use

Local

If all is installed correctly, you can start an IRB session and start a browser session.
    irb(main):001:0> require 'lapis_lazuli'
    => true
    irb(main):002:0> require 'webdrivers' 
    => true
    irb(main):003:0> include LapisLazuli
    => Object
    irb(main):004:0> browser :chrome
    DevTools listening on ws://127.0.0.1:12045/devtools/browser/d9e98499-7e32-4335-bae4-c78d379cdc4b
    => #<LapisLazuli::Browser:0x00000000062ec358 @browser_wanted=:chrome, @optional_data={}, @browser_name="chrome", @browser=#<Watir::Browser:0x..fb79a234a url="data:," title="">>
    irb(main):005:0>

Selenium-grid

In case you use a selenium-grid with a hub and nodes all on the same machine, then it's no different from local when it comes to webdrivers. If the nodes are different machines, make sure you have the browser installed and the driver in a folder that is added to the PATH variable of each machine. In case you want to start the node with a specific driver from a certain file location, then start the node like this:
java "-Dwebdriver.edge.driver=C:\<whatever_folder>\<driver_name>.exe" -jar ...

Appium-node

When you want to test on a Android phone with Chrome browser, check out my other post for the complete "how to". But in case you just need to use a different driver, just start the server like this:
appium --chromedriver-executable ~/<whatever_folder>/<drivername>

Comments

Popular posts from this blog

PowerShell - How to overcome Azure VM's fixed resolution limitation

TA Basics: Website Test Automation on mobile devices via Appium server

TA: Who doesn't like proxies? Me!