...

/

Testing Miscellaneous Data

Testing Miscellaneous Data

Learn how to test advanced data types with Selenium.

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:

Press + to interact
// 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.

Press + to interact
// 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 obtaining accurate ...