How to extract and verify textual patterns in a test case with Katalon Recorder
In a test project, you might want to verify that the application under test (AUT) displays information in the correct pattern. For example:
- Price tag pattern of a shopping item.
- Email address pattern.
- Phone number pattern.
Katalon Recorder supports this pattern matching process with several commands. Specifically, commands in Katalon Recorder can take patterns as input to verify several data types on an AUT.
This tutorial shows you how to extract and verify displayed text on an AUT. The pattern used in our case is a regular expression (regex): a sequence of characters that specifies a search pattern.
- To use the sample project of this tutorial, navigate toActions > Sample Projects, then add the Extract data and verify textual patterns project.
Test case scenario
In our example, we have a scenario "extract and verify item price," which consists of these steps:
- Navigate to the AUT - Katalon Zack Market:
https://demo-store.katalon.com
. - On the homepage, select an item.
- On the item details page, extract the item price.
- Verify that the item price is in the correct pattern: a currency symbol (
$
) followed by a numeric value.
Our AUT displays the price text of an item as follows:
We can see that the price text consists of the item price ($25.99
) and the currency code (CAD
).
To automate the scenario with Katalon Recorder, we propose the following process:
- Record the test case: we record the test case to open the AUT and select an item.
- Extract and verify textual patterns: we manually modify the recorded test case to select and verify the price text.
Record the test case
-
In Katalon Recorder, create a test suite and a test case, then click Record to start recording.
-
In an active browser tab, navigate to the AUT - Katalon Zack Market:
https://demo-store.katalon.com
.
-
On the displayed homepage, select an item.
-
In Katalon Recorder, click Stop to stop the test recording.
Extract and verify textual patterns
To complete the scenario, we manually modify the test case to extract only the price from the displayed price text. Then, we verify that the price is in the correct pattern using a regex.
Open the recorded test case and follow these steps:
-
Store the displayed price text. We want to store the price text into a variable.
To do so, we use the
storeText
command.
We need to capture the Target (the price text locator) for the command. Click on the Selector tool, then hover the cursor over and select the price text on the page.
In our case, the captured locator is the XPath:
xpath=//div[@id='root']/div/div/div/div[2]/div/div[2]/div/div[3]
. We then store the text into thedisplayedPrice
variable.
Command Target Value storeText
xpath=//div[@id='root']/div/div/div/div[2]/div/div[2]/div/div[3]
displayedPrice
-
Extract the item price.
We use the
storeEval
command to extract the item price$25.99
(the first six characters) from the text.
Command Target Value storeEval
"${displayedPrice}".substring(0, 6)
itemPrice
The
storeEval
command evaluates a Javascript expression, then stores the result into a variable. In our example, the target Javascript expression is"${displayedPrice}".substring(0, 6)
. Here,substring()
is a method that extracts characters from a string, given two indices.The expression evaluates the
displayedPrice
variable into its value. Then it extracts and stores the first six characters into theitemPrice
. -
Verify the item price with regex.
We use the
verifyEval
command with a regex in the Value field. This command verifies that the item price is displayed in the correct price pattern.
Command Target Value verifyEval
"${itemPrice}"
regexp:^[$](\d+\.\d+)
To input a regex as a value, we prefix the expression with
regexp:
. For our purpose, we use the regex^[$](\d+\.\d+)
that checks if the item price starts with a dollar sign and ends with a numeric value. -
Play the test case and verify the Log view results.