QA Automation Robot Framework

Page Object Model (POM) in Robot Framework with Selenium and Python

Page Object Model

Introduction: 

Page Object Model (POM) is a design pattern in which we can maintain the page elements in separate files. We have multiple pages in our application and every page has a different file or we can specify elements from different pages into one file. So, the test cases will be divided into different parts. 

Prerequisite: 

  • Install PyCharm IDE
  • Download Selenium Browser Drivers for the browsers 
  • Install selenium package 
  • Install robotframework package 
  • Install robotframework-seleniumlibrary package 
  • Install IntelliBot #patched plugin  

Comparison Between Non-POM based and POM based Projects: 

Non-POM-based project:  

Usually while automating test cases, we analyze applications that have multiple pages. We have test cases, and every test case includes page elements, corresponding keywords to perform operations, and test case steps. So, in this approach the issue that arises is that suppose we have a test case1 where we need only login page elements and we have test case2 where login page elements as well as booking a flight elements page is needed.

So, in these 2 test cases, we are identifying login page elements in both the test cases. If login is required for multiple test cases, we need to specify the same elements in different test cases multiple times which causes repetition. Also, the other problem that arises is that in case in future if login page elements get modified or attributes are changed then we need to again modify each and every test case wherever we have those elements. 

POM-based project 

To overcome the above drawback of non-POM-based projects, we have another approach called Page Object Model Pattern. Using this we can specify one file which contains only page elements. Also, for every test case, different keywords are required that can be created as separate files, and in the test case, we can just specify the test steps which means if we have a test case1 that has only test steps those test steps will use the keywords from keywords file and those keywords will use the elements from the page element file.

So, the advantage of doing this is that we do not have to specify the element in every test case where it is needed. It can just be referred from the elements file. This is how we can overcome the repetition issue. Also, the reusability is achieved and in case of any modification of the elements, we just have to modify one file i.e., the elements file. 

Folder Structure: 

The folder structure contains three directories as follows: 

  • Page Object directory: It maintains the files which contain page elements. 

(In this directory we must create python files where the locators will be stored in robot framework syntax and kept into a variable.) 

  • Resources directory: It maintains keyword files that contain user-defined keywords. 

(Here we must create a robot file, and refer to the locators mentioned in the Page Object directory by providing its location in the settings section using the variables keyword and specify the variables created in the elements file to pass values to them) 

  • Testcases directory: It contains multiple test case files consisting of test steps. 

(In this directory we must create a robot file and it should use the keywords defined in the file from the Resources directory by providing its location in the settings section using the Resource keyword.) 

Project folder structure

Steps for writing test cases using Page Object Model – Design Pattern: 

  • Step 1: Create a class file with .py extension inside the Page Object directory which should include the page elements.  
Test case- Page Object Model step 1
  • Step 2: Create a file with .robot extension inside the Resources directory. It should include the user-defined keywords to perform operations on the elements.
Test case- Page Object Model step 2
  • Step 3: Create a Test file with .robot extension inside the TestCases directory. This should use the keywords that are defined in the keywords file of the Resources folder.
Test case- Page Object Model step 3
  • Step 4: Execute the Test file using the below command: 
Test case- Page Object Model step 4

The Test file will internally call the keywords file and the keywords file will internally use the file containing page elements. 

  • Step 5: Check the HTML reports created by Robot Framework 

Log.html: 

log.html

Report.html: 

report.html

Conclusion: 

Page Object Model-Design Pattern makes our code optimized, It is useful in reducing code duplication and improves test case maintenance. By using this pattern we can make our code more readable and it also helps in maintaining the code efficiently.