page object model page factory in selenium python

Page Object Model  in Python

Web automaton testing is used for automated testing for website or web application, in order to test functionalities and load times, it also used in regression testing. Automation testing is important so far software development lifecycle because it reduces the complexity and it ensure that site is operating as per requirement.

Automation testing can be done through programming languages such as:
  • Python
  • Selenium
  • Javascript
  • Appium

Web automation increase day by day due  to  expand nature of websites or web-application. In order to handle big application manually is not possible, take a long time. When it come to automated testing  the first thing come our mind that Selenium. Selenium is most import tool user for automated web browser.

What is POM?

A POM is defined as the class in which you defined as the constructor of python and explicit methods for element initialization.

Advantages of Page Object Model

Testing Quality: It improves the readability  of the scripts

Site Maintenance: if something got change in any page easy way to change the locator.

Team Collaboration : it helps to collaboration between team members.

Reusing the code: it does not require to write the same function in different test cases.

What Is Page Factory?

Page Factory is defined as the class in which you defined as the the constructor of python and the locator are saved in a dictionary with the locator name as a key and tuple having its locator type and value as its value.

Locators

Page Object Model in Selenium with Python using selenium-page-factory:
Step are:
  • Install pip install selenium-page-factory

To use selenium-page-factory every Page in the Page Object Model should have a WebDriver object as a class member as shown below:

page class

Extended Web Elements Methods in selenium-page-factory

set text

Sample Project Structure for Page Object Model & Page Factory in Selenium Python
Pre-requisites:
  • IDE– PyCharm
  • Python– 3.4 or higher
  • Framework Used– Pytest

Package Used – Selenium, selenium-page-factory, pytest

Setup:
  • Create a Python project in Pycharm.
  • Add 2 Python Package src and test.
  • Add Page Classes under src.pages package.
  • Add test scripts under test package.

project

In POM structure we have a package for all the pages. This package consists of class files related to every page

python selenium

After that, we write all the functions which need to be performed on that page. For example Entering the user name, password,  etc. Syntax to write those functions is :

self locator

selenium page factory

Test_browserstack:

selenium webdriver

To run this code:

pytest-v

Write A Comment