Table Handling in Robot Framework
In Robot Framework, table handling is commonly used to read, validate, iterate, and extract data from tables on web pages or API responses.
There are two common types of table handling:
- Web Table Handling – Using SeleniumLibrary to interact with HTML tables.
- API Table Handling – Using RequestsLibrary and JSONLibrary to validate tabular data returned by APIs.
Let's understand both with examples.
1. Web Table Handling using SeleniumLibrary
Consider the following HTML table:
Employee Details
| ID | Name | Department | Salary |
|---|---|---|---|
| 101 | Sumit | QA | 50000 |
| 102 | Amit | Development | 60000 |
| 103 | Rahul | Testing | 55000 |
Install Required Library
pip install robotframework
pip install robotframework-seleniumlibrary
Robot Framework Example
File: table_handling.robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://example.com
${BROWSER} chrome
*** Test Cases ***
Handle HTML Table
Open Browser ${URL} ${BROWSER}
Maximize Browser Window
# Get total number of rows
${rows}= Get Element Count //table/tbody/tr
Log To Console Total Rows: ${rows}
# Get total number of columns
${columns}= Get Element Count //table/thead/tr/th
Log To Console Total Columns: ${columns}
# Get complete table data
${data}= Get WebElements //table/tbody/tr
FOR ${row} IN @{data}
${text}= Get Text ${row}
Log To Console ${text}
END
Close Browser
Note: Replace the URL and XPath with the actual application URL and table locators.
2. Get Specific Cell Data
Use XPath to retrieve a particular cell from the table.
*** Test Cases ***
Get Specific Cell Value
Open Browser https://example.com chrome
${value}= Get Text //table/tbody/tr[1]/td[2]
Log To Console Employee Name: ${value}
Close Browser
XPath Explanation
tr[1]– First row.td[2]– Second column.- Result – Employee name.
3. Get All Rows and Columns
*** Test Cases ***
Read All Table Data
Open Browser https://example.com chrome
${row_count}= Get Element Count //table/tbody/tr
${col_count}= Get Element Count //table/tbody/tr[1]/td
FOR ${row} IN RANGE 1 ${row_count + 1}
FOR ${col} IN RANGE 1 ${col_count + 1}
${value}= Get Text
... //table/tbody/tr[${row}]/td[${col}]
Log To Console ${value}
END
END
Close Browser
4. Verify Table Contains Particular Data
*** Test Cases ***
Verify Employee Exists
Open Browser https://example.com chrome
${result}= Get Text
... //table//tr[td[normalize-space()='Sumit']]
Should Contain ${result} Sumit
Close Browser
5. Find Row Based on Column Value
For example, find the employee whose ID is 102 and validate the department.
*** Test Cases ***
Find Employee By ID
Open Browser https://example.com chrome
${department}= Get Text
... //table/tbody/tr[td[1]='102']/td[3]
Should Be Equal ${department} Development
Close Browser
6. Click a Button in a Specific Table Row
*** Test Cases ***
Click Employee Edit Button
Open Browser https://example.com chrome
Click Element
... //table/tbody/tr[td[1]='102']//button[text()='Edit']
Close Browser
This is useful when each row has Edit, Delete, View, or other action buttons.
7. API Table Handling in Robot Framework
When an API returns JSON containing an array of records, you can iterate through the records just like table rows.
Install Libraries
pip install robotframework-requests
pip install robotframework-jsonlibrary
Example API Response
{
"employees": [
{
"id": 101,
"name": "Sumit",
"department": "QA"
},
{
"id": 102,
"name": "Amit",
"department": "Development"
}
]
}
Robot Framework Example
*** Settings ***
Library RequestsLibrary
Library Collections
*** Test Cases ***
Handle API Table Data
Create Session api https://example.com
${response}= GET On Session api /employees
${employees}= Get From Dictionary
... ${response.json()} employees
FOR ${employee} IN @{employees}
${id}= Get From Dictionary ${employee} id
${name}= Get From Dictionary ${employee} name
${department}= Get From Dictionary ${employee} department
Log To Console ${id} - ${name} - ${department}
END
8. Important Table Handling Keywords
| Keyword | Purpose |
|---|---|
Get Element Count |
Count table rows or columns |
Get Text |
Get cell or row text |
Get WebElements |
Get multiple table elements |
Get WebElement |
Get a specific element |
Click Element |
Click a button inside a row |
Should Contain |
Validate table content |
Should Be Equal |
Compare cell values |
Get From Dictionary |
Extract API response values |
Get From List |
Extract a particular row from a list |
9. Practical Interview Scenarios
Some commonly asked Robot Framework table-handling scenarios:
- How do you count the number of rows and columns?
- How do you retrieve a particular cell value?
- How do you read all table records?
- How do you find a row using a unique ID?
- How do you validate whether a particular record exists?
- How do you click Edit or Delete for a particular row?
- How do you handle pagination in HTML tables?
- How do you handle dynamic tables?
- How do you validate sorting in a table?
- How do you compare UI table data with API response data?
Interview Tip
The most useful technique is dynamic XPath using a unique column value, for example:
//table/tbody/tr[td[1]='102']/td[3]
This allows you to find a row dynamically rather than hardcoding its row number.
I can also explain advanced Robot Framework table handling, including pagination, sorting, filtering, dynamic tables, and comparing UI table data against API responses with a complete runnable example.

No comments:
Post a Comment