Search⌘ K
AI Features

Testing Miscellaneous Data

Explore methods for testing miscellaneous data types in Selenium WebDriver using Node.js. Understand how to dynamically retrieve dates, extract test data from a database with sqlite3, and generate test files of fixed sizes. This lesson equips you with practical techniques to handle diverse data scenarios in automated web tests.

Working with miscellaneous data

In addition to already discussed basic data types, we can also work with many other data types as well.

Get date dynamically

We can get today’s date by:

Javascript (babel-node)
// assume today is 2016-07-18
var today = new Date();
// generate today's time as string, e.g. 18-7-2016 8:14
var dateStr = today.getDate() + "-" + (today.getMonth()+1) + "-" + today.getFullYear() + " " +
today.getHours() + ":" + today.getMinutes();
driver.findElement(By.name("username")).sendKeys(dateStr);

Here, the month is 0-based. In addition to JavaScript’s built-in date functions, we can also use the Moment.js library for similar purposes, which is much easier and more flexible.

Javascript (babel-node)
// npm install --save moment
var moment = require('moment');
// current time
console.log(moment().format()); // 2016-07-18T07:53:42+10:00
console.log(moment().format("MMM DD, YYYY")); // Jul 18, 2016
console.log(moment().format("DD/MM/YY")); // 18/07/16
// yesterday and tomorrow
console.log(moment().subtract(1, 'days').format("YYYY-MM-DD")); // 2016-07-17
console.log(moment().add(1, 'days').format("YYYY-MM-DD")); // 2016-07-19
// in fortnight
console.log(moment().add(2, 'weeks').format("YYYY-MM-DD")); // 2016-08-01

Retrieve data from Database

The best way of ...