How to extract and write data in a test case

When testing a website with Katalon Recorder, you might want to extract data from the website and store the data into an external file. Katalon Recorder facilitates this process of extracting and writing data with several commands.

This tutorial shows you how to use Katalon Recorder to extract data from a website and write the data to a CSV file.

In our example, we follow the scenario "extract data from the checkout page," which consists of these steps:

  1. Navigate to the application under test (AUT) - the Katalon E-commerce page: https://cms.demo.katalon.com.
  2. Add a few items to the cart.
  3. Navigate to the checkout page of the AUT: https://cms.demo.katalon.com/checkout/.
  4. On the checkout page, extract the names and prices of the added items.
  5. Write the extracted data to a CSV file.

After we follow the first three steps of the presented scenario, the checkout page is displayed as below:

Checkout Page Overview

Click the image to enlarge

Items added to the cart are organized in a table displaying names and prices.

To extract displayed data on an AUT, and write the data to a CSV file, the process is as follows:

  1. Prepare the CSV file: we create a CSV to store the data.
  2. Add the CSV file to the workspace: we add the CSV file to the Katalon Recorder workspace.
  3. Extract and write data: we create a test case to extract and write the data from the checkout page to the CSV file.

Prepare the CSV file

As Katalon Recorder navigates using column names in a CSV file, we need to create a CSV file with specific column names for data extraction. In our case, we need two columns for names and prices.

Prepared CSV file

Add the CSV file to the workspace

Follow these steps:

  1. Open Katalon Recorder. In the Workspace sidebar, click on the more icon in the Test Data section.

    Test Data section
  2. In the displayed file explorer, select the CSV file.

    File dialog
  3. The added CSV file is displayed under the Test Data section.

    Added CSV file

Notes:

As a browser extension, Katalon Recorder cannot write data directly to the user file system; the extension only writes to the version of the data file that is added to the workspace. Therefore, you need to download the data file again to view the extracted data.

Extract and write the data

After preparing and adding the CSV to the workspace, we locate the items on the checkout page by getting their associated XPath. Using the XPath, we create a test case to iterate over all items, extract the names and prices, and then write the data to the CSV file.

Get the associated XPath

To locate the items on the checkout page for data extraction, we need three types of XPath locators:

  • The XPath of a displayed item.
  • The XPath of the name of the item.
  • The XPath of the price of the item.

We use the browser Inspector tool to get the XPath locators.

Follow these steps:

  1. Get the XPath of an item. On the checkout page, right-click on the displayed table of items, then select Inspect.

    Inspect the items

    Click the image to enlarge

    The inspector window shows that the items are organized in an HTML table element, and each item is represented as a table row. Inspected table

    Click the image to enlarge

    To get the XPath of an item, right-click on a <tr> tag and select Copy > Copy XPath. Inspected table row

    Click the image to enlarge

  2. Get the XPath of an item name. In the inspector window, right-click on the <td> tag associated with the item name and select Copy > Copy XPath.

    Inspected item name

    Click the image to enlarge

  3. Get the XPath of an item price. In the inspector window, right-click on the <td> tag associated with the item price and select Copy > Copy XPath.

    Inspected item price

    Click the image to enlarge

The three XPath locators that we get are as follows:

Element XPath
Item //*[@id="order_review"]/table/tbody/tr[1]
Name //*[@id="order_review"]/table/tbody/tr[1]/td[1]
Price //*[@id="order_review"]/table/tbody/tr[1]/td[2]

From the XPath of the item element, we can see that each row (<tr>) in the table is associated with an ID value. For example, the XPath of the item that we copy contains the ID value 1. With this ID value, we can quickly iterate over all items in the table to extract the names and prices.

Extract and write data in a test case

Knowing that items are organized in an HTML table element, we propose the following process to extract the data:

  1. Navigate to the checkout page.
  2. Count the number of rows in the table.
  3. Start a loop to iterate over all items with the row count.
  4. For each row, extract the name and price of the associated item.
  5. Write the extracted name and price to the CSV file.
  6. Move on to the next item, and continue with step 4.
  7. When all items are processed, terminate the loop.

Follow these steps:

  1. Open a new Test Case. With the open command, navigate to the checkout page of the AUT.

    The URL for the checkout page is https://cms.demo.katalon.com/checkout/.

    KR open command
  2. Count the number of rows in the table.

    To iterate over all items in the table, we need the total number of rows. Here we use the storeXpathCount command with the target //*[@id="order_review"]/table/tbody/tr to count the number of rows. We then store the count into the rowNum variable.

    KR storeXpathCount
  3. Iterate over the items.

    Here we use two commands: store and while. The store command creates the ID variable to represent the ID value of an item. The while command starts a loop to iterate over the items in the table.

    To learn more about control flow commands in Katalon Recorder, refer to this guide: Handle conditional cases in your tests.

    KR store and while commands

    Here the while command checks if the current ID value is valid using the expression ${ID} < ${rowNum}.

  4. Store the name and price of an item.

    We use two storeText commands to store the item name and price into two variables.

    KR storeText command for price

    The Target and Value for the two storeText commands are:

    Element XPath Value
    Name //*[@id="order_review"]/table/tbody/tr[${ID}]/td[1] itemName
    Price //*[@id="order_review"]/table/tbody/tr[${ID}]/td[2] itemPrice
  5. Write data to the CSV file.

    We use the appendToCSV command to append the extracted name and price to the CSV file.

    KR storeText command for price

    The Target of the command is the name of the CSV file that we prepared. The added values to the CSV file, item name and price, are separated by a comma.

  6. Move on to the next item.

    To continue selecting and verifying the next item, we use the storeEval command to increment the value of the ID variable.

    KR storeEval endWhile commands

    The extracting and writing steps execute the while loop, so we use the endWhile command to terminate the loop.

  7. Play the test case and download the CSV file.

    KR results
  8. Open the downloaded CSV file and verify the results.

    KR downloaded CSV file

Feedback