Testing Miscellaneous Data
Learn how to test advanced data types with Selenium.
We'll cover the following...
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-18var today = new Date();// generate today's time as string, e.g. 18-7-2016 8:14var 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 momentvar moment = require('moment');// current timeconsole.log(moment().format()); // 2016-07-18T07:53:42+10:00console.log(moment().format("MMM DD, YYYY")); // Jul 18, 2016console.log(moment().format("DD/MM/YY")); // 18/07/16// yesterday and tomorrowconsole.log(moment().subtract(1, 'days').format("YYYY-MM-DD")); // 2016-07-17console.log(moment().add(1, 'days').format("YYYY-MM-DD")); // 2016-07-19// in fortnightconsole.log(moment().add(2, 'weeks').format("YYYY-MM-DD")); // 2016-08-01
Retrieve data from Database
The best way of obtaining accurate ...