Monthly Archives: August 2016

Installation and Environment set up of Selenium and Node.js

You need to follow below steps to set up the environment for running Selenium tests using Node.js

  1. Download and install Node.js
  2. Install Selenium Webdriver package for Node.js
  3. Download the drivers for various browsers like IE, Chrome, Firefox etc
  4. Write the tests in Java Script and Execute it.

Downloading and Installing Node.js

Just visit official Node.js website and download the Node.js installer for windows. Double click on that installer file and follow on-screen instructions to install the Node.js. After installation is completed, update the system path to include the location of Node.js installation directory.

Node.js in System Path
Node.js in System Path

To ensure that Node.js is installed correctly, open the command prompt and execute below command.

Checking Node.js Version
Checking Node.js Version

As you can see, I have got v4.4.7 installed on my system.

Installing Selenium Webdriver Package for Node.js

Next you have to install the Selenium Webdriver package for Node.js. For that, just execute below command in a command prompt.

npm install selenium-webdriver

Downloading driver files for browsers

After that download the driver applications for each browsers and update the system path to point to the location where you have placed the drivers.

  1. http://chromedriver.storage.googleapis.com/index.html
  2. http://selenium-release.storage.googleapis.com/index.html
  3. http://go.microsoft.com/fwlink/?LinkId=619687
  4. https://github.com/mozilla/geckodriver/releases/
  5. http://selenium-release.storage.googleapis.com/index.html

I have put the chromedriver.exe and IEDriverServer.exe files in Downloads directory so I have updated the path variable as shown in below image.

Driver files included in System Path
Driver files included in System Path

Writing and running the first script using Node.js

Then write the sample selenium code as shown in below example and save the file as launchChrome.js

var webdriver = require('selenium-webdriver');
   
var driver = new webdriver.Builder()
        .forBrowser('chrome')
	.build();
	
    driver.get('http://www.softpost.org');
    driver.quit();

To execute the above file, you need to fire below command in a command prompt.

node launchChrome.js

There are many Node.js packages out there for browser automation using Selenium. But we we would be using selenium-webdriver official package.

Here is the list of other node.js packages for selenium automation.

  1. Synchronized Selenium Webdriver – https://www.npmjs.com/package/webdriver-sync
  2. Another way is by using node-fibers and wd-synch libraries as explained at Webdriver.io