In test automation, it is crucial to understand how to work with dates in order to perform tasks such as booking, reserving, and scheduling at specific times.
Luckily, both Playwright and NodeJS have excellent support for working with dates, making it easier for test automation professionals to manipulate dates in their scripts.
In this blog, we'll cover a couple of great examples of how to work with dates in test automation using Playwright and NodeJS.
Whether you're a beginner looking to learn more about working with dates in automation or an experienced professional looking for new ways to improve your skills, this blog has something for you.
Before we get started, it's important to give a quick shoutout to the amazing NodeJS community. The support and resources provided by this community have made it an invaluable resource for developers and test automation professionals around the world.
Whether you're a beginner looking to learn more about NodeJS or an experienced developer seeking new ways to improve your skills, the NodeJS community has something for you.
It's also worth noting that understanding how to work with NodeJS is an incredibly useful skill for any test automation professional to have in their toolkit, as it allows you to easily incorporate pre-existing code into your projects and save time and effort.
Getting today's date is a common task in test automation and is made easy with the "date-and-time" node module. First, let's make sure to import the module.
Then, getting today's date is as simple as creating a new Date object.
That's it! You now have today's date stored in the "now" variable. You can then use this date in your automation scripts, format it as needed, or perform any other desired actions with it.
This will output the current date and time to the console. With the "date-and-time" module and a few simple lines of code, getting today's date in test automation is a breeze.
Check out the NodeJS module "date-and-time" here.
Formatting dates is an important task as it allows you to customize the way your dates are displayed and ensure that they are in the desired format for your scripts. The "date-and-time" node module provides a variety of options for formatting dates, making it easy to tailor your dates to your specific needs.
To format a date, you can use the "format" function of the "date-and-time" module and pass in your date and a formatting string. The formatting string uses special characters to represent different parts of the date, such as the month, day, and year. For example, to format a date as "January 1, 2021," you could use the following formatting string: "MMMM D, YYYY."
Here's another example of how to use the "format" function to format a date. This will return only today's Day as "09" or "15".
In addition to the options for formatting months, days, and years, you can also use special characters to add leading zeros to single-digit dates.
For example, the formatting string "DD/MM/YYYY" will output the date as "01/01/2021" for January 1st. If you want to drop the leading zero from single-digit dates, you can use the "D" character instead of "DD."
For example, the formatting string "D/M/YYYY" will output the date as "1/1/2021" for January 1st.
Finding future dates is a common task in test automation and can be easily accomplished with the "date-and-time" node module. To find a future date, you can use the "addDays" function of the module and pass in the current date and the number of days you want to add.
This will add one day to the current date and store the resulting future date in the "AddDays" variable. You can then use this future date in your automation scripts or format it as needed.
This will output the date one day in the future in the format January 2, 2023 becoming "02". By using the "addDays" function and the "format" function of the "date-and-time" module, you can easily find and customize future dates in your test automation scripts.
Finding the nearest weekday is an important task in test automation, particularly when working with applications that may have different behavior on weekends. By blindly adding days to a date, we can't guarantee that we will end up on a weekday, which may lead to unexpected results in our automation scripts.
One solution to this problem is to use a node module like "get-nearest-day-of-week," which allows us to find the nearest weekday to a given date. In the example provided, the "get-nearest-day-of-week" module is used to find the nearest Monday to the current date.
This will store the nearest Monday to the current date in the ""Monday" variable. We can then use this date in our automation script, just like any other date. In the provided example, the nearest Monday is used as the start date for a booking process. The "date-and-time" module is also used to format the start date for use in a locator, allowing us to click on the correct day on a calendar.
By using a node module like "get-nearest-day-of-week," we can ensure that we are always working with weekdays in our automation scripts, avoiding any potential issues caused by different behavior on weekends. This can save time and effort by eliminating the need to manually add or subtract days to ensure we are on a weekday.
Using dates as arguments in locators is a powerful technique in test automation, as it allows the locator to be dynamic and adapt to changing dates. This can be particularly useful when working with calendars or other features that rely on specific dates.
In the provided example, a locator is used to click on a specific day on a calendar. The locator is dynamic, as it uses the "startFormatted" variable as an argument. This variable contains the formatted date of the nearest Monday to the current date, which was found using the "get-nearest-day-of-week" node module.
By using the "startFormatted" variable as an argument in the locator, the locator is able to adapt to changing dates and always click on the correct day on the calendar. This dynamic behavior is made possible by the use of dates as arguments in the locator.
In conclusion, working with dates in test automation is an essential skill for any test automation professional. Whether you're booking a reservation, scheduling a meeting, or navigating a calendar, understanding how to work with dates can greatly simplify your tasks and improve the efficiency of your automation scripts.
Both Playwright and NodeJS provide excellent support for working with dates, making it easy to manipulate and format dates in your scripts. In this blog, we covered a few examples of how to work with dates in test automation using Playwright and NodeJS, including getting today's date, formatting dates, finding future dates, and finding the nearest weekday.
For more advanced techniques in Playwright automation, you might be interested in our article on Passing Authentication & Other Dynamic Files Between Jobs in GitHub Actions & Playwright, which explores how to handle dynamic data across different stages of your automation pipeline.