- 6 Minutes to read
- Print
- PDF
Test Automation Example: Business date calculation
- 6 Minutes to read
- Print
- PDF
Goal
Demonstrate multiple ways to address a common customer request: ensuring that the delivery date for a purchase order is automatically set to 5 business days after the document date.
Solutions
There are several possible ways to achieve this using Panaya’s ScriptBuilder. Three examples are below:
1.) AI Prompts – Automates "business days" calculations.
2.) ScriptBuilder Functions – Leverages built-in functionality for date adjustment.
3.) JavaScript – Customizes logic for web applications.
(Note: JavaScript functionality is only available if the automated test is for Web applications. Tests on SAP GUI only do not offer JavaScript functionality)
Example Scenario
In a Purchase Order, set the delivery date to 5 business days after the document date. Adjust dates falling on weekends to the following Monday.
To illustrate this process, we will use creating a Purchase Order as an example. First, the Vendor and item(s) are entered, followed by selecting a document date (e.g., today’s date). The delivery date must then be set to five business days after the document date. If the calculated delivery date falls on a weekend, it should be adjusted to the following Monday.
The recorded script contains the following part, which we will focus on:
In Step 13, the Document date is entered.
In Step 16, the Delivery date is entered.
Since we see that the Document date is a Monday (30th Dec 2024), 5 days later would be Saturday, and therefore the Delivery date was increased by 2 days, to fall on the following Monday (6th Jan 2025).
We want to calculate and enter the delivery date automatically.
Solution 1: AI Prompts
Use an AI prompt similar to the following generic one:
- “set parameter X to 5 business days after parameter Y”
The important part here is “business days” – the AI will automatically exclude weekends (and public holidays)
- In Panaya’s AI prompt, all parameters must be explicitly declared and enclosed in curly brackets to ensure their values are utilized. For example, a possible AI prompt could look like this:
“set {delivery_date} to 5 business days after {document_date}”
Example Steps:
Capture document_date as a variable.
Use an AI prompt to compute and set the delivery_date.
In the example, the script was amended as follows:
First, capture the entered date into a variable called ‘document_date’ (Step 14)
Next, define a second variable, ‘delivery_date,’ to be calculated as 5 business days after the ‘document_date’ using an AI prompt. The AI will generate the appropriate date, which ScriptBuilder will automatically input into the ‘Delivery Date’ column.
Note:
Sometimes the “format” of the date can be rejected by the AUT (Application Under Test). In order to make the results more predictable, it is recommended to set a specific date format if necessary.
Example AI prompt that includes the format:
“set {delivery_date} in mm/dd/yy date format to be 5 business days after the {document_date}”
Solution 2: ScriptBuilder Functions
Panaya’s ScriptBuilder comes with inbuilt instructions and functions which can be leveraged to easily and quickly build automated scripts (more information: Script Builder Instruction Types and Automation Functions)
One possible solution is outlined below (though there are many other ways to address this):
In order to set the delivery date correctly and only include business days, ScriptBuilder can be used to automatically set a date in 5 days by using the today() function, which returns today’s date. Simple math functionality can then be used to add or subtract days. In our example, we will capture what the user entered into the document_date, and then use the today() function + 5 to calculate the date for 5 days later. We then capture this date into the delivery_date variable.
The above does not consider any weekend days that the delivery date could fall on.
As a next step, the day_of_week() function can be used to determine what day of the week the delivery_date falls on. The day_of_week() function returns a number which indicates what day of the week a date is.
This means that if
day_of_week(date) = 1 → The date is a Sunday
day_of_week(date) = 2 → The date is a Monday
…
day_of_week(date) = 7 → The date is a Saturday
We could create three sections:
one to check which weekday the delivery_date falls on
another to add two days if it falls on a Saturday
a third to add one additional day if it falls on a Sunday.
Note
The date format should match the Script Setting format.
So we add a section for validation purposes next, and we call it “Required delivery date Validation”:
Step 17: The script above checks whether the day of the delivery_date variable is a Saturday (indicated by the day_of_week() function returning 7). If it is, the script proceeds to a section called “Add 2 days.”
Step 19: If the delivery_date variable falls on a Sunday (indicated by the day_of_week() function returning 1), the script proceeds to a section called “Add 1 day.”
The section “Add 2 days” looks as follows:
This section is accessed using the "Go to" command only when the delivery_date falls on a Saturday. Within this section, the delivery_date is adjusted by adding 2 days (+2), and the updated date is entered into the Delivery Date column.
In the subsequent Step 28, an expression (1=1, which is always true) is validated, ensuring the script returns to the “Required delivery date Validation” section above.
(This also rechecks whether the delivery_date variable falls on a weekend. However, since the delivery_date has already been adjusted by 2 days and now falls on a Monday, the script proceeds without entering the “Add 2 days” or “Add 1 day” sections.)
The section “Add 1 day” looks as follows:
This section is accessed via the "Go to" command only when the delivery_date falls on a Sunday. Within this section, the delivery_date is adjusted by adding 1 day (+1), and the updated date is entered into the Delivery Date column.
In the subsequent Step 28, an expression (1=1, which is always true) is validated, causing the script to return to the “Required delivery date Validation” section above.
Afterwards, the delivery_date will be a business day, and the script continues:
The script captures the delivery_date entered (after ensuring the date does not fall on a weekend), saves the input data, and verifies that the confirmation message begins with ‘Standard PO created under the number …’.
Solution 3: JavaScript
Using JavaScript, you can determine whether the delivery_date falls on a business day. If it does not, the date can be adjusted to the next business day. This approach is similar to the first option, which uses an AI prompt. However, instead of relying on the AI prompt, JavaScript is used to make any necessary adjustments to the delivery date.
JavaScript similar to the following can be used:
let dd = panayaVars.delivery_date; //declare a new variable dd, which reads the delivery_date value
const dayOfWeek = dd.getDay(); // 0 for Sunday, 6 for Saturday
if (dayOfWeek === 6) {
// If it's Saturday, add 2 days to get to Monday
dd.setDate(dd.getDate() + 2);
} else if (dayOfWeek === 0) {
// If it's Sunday, add 1 day to get to Monday
dd.setDate(dd.getDate() + 1);
}
panayaVars.delivery_date = dd;
// set the delivery_date to dd (to update the delivery_date to a weekday if necessary, if the delivery_date is a weekday - no changes are made)
For this example, the script would look similar to the following (abbreviated):
In Step 15 the parameter delivery_date is initialized with the value of 5 days after the document_date.
Step 16 is the “Execute Code” step, where the JavaScript runs:
Note:
It is possible to use AI to generate the JavaScript code for you. Details can be found here.
Summary
There are multiple ways to build your script, and Panaya recommends using components for repetitive actions (learn more here). The example above can be created as a reusable component that takes a date as an input variable, calculates a date 5 business days later (excluding weekends), and returns it. This component can then be called with the document_date as the input to generate the corresponding delivery_date.