Friday, August 18Be an Automation Engineer

Basic example program in webdriverjs

As we all know that webdriverjs is the Javascript library for Selenium. Before starting our first program it is always better to know about the automation tool which we will be using further. Many people will get confused on using WebdriverJs while we already have Selenium Java. Read my explanation below so that you will get some idea on it.

When to choose WebdriverJS over selenium Java?
Don’t confuse whether to go with selenium Java or WebdriverJs. It’s simple. If your application uses Node platform for developing User interface, you can choose webdriverjs so that you can easily integrate your test scripts with your development code. Go with Selenium java if your application is using more of Java technologies like JSP and spring. Finally it’s upto you to choose which automation tool you want to use based on your comfort.
You may feel difficulty to understand promises and asynchronous nature of Javascript with respect to WebdriverJs. But it would be easy once we get used to it.
1. Installation

Before starting our first example program, we need to install WebdriverJs. Click here to go through step by step installation process.

Now, let’s start writing a basic example program.

2. Write Program

Create the below test script (JS) file in your project folder.

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('http://www.webdriverjs.com/');
driver.findElement(By.css('.search-field.form-control')).sendKeys("WebdriverJs");
driver.findElement(By.css('button.search-submit i')).click();
driver.wait(until.elementTextIs(driver.findElement(By.css('.vl-main-header>h1')), "Search Results for: WebdriverJs"), 10000);
driver.quit();

We can configure the driver using webdriver.builder(). We can use all the element identification strategies like Xpath, cssSelector, className, id etc..

driver.wait() is an explicit wait like in selenium.

3. Add driver in Path

This is an additional step specific to webdriverjs which you don’t find in other selenium based automation tools.
WebdriverJs will try to use the browser driver (ChromeDriver/geckodriver/safaridriver etc..) from system path variable. So we need to add the driver location to the path variable.
Please follow the below steps to add driver to your PATH variable.

  • First download the required version of the driver. You can find download locations of drivers Here.
  • Extract the driver from downloaded to zip file and save it in your required location.
    IMPORTANT: Remove version information from the driver file name. If your extracted driver name is ‘chromedriver_2.25.exe’ then you need to rename it to ‘chromedriver.exe’
  • Now, add your driver location to the Path. Lets say your driver location is ‘Documents/Seleniumdrivers’.
    • In Mac
      • export PATH=$PATH:~/Documents/Seleniumdrivers
        
    • In Windows 10 and Windows 8
      • You can set path in windows under Environment Variables -> System variables
4. Run Test
node site_search.js

You would see chrome browser opens up and navigates to webdriverjs.com and searches the site with ‘WebdriverJs’ keyword and verifies the search page heading.

%d bloggers like this: