Some scripts include dates or times. For example, a script for an airline's reservation website would include the date and time of a flight reservation. When a static date in a script is in the past, the expired date may cause the script to fail.
To prevent script error or failure, you can add custom code that automatically updates date values.
Before you begin
You should know the following:
There are several ways to add dynamic dates to a GSLv2 script. The following procedure keeps the code easy to read and makes it accessible in the Recorder. In this procedure, all the date generation code is contained within an
executeJS (Custom) code block in the script. Any date variables you need to substitute in are stored in the control window. Those values are then accessed using the backtick syntax when defining the value for the date's text or select field.
You can use this procedure with either desktop browser scripts or mobile scripts.
Record the script, entering any valid date into the date selection fields.
Be sure to perform an action on each date selection field, even if the application auto-populates a date for you. This creates a reference to the field's locator when adding in the dynamic dates.
Review the date selection method on the page.
Note whether the date is defined through an input field or multiple select fields, and what format the date should be.
Keep track of the information you will need to calculate and store in the script.
After you record the script, play it back to verify that it finished successfully.
In the Recorder, add a Custom action for the
executeJS function before the recorded date selection actions.
Click the separator bar before the date selection action to add the action.
If the script contains more than one date selection — for example, departure and return dates for an airline or hotel reservation — make sure the new Custom action is inserted before the first such action.
Set the Target Window to
The Recorder uses the control window to perform background operations in a separate process from the interactions with the browser. The results of those operations are passed back to the browser.
In the Code field, enter your dynamic date code.
If you use Dynatrace Synthetic Monitoring Date functions, begin the code block with the following declaration:
Make sure any values you want to store in the control scope are referenced without the
var myDate = new Date();
myDate = new Date();
See the example code below.
Locate an action where you are setting a date value.
In the value field for that action, replace the static value with the appropriate date variable declared in the control window for that action, using the backtick syntax.
The figure shows an example where the variable is
Play back the script to verify that the dynamic dates are working and the script still finishes successfully.
Below is sample code from a transaction against a travel booking site. In the site, dates are selected through two dropdown (select) fields. One field is for the date value, and the other is for the month and year. Each variable can be referenced from the corresponding Select action in the script using the backtick syntax.
Lines beginning with two slashes (
// ) are comments.
executeJS code: //initiate Dynatrace Synthetic Monitoring Date gomezDate.init(); //calculate the depart and return dates var departDate = gomezDate.addDays(now, 30); var returnDate = gomezDate.addDays(departDate, 7); //code for date formatting var months = new Array('January','February','March','April','May','June', 'July','August','September','October','November','December'); var departMonth = months[departDate.getMonth()]; var returnMonth = months[returnDate.getMonth()]; //format and store dates in control window departMY = departMonth + departDate.getFullYear(); departDay = departDate.getDate(); returnMY = returnMonth + returnDate.getFullYear(); returnDay = returnDate.getDate();