When you have data that needs to change, or you don’t know what the value is, you insert a variable to hold a place for the value. This article provides a variety of examples on how you can use a variable to make your script more dynamic.
When you create a scenario in ScenarioBuilder, you can use static data that never changes each time you execute it, but it is more realistic that you want the data to change each time you run your scenario.
Let’s look at some scenarios to see how using variables can make our scripts more dynamic and reusable.
NOTE - if you are using AppVerify, then your scripts must contain at least 1 variable in order to generate test cases.
Scenario 1 – Setting a Value for an Empty Variable (Set String Variable)
I have a required field in the application. If the value coming from an external variable (CSV) file (reason for visit) is empty, then I want to set a default value. Here are the steps to do that:
- Add an If statement to your script.
- Enter “%reason for visit% == EMPTY” for the Expression property.
- Add Set String Variable as a child step.
- For the Variable Name property, enter “%reason for visit%”.
- For the Variable Value, enter the data you want the script to use if the “reason for visit” is blank.
Now when the script executes it will read the value in the CSV for reason for visit. If the value is empty, then it will use the value set by the If statement.
Scenario 2 – Random Number (Set Number Variable)
I want to loop a set of steps a random number of times, so I need to create a variable that contains a randomly generated value. To do this, follow these steps:
- Drag the Set Number Variable onto your script.
- In the Variable Name property, enter a name for the random value.
- In the Variable Value drop-down, select [30-47] Random Number.
- Change the 30 to represent the least number of times you want to iterate through your loop.
- Change the 47 to represent the greatest number of times you want to iterate through your loop.
- For your Loop step, in the Iterations property, enter the name you created in step 2 surrounded by % (i.e. %randomnumber%).
Each time the script is executed the loop will use a randomly generated number using the range you set in steps 4 and 5.
Scenario 3 – Determine the Last Day of the Month (Date Functions)
I need to test that my application behaves correctly when the report date falls on the last day of the current month. Because the last day of the month varies, you need to calculate what the last day is each time the script is executed. To do this, follow these steps.
- Drag a Set Date Variable onto the script.
- Enter “LastDayofMonth” for the Variable Name property.
- Leave the Variable Value property blank. This will set it to today’s date.
- Drag Date Functions onto your script.
- In the Variable Name property, enter “%LastDayofMonth%”.
- In the Function drop-down, select LastDayOfTheMonth.
Now when you use the “LastDayofMonth” variable, it will enter the last day of the month corresponding to the current month.
Scenario 4 – Setting the Time to 1 Hour from Current Time (Time Functions)
I’m working with an application that creates products with short lifespans. When I create the product, I need to set the expiration date to 60 minutes from the current time. Obviously, this will change based on the time when the script is executed. To create this time, follow these steps:
- Drag a Set Time Variable onto the script.
- Enter “ExpirationTime” for the Variable Name property.
- Leave the Variable Value property blank. This will set it to the current time.
- Drag Time Functions onto your script.
- In the Variable Name property, enter “%ExpirationTime%”.
- In the Function property drop-down, select AddHours.
- In the Add/Subtract Value property enter “1”. This will add 1 hour to the current time.
Now when the ExpirationTime variable is used in the script, it will be a time that is 1 hour ahead of the current time.
Scenario 5 – Pulling the Values out of a Column in a CSV File that has Address, City, State, and Zip (String Functions)
I have a column “address” in a CSV file that contains a full address (address, city, state, zip). I need to extract the parts of the address separately so I can use them to populate separate address, city, state and zip code fields in the application. If the data is consistent in format, we can use a string function to extract the data into a list. The items in the list are indexed as 1 (address), 2 (city), 3 (state), and 4 (zip). To do this follow these steps. Our example CSV value is “123 Ash Drive, Anytown, CA, 90210”.
- Drag String Functions from the Variables Actions onto your script.
- In the Variables property, enter “%address%”.
- For the Function property, select Split from the drop-down.
- In the Split Delimiter property, enter “,” since our data is separated by commas.
- For the step that enters the address, enter the variable as “%address%”. This will enter 123 Ash Drive when the script is executed.
- For the step that enters the city, enter the variable as “%address%”. This will enter Anytown when the script is executed.
- For the step that enters the state, enter the variable as “%address%”. This will enter CA when the script is executed.
- For the step that enters the zip code, enter the variable as “%address%”. This will enter 90210 when the script is executed.
Scenario 6 – Change the Date Format to the Day of the Week (Date Functions)
I need to make a day of the week selection in my application based on the current date (Sunday, Monday, Tuesday, etc.). I can pull out the day of the week from today's date to accomplish this. Here are the steps to take:
- Drag a Set Date Variable action to your script.
- Set the Variable Name property to a name of your choosing (e.g. today).
- Leave the Variable Value property blank so it will default to today's date.
- Drag Date Functions action to your script after the Set Date Variable you added in step 1.
- In the Variables Name property, enter the name of the date variable you set in step 2.
- For the Function property, select Format from the drop-down.
- In the Date Format property, select "dddd" from the drop-down.
Now when the script is executed, the %today% variable will be the day of the week based on today's date.
Scenario 7 – Automatically Number Elements in a Text File (Increment a Variable)
I have a text file that is created during script execution. As new items are added to the file, I want it to number them automatically. Here are the steps to take:
- Drag Set Number Variable to your script.
- Enter a name in the Variable Name property.
- In the Variable Value property, enter 1. This will start our numbering at 1.
- Add the variable to the text that is written to the file (i.e. %num% - this is what happened…)
- After each step that writes to the text file, add an Increment A Variable action.
- In the Variable Name property enter the name you used for step 2. Remember to enclose it in % (i.e. %num%).
- In the Variable Increment property, enter 1. This will increase the value of the variable by 1.
Now the items written to the text file can be numbered 1, 2, 3, etc.
Scenario 8 – Dynamic Application Buttons (Load an Image Into a Variable)
In the application I want to test the action buttons change based on the record selected (includes the patient name). To handle this, after loading the record I will capture the button and save it as a variable. That allows me to use the image in a Find Image or Click on Image action. Here are the steps to do this:
- In your script after you access the record you are going to work with, drag the Load an Image Into a Variable action onto your script.
- Enter a name for the variable into the Variable Name property.
- Click the ellipse button for the Image area property and drag the mouse over the button to select the area it should capture.
- Add a Click on Image action below the Load an Image Into a Variable step.
- For the find Image #1 property, right click on No image data and select Choose a Variable from the menu. The Select Variable window opens.
- Open the Variable name drop-down and select the variable name used for step 2.
Now when the script runs it will click on the button matching the image captured in the Load an Image Into a Variable step.