Selenium Webdriver 27Aug, 2015

WebDriver is a tool for automating testing web applications and can be used for automating the interactions with browsers. As a tester its very difficult to perform regression repetitively for same test cases on different browsers. Delimma of the developers is that sometimes it is very time consuming to perform unit testing again and again on same thing.

WebDriver is a magical sticktool that can save our time. Just write the test cases and run them through WebDriver. It will automatically open specified browser for you and run the test cases. It performs all the actions that you can do with your keyboard and mouse.

Here we are discussing about Selenium Webdriver. You need to install language-specific client drivers to run Selenium WebDriver scripts. This article will demonstrate the steps in Javascript (Node), so our first step will be to install selenium-webdriver  module via npm(we are assuming that you have already installed npm on your machine)

npm install selenium-webdriver

Let us create our first automation script

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

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

//Open url on browser
driver.get('http://www.bitsntricks.com/');

//Click the 3rd anchor tag inside the #mainmasonry
driver.findElement(By.css('#mainmasonry .isotope-item:nth-child(3) a')).click();

//Wait for a moment. Let the browser redirect ;)
driver.sleep(5000);

//Select search box having name 's' and type zap
driver.findElement(By.name('s')).sendKeys('zap');

//wait for a moment
driver.sleep(5000);

//Submit the form
driver.findElement(By.id('searchForm')).submit();
driver.sleep(5000);
driver.quit();

WebDriver also returns the Promises, same code can also be written as

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

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

//Open url on browser
driver.get('http://www.bitsntricks.com/');

//Click on 3rd anchor tag inside the #mainmasonry
driver.findElement(By.css('#mainmasonry .isotope-item:nth-child(3) a'))
.click()
.then(function(){
      return driver.findElement(By.name('s')).sendKeys('zap');
 },function(err){
      console.log(err);
 })
.then(function(d){
      driver.findElement(By.id('searchForm')).submit();
      driver.sleep(5000);
      driver.quit();
},function(err){
      console.log(err);
})

To run the same script on Chrome,

1) We need to install Chrome driver

npm install --save chromedriver@2.12.0

2) Set chrome service as

var webdriver = require('selenium-webdriver'),
    By = require('selenium-webdriver').By,
    until = require('selenium-webdriver').until,
    chrome = require('selenium-webdriver/chrome'),
    path = require('chromedriver').path,
    service = new chrome.ServiceBuilder(path).build();
	chrome.setDefaultService(service);
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

Now it is all set to run your first browser automation script. For more details, please go to http://seleniumhq.github.io/selenium/docs/api/javascript/ or comment below.

Posted by: naresh / In: Node JS and Tagged , , ,
Cam

Leave a Reply

Your email address will not be published.