Robot Framework

How to handle Web Tables in Robot Framework 

Web Tables in Robot Framework

A Web table has rows and columns containing data that is sortable. We can use various keywords that are available in Robot Framework to perform various actions on the web table and even validate the table such as getting the count of all the rows present in the table, getting a count of all the columns present in the table, get data from a specific cell, validate the header, rows, columns, etc.      

In this blog we will discuss: 

  • How we can get data from the table 
  • How we can count the number of rows in a table 
  • How we can count the number of columns in a table 
  • How to perform validation on the table. 

Project setup for Web Table-  

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

To ensure that the required libraries are installed, use pip list command or manually navigate to: 

File > settings > project: robot automation> python interpreter 

Project setup

After verifying the prerequisites, create a file named ‘WebTables’  

— Create the file under the ‘Testcases’ directory with .robot extension >Enter 

File Name

Given below is an example for a better understanding of how we can handle a web table in our test.

Testcase: 

Step1: Visit https://the-internet.herokuapp.com/tables

Step2: Capture the data of a specific cell 

Step3: Get the count of columns in a specific row 

Step4: Get the total number of rows in the table 

Step5: Validate header 

Step6: Validate row 

Step7: Validate column 

Step8: Validate cell 

Step9: Close the browser 

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${browser}  chrome
${url}  https://the-internet.herokuapp.com/tables
*** Test Cases ***
Table Test
    open browser    ${url}  ${browser}
    maximize browser window
#Capture the data of a specific cell
    ${data}=    get text    xpath://table[@id='table1']/tbody/tr[2]/td[5]
#get the count of columns in a specific row
    ${Columns}=     get element count  xpath://table[@id='table1']/tbody/tr[2]/td
#get the total number of rows in the table
    ${Rows}=    get element count    xpath://table[@id='table1']/tbody/tr
Validations
#Validate header
    table header should contain     xapth://table[@id='table1']     Action
#Validate row
    table row should contain        xpath://table[@id='table1']     3       http://www.jdoe.com
#Validate column
    table column should contain    xpath://table[@id='table1']  5   Web Site
#Validate cell
    table cell should contain    xapth://table[@id='table1']    4   3   fbach@yahoo.com
   close browser

***Settings*** – To work with robot framework keywords we need to import   Library SeleniumLibrary in the settings section because all the keywords are built in the selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

***Variables*** – This section contains the defined variables that are used commonly, here we declared ${url} and ${browser} variables for browser and URL so that we can reuse these variables. In the same way, we can create more variables according to the project requirement. 

Web Tables Code

Code Explanation:

In the Table Test test case we visited the https://the-internet.herokuapp.com/tables URL in the Chrome browser, after which we were redirected to a web page having data tables from which we chose the first table and captured the data from its second row and fifth column by using get text keyword and stored the data that was captured into a variable named ${data}.

Then we got the count of the number of columns present in a single row by using get element count keyword and stored the count of columns that was received into a variable named ${Columns}. 

Next, we got the total count of the number of rows present in the table by using get element count keyword and stored the count of the rows that were received into a variable named ${Rows}.  

Later In the Validations test case, we performed various validations on the web table, we first validated the table header in which we checked if the header part of the table contains ‘Action’ as a header by using table header should contain keyword, then we validated if the third row of the table contains ‘http://www.jdoe.com’ as a value by using table row should contain keyword. Next, we validated the table column in which we checked if the fifth column contains ‘Web Site’ as a value by using table column should contain keyword. Lastly, we validated the cell of the table in which we checked if the fourth row and third column cell contains ‘fbach@yahoo.com’ as a value by using table cell should contain keyword, and finally ended the session with close browser keyword. 

Execution: 

To execute the test case we need to use the command below: 

Execution Command

After execution, we should get the result as a pass. 

Pass Result

Reports: 

After every execution, the robot framework creates detailed HTML reports. 

We get three files – log.html, output.xml, and report.html. 

Log.html: 

Web Tables log

Report.html: 

Web Tables Report

Read our next blog on “How to handle checkbox in Robot Framework”.