Conditional Statements in the Test Scripts
This article is a part of the Self-paced Learning Series for the Course: First-Hand Experience of Worksoft SaaS.Please refer to the link for more details on the Course.
Mapping the Worksoft SaaS feature to the Course Context :: Lesson 13:: Topic 1
1. Review Manual Test Case 4:
Refer to TC #004 from the Test Case Repository
As per the Test Case 4, the user is 18 years old, has an Income of $2000 and enters his/her details to check the eligibility.
Let's enter these details on the AUT to see the Calculated Results:
- Enter the yearly household income (dollars) of the person buying insurance: $2000
- Enter Age of the oldest person on an insurance policy: 18
- Enter Zipcode of the person buying insurance: 08817
- Click on Calculate button
- The Result Header should say "You are younger than age 19" and the Premium and Penalty are not applicable.
2. Editing the Test Script to add Conditional Statement
As you noticed in the Test Case 4, the Inputs and Outputs are completely different and the validations we built for Test Case 1 like verify Premium amount and verify Penalty amount will not work for this Test Case.
Apart from these options, there is another option where we can add a Conditional Statement to the Test Script that we automated for Test Case 1 and use it.
To do this, we will follow the steps below:
Step 1: We will edit the Test Script Instructions to update the new input values
Step 2: We will use the storetext command by editing the Test Script and adding the if-elseif-else-end if condition based on the Premium value and verify the Result Header. And to get the value of Premium, we will be using the storetext command.
To do this, we will follow the steps below:
Step 1: We will edit the Test Script Instructions to update the new input values
Step 2: We will use the storetext command by editing the Test Script and adding the if-elseif-else-end if condition based on the Premium value and verify the Result Header. And to get the value of Premium, we will be using the storetext command.
3. Practice
Editing the Instructions to update the values
- Search for the Test Script 'Automate - Open Webpage & Enter Inputs for HICC - State = NJ Zip = 08817 Age = 45 Income = $20000'.
- Clone the Test Script and update the name as ' Automate - Open Webpage & Enter Inputs for HICC - State = NJ Zip = 08817 Age = 18 Income = $2000'
- Go to the 'Age' instruction and change the value to 18
- Go to the 'Income' instruction and change the value to 2000
- No changes required on the ZipCode value
- Click on 'Save & Continue' button to save the Test Script and perform a Standalone execution.
- Search for the Test Script 'Automate Verify Results of the HICC - State = NJ Zip = 08817 Age = 45 Income = $20000' and Calculate' and click Edit.
- Before adding, ensure that the Results page of the AUT is open.
- To add the storeText command, the easy option is to copy the Instruction from the Premium command that has a value of $1,021.
- Select the verifytext command that has a value of $1,021 and use the keyboard shortcuts Ctrl+C and Ctrl+V.
- The command should now be copied, expand the instruction and go to the Target field to check if the locators are copied. Then, update the command(from verifyText) to storeText and change the value $1,021 and to Premium. Save the Instruction. Also, drag this to the first instruction.
- Now the second instruction is adding Conditional Statements. Before that, add a Comment 'Adding Conditional Statement to handle result-based actions'.
- Add a new Instruction, in the command field, enter "if", in the Target field, enter the condition "${Premium}"=="". (This is the Syntax for comparing strings in the 'If' Conditional Logic). Save the locator and the Instruction. Notice that the else and endif commands are automatically added.
- Under the if Condition, add an assert Command for the Result Header. You can either add this manually or an easy option is to start the recording and use the Context Menu. Before starting the recording, ensure that the AUT is on the Results page.
- Start the recording. On the AUT page, right-click on the Result Header, click Show Context Menu, click the tick mark to add assertText. Stop the recording and move the instruction below the 'if' command.
- Next, Multi-select all 3 Instructions and their comments by using the Cntrl button on your keyboard and drag them under the 'else' block.
- Save the Test Script. Now this Test Script can be used to verify the results of 2 Test Cases (Test Case 1 & Test Case 4)