Sometimes, instead of asserting or verifying that the data is as expected, you need to get the data; either in order manipulate it or use it later.
For this QWeb provides a number of "getters", i.e keywords for getting data to variables. These keywords usually begin with word Get. Here is a list of Get keywords in QWeb:
-
GetAlertText
-
GetCellText
-
GetConfig
-
GetDropDownValues
-
GetElementCount
-
GetFileText
-
GetInputValue
-
GetList
-
GetPdfText
-
GetSelected
-
GetTableRow
-
GetText
-
GetTextCount
-
GetWebelement
Many of these will be covered in later chapters (for example GetCellText & GetTableRow in chapter 10 - Tables
), but we’ll cover most common ones below.
GetText is meant for getting element’s text into a variable. It takes at least one mandatory argument, locator. Locator can be either xpath or any attribute value of an element with specific tag defined. For example:
*** Settings ***
Library QWeb
*** Test Cases ***
GetText examples
${text}= GetText //button[@data-toggle\="dropdown"] # using xpath
${text}= GetText dropdown tag=button # using tag+attribute value
GetTextCount returns the amount of specific texts on the page. It takes the text being searched for as an argument.
*** Settings ***
Library QWeb
*** Test Cases ***
GetTextCount examples
${count}= GetTextCount New York # waiting default 10 secs before returning 0
GetInputValue returns the text written in an input field. It has mandatory argument locator, which can be text, xpath or any unique attribute’s value.
*** Settings ***
Library QWeb
*** Test Cases ***
GetInputValue examples
${text}= GetInputValue Full Name # using label near text field
${text}= GetInputValue name # using attribute value (id = name)
${text}= GetInputValue //input[@id\="name"] # using xpath
${text}= GetInputValue Haku # using attribute value (alt text)
Build on top of the exercise on previous chapter, but this time get data to variables and just log it using Log keyword:
-
Add product "Scar the Lion" to shopping cart
-
(NEW): Get price of the item to variable ${item_price} and log it
-
Select "Large" as size of a t-shirt
-
Set quantity of products to 2
-
Checkout
-
Fill in "Full Name", "Email" and "Billing Address"
-
(NEW): Get total price of all items to variable ${total_price} and log it
-
Change your mind and edit cart, remove product from cart and verify cart is empty
💡
|
GetText returns value in an element immediately without waiting. You may want to take this into account and verify something from the new view before getting data, since sometimes it might take a while before text is actually available. |
Exercise example solution can be found the repo. Please try to implement a solution first before looking at the solution.
Run this solution from repo root by:
robot ./04/04_solution.robot