Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

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.