Skip to content

How to get command details (Target, Value) of the failed command(s) in the test run

This example demonstrates how to give the provide the users with information of failures like instruction target and value in the flow of execution if any exceptions came up during execution.

In the example below, we have two scripts where the first script has the application flow with verification while the second script has the steps to be executed when there is a failure in application flow. The details like instruction id, failed instruction target, value and the instruction are being stored to different script variables and the execution jumps to the Test Script (exception handler Test Script) in Exception Handler Code from command "setExceptionHandlerContext". Script 2 sends an email about the details of error captured in the first script.

Solution:

Script 1:

# Command Target Value
Enter Age
1 type
id="txt_age" 45 
Enter Income
2 type
id="txt_income" 55000 
Enter Zipcode
3 type
id="txt_ZipCode"
10001 
Click on the Calculate button
4 click
id="btn_calculateControls"
Verify the PDP page
5 assertText
//div[@id="outputheader"]/h1
Results
To get if there is any Failed Command and store the presence of command in variable "FailedCommand"
6 getFailedInstructionCommand
FailedCommand
 
If FailedCommand returns any value/text, execute exception handling script
7 if
"${FailedCommand}"!="" 
Print the value in FailedCommand
8 echo${FailedCommand} 
To get the Failed Instruction ID
9 getFailedInstructionId
InstructionID

To get the Last Failed Instruction Identifier to use to get the screenshot as a mail attachment
10 getIdentifierForLastFailedInstruction LastFailedInstructionIdentifier 
To get the Last Failed Instruction Target value
11 getFailedInstructionTargetLastFailedInstructionTarget
To get the Last Failed Instruction value
12 getFailedInstructionValueLastFailedInstructionValue 
To set the Exception Handler script
13 setExceptionHandlerContext
Script 2(W)
 
End the if statement
14 endIf
 

Script 2: 

# Command Target Value
To begin the email service block
1 emailBlockStart    
To set the sender for the composed email
2 setSender info@eureqatest.com  
To set the subject for the composed and email
3 setSubject Worksoft SaaS Notification: <Subject>  
To set the "To" recipient list for the composed email
4 setRecipientToList recipient1@gmail.com  
To create the email body with a variable name
5 createBody body_template  
To create an email body part from Text or HTML string
6 createBodyPartFromTextOrHTML

Hi <mention recipient's name here>,<br/> 
<mention your body text here such as "product not available" with details like > Failed Command - ${FailedCommand} <br/>

Failed Instruction ID - ${FailedInstructionID } <br/>

Last Failed Instruction Identifier - ${LastFailedInstructionIdentifier}<br/>

Last Failed Instruction Target- ${LastFailedInstructionTarget}<br/>

Last Failed Instruction Value- ${LastFailedInstructionValue}

body_template
To set the screenshot captured during the test run as an attachment to the email
7 setAttachmentScreenshot ${LastFailedInstructionIdentifier}  
To send the composed email to the sender's specified in <To> list
8 sendEmail    
End of the email block
9 emailBlockEnd    

Tips, Tricks, Gotchas & Best Practices:  

  • Customization of the emails can be done. Refer to Email command examples.
  • As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscore or hyphen.

Representative Usage Scenarios for this Example:

Use Case #1:

Consider an e-commerce site, for example, 'Amazon' where when product availability is to be verified or if the page you have accessed is loaded correctly is to be tested for and if there are any exceptions, then you can use these exception handling commands to customize the outcome you require. 

Feedback and Knowledge Base