Skip to content

How do I perform specific steps on any command failure in the test run

This example demonstrates how to handle the flow of execution if there are any exceptions that might arise 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 execution jumps to the Test Script (exception handler Test Script) upon any failures in Exception Handler Code from command "setExceptionHandlerContext". Script 2 sends an email about the error captured in the first script.

Solution:

Script 1:

# Command Target Value
Launch Application
1 open /  
Verify the title
2 assertTitle Online Shopping site in India: Shop Online for Mobiles, Books, Watches, Shoes and More - Amazon.in  
Verify the Search text box
3 assertEditable //input[@id="twotabsearchtextbox"]  
Enter a value in the Search text box
4 type //input[@id="twotabsearchtextbox"] Sony 138.8 cm (55 inches) 4K Ultra HD Smart LED TV KD-55X8200E (2017 model)
Verify the value entered in the Search text box
5 assertValue //input[@id="twotabsearchtextbox"] Sony 138.8 cm (55 inches) 4K Ultra HD Smart LED TV KD-55X8200E (2017 model)
Click on the Search button
6 click //input[@type="submit"][1]  
Verify the PLP page of the product search
7 assertVisible //h2[contains(text(),'Bravia 4K Ultra HD Android Smart LED TV')]  
Click on the Product link
8 click //a[contains(@title,'Bravia 4K Ultra HD Android Smart LED TV')]  
Verify the PDP page
9 assertText //span[@id="productTitle"] Sony 138.8 cm (55 inches) Bravia 4K Ultra HD Android Smart LED TV KD-55X9000F (Black) (2018 model)
setTimeout command to set the time for the execution of a storeVisible command
10 setTimeout 3000  
Store the visibility of Add to cart button
11 storeVisible //input[@id="add-to-cart-button"] AddtoCart
Reset the time to 2 minutes
12 setTimeout 120000  
Print the value in AddtoCart 
13 echo ${AddtoCart}  
If AddtoCart returns true, click on add to cart button
14 if "${AddtoCart}"=="true"  
Click on Add to cart button
 15 click  //input[@id="add-to-cart-button"]  
Else verify that Add to cart button is not present
16 elseIf "${AddtoCart}"=="false"  
Verify that Product is unavailable
17 assertVisible //span[text()='Currently unavailable.']  
Execute the handler script
18 setExceptionHandlerContext Script 2(W)  
End the if statement
19 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">
body_template
To send the composed email to the sender&rsquo;s specified in <To> list
7 sendEmail    
End of the email block
8 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.

Use Case #2:

Consider a banking site, for example, 'Paytm' where when a new user is to be registered or log in to an existing account. If the registration or login failed, we can handle this exception using these commands.

Feedback and Knowledge Base