Installation of Selenium in nodeYou need to follow below steps to set up the environment for running Selenium tests using Node.js
- Download and install Node.js
- Install Selenium Webdriver package for Node.js
- Download the drivers for various browsers like IE, Chrome, Firefox etc
- Write the tests in Java Script and Execute it.
Downloading and Installing Node.jsJust 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.jsNext 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 browsersAfter that download the driver applications for each browsers and update the system path to point to the location where you have placed the drivers.
Writing and running the first script using Node.jsThen write the sample selenium code as shown in below example and save the file as launchChrome.js
To execute the above file, you need to fire below command in a command prompt.
var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder() .forBrowser('chrome') .build(); driver.get('https://www.softpost.org'); driver.quit();
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. Synchronized Selenium Webdriver – https://www.npmjs.com/package/webdriver-sync Another way is by using node-fibers and wd-synch libraries as explained at Webdriver.io
Complex problems, Simple Solutions