Print

Selenium JavaScript NodeJs Web Driver Simple Test

This article explains how to write a simple automated test using Selenium JavaScript node.js web driver. 

Test Case: authorized users can login Mahara ePortfolio demo site. 

Test Steps

(1) Navigate to the site "http://demo.mahara.org";

(2) Enter "student1" in the username field;

(3) Enter "Testing1" in the password field;

(4) Click on the Login button;

(5) Verify that user "Settings" link is displayed. 

Test Implementation

Open a text editor or JavaScript editor and write the following script and save it as "login_test.js" under a folder. In this example, the folder name is "C:\SeleniumNodeJsAutomation"

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

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

driver.get('http://demo.mahara.org');
driver.findElement(By.id('login_login_username')).sendKeys('student1');
driver.findElement(By.id('login_login_password')).sendKeys('Testing1');
driver.findElement(By.id('login_submit')).click();

driver.findElement(By.linkText('Settings')).then(function(element) {
  console.log('Yes, found the element');
}, function(error) {
  console.log('The element was not found, as expected');
});
driver.quit();

Run the script with node and see the result as shown below:

C:\SeleniumNodeJsAutomation>node login_test.js
Yes, found the element

C:\SeleniumNodeJsAutomation>

The result showed that the test passed. This is a simple example about the usage of Selenium JavaScript node.js web-driver. 

Print

selenium javascript node.js installation

To automate web testing with Selenium JavaScript node.js web driver, we need to install required components on the test computer. Here are the required application. 

Install node.js

 Open the address "https://nodejs.org/download/" with your browser and download an installer package depending on your OS and then install the application. On Windows 7, you need to install it as an administrator. 

After installing node.js, you can test the installation with the command line below. 

C:\Users\SeleniumMaster>node --version
v0.10.33
C:\Users\SeleniumMaster>npm --version
1.4.28

The above command result showed that current installed node.js version is v0.10.33 and npm version is 1.2.28

Install Selenium Web Driver for JavaScript node.js

You can install Selenium web driver for JavaScript node.js via npm command below: 

npm install selenium-webdriver
 

check selenium-webdriver installation

npm show selenium-webdriver

if selenium-webdriver is successfully installed, some information below displays based on your version of selenium-driver installation

{ name: 'selenium-webdriver',
  description: 'The official WebDriver JavaScript bindings from the Selenium pro
ject',
  'dist-tags': { latest: '2.45.1' },
  versions:
   [ '2.29.0',
     '2.29.1',
     '2.30.0',
     '2.31.0',
     '2.32.0',
     '2.32.1',
     '2.33.0',
     '2.34.0',
     '2.34.1',
     '2.35.0',
     '2.35.1',
     '2.35.2',
     '2.37.0',
     '2.38.0',
     '2.38.1',
     '2.39.0',
     '2.40.0',
     '2.41.0',
     '2.42.0',
     '2.42.1',
     '2.43.1',
     '2.43.2',
     '2.43.3',
     '2.43.4',
     '2.43.5',
     '2.44.0',
     '2.45.0',
     '2.45.1' ],
  maintainers: 'jmleyba <
 This email address is being protected from spambots. You need JavaScript enabled to view it.
 >',
  time:
   { modified: '2015-03-02T04:51:05.194Z',
     created: '2013-02-14T20:44:42.963Z',
     '2.29.0': '2013-02-14T20:44:43.830Z',
     '2.29.1': '2013-02-18T05:43:04.425Z',
     '2.30.0': '2013-02-19T00:13:07.536Z',
     '2.31.0': '2013-03-03T01:12:33.960Z',
     '2.32.0': '2013-04-14T18:59:32.486Z',
     '2.32.1': '2013-04-23T03:44:56.629Z',
     '2.33.0': '2013-05-27T16:15:25.525Z',
     '2.34.0': '2013-08-06T18:14:38.581Z',
     '2.34.1': '2013-08-09T20:28:32.702Z',
     '2.35.0': '2013-08-12T22:10:28.241Z',
     '2.35.1': '2013-08-27T22:00:38.139Z',
     '2.35.2': '2013-09-27T16:42:53.036Z',
     '2.37.0': '2013-10-18T17:14:59.996Z',
     '2.38.0': '2013-12-05T18:09:44.636Z',
     '2.38.1': '2013-12-09T01:49:35.256Z',
     '2.39.0': '2013-12-16T20:35:40.801Z',
     '2.40.0': '2014-02-19T18:23:45.907Z',
     '2.41.0': '2014-03-28T16:58:42.287Z',
     '2.42.0': '2014-05-23T20:15:49.862Z',
     '2.42.1': '2014-06-10T19:03:16.647Z',
     '2.43.0': '2014-09-12T17:01:24.930Z',
     '2.43.1': '2014-09-12T18:17:26.624Z',
     '2.43.2': '2014-09-12T21:34:10.491Z',
     '2.43.3': '2014-09-12T21:39:03.790Z',
     '2.43.4': '2014-09-13T17:28:53.165Z',
     '2.43.5': '2014-09-18T02:59:14.398Z',
     '2.44.0': '2014-10-24T02:53:55.449Z',
     '2.45.0': '2015-02-27T01:37:40.271Z',
     '2.45.1': '2015-03-02T04:51:05.194Z' },
  repository:
   { type: 'git',
     url: 'https://code.google.com/p/selenium/' },
  readmeFilename: 'README.md',
  homepage: 'https://code.google.com/p/selenium/',
  keywords:
   [ 'automation',
     'selenium',
     'testing',
     'webdriver',
     'webdriverjs' ],
  bugs: { url: 'https://code.google.com/p/selenium/issues/list' },
  users:
   { winsonwq: true,
     avishek: true,
     jayrobot: true,
     dujuan: true,
     syzer: true,
     blackoperat: true,
     tomwayson: true,
     ny: true,
     rowemore: true,
     awaterma: true,
     flowlo: true,
     vaibhavsingh: true,
     hq: true,
     mtscout6: true,
     'slava.hatnuke': true,
     pratheek: true,
     flashyrc: true,
     roshanravi10: true,
     walmik: true,
     sobear: true,
     paulomcnally: true,
     'nandini.3186': true,
     billfeller: true,
     fattenap: true,
     rgraves90: true,
     jmeester: true,
     parkerproject: true,
     shanewholloway: true,
     hrmoller: true,
     dyoshida: true,
     santosharakere: true,
     jhabdas: true,
     nchase: true,
     kharytonenka: true,
     jefrancomix: true,
     taoyang987: true,
     itonyyo: true },
  version: '2.45.1',
  main: './index',
  engines: { node: '>= 0.10.x' },
  dependencies:
   { 'adm-zip': '0.4.4',
     rimraf: '^2.2.8',
     tmp: '0.0.24',
     ws: '^0.7.1',
     xml2js: '0.4.4' },
  devDependencies:
   { express: '^4.11.2',
     mocha: '>= 1.21.x',
     multer: '^0.1.7',
     'promises-aplus-tests': '^2.1.0',
     'serve-index': '^1.6.1' },
  scripts: { test: 'mocha --harmony -t 600000 --recursive test' },
  dist:
   { shasum: '6fc6a8ce385085a7b670edd36a379b2eddadf2c9',
     tarball: 'http://registry.npmjs.org/selenium-webdriver/-/selenium-webdriver
-2.45.1.tgz' },
  directories: {} }

Now, your can write test automation with selenium-webdriver JavaScript node.js on your PC.