Skip to content

To enter the value based on the condition

This example demonstrates how to enter the required value in an input field if the field does not contain any value in it. The storeValue command fetches the text/value of an input field (or any element with value attribute) and stores in the variable specified in the value cell.

An application is launched using open command and the title of the page is verified using the command assertTitle. To search for a product, the input search field should be editable which verified using the assertEditable command. The storeValue command is used to store the value available in the input search field. The required value is entered into the search field only if there is no/blank value available in the field.

Solution:

# Command Target Value
Launch Application
1 open /  
Verify the title of the page
2 assertTitle Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more  
Verify if the Input search box is Editable
3 assertEditable //input[@id="twotabsearchtextbox"]  
Store the value from the search field in ValueInSearchField variable
4 storeValue //input[@id="twotabsearchtextbox"] ValueInSearchField
Print the value store in ValueInSearchField variable
5 echo ${ValueInSearchField}  
Start of if block
6 if "${ValueInSearchField}"==""  
If the variable ValueInSearchField returns null/blank, enter the required value in the input search field
7 type //input[@id="twotabsearchtextbox"] Books
Else, print the value stored in ValueInSearchField variable
8 else    
End of if block
9 endIf    
Click on the Search button
10 click //div[@class="nav-search-submit nav-sprite"]//input[@type="submit"]  

Tips, Tricks, Gotchas & Best Practices:  

  • storeText command can be used if the text is outside the HTML tags (<span>text</span>)
  • It is always advisable to use double quotes (") to compare two string values, whereas no quotes used if numeric values are to be compared

Representative Usage Scenarios for this Example:

Use Case #1:

Consider an e-commerce site, for example, 'Amazon'. When logged in as a registered user, there might be chances of products existing in the cart. The storeText command can be used to fetch the text/value from the cart. Using the conditional logic commands (if and else), if the cart has 0/x products, based on the requirement, products can be added/removed before performing the checkout action.

Feedback and Knowledge Base