Page-Object Pattern for Selenium Test Automation with Python

Page-object model is a pattern that you can apply it to develop efficient automation framework. With the page-model, it is possible to minimize maintenance cost. Basically page-object means that your every page is inherited from a base class which includes basic functionalities for every page. If you have some new functionalities that every page should have, you can simple add it to the base class. Base class is like the following:


In this part we are creating pages which are inherited from base page. Every page has its own functionalities written as python functions. Some functions return to a new page, it means that these functions leave the current page and produce a new page. You should write as much as functions you need in the assertion part because this is the only part you can use the webdriver functions to interact with web pages. This part can be evaluate as providing data to assertion part.  

The last part is related to asserting your test cases against to the pages you have by using the unit test class of the language you preferred. Actually, there are two opinions for asserting. The first one it should be page code,  and the second one is writing the assertion part in different place. Martin Fowler suggests not using assertion inside of page class.

One of the most maintenance required attributes is object locators. You can also solve this problem by inserting objects for each pages to a single object module by separating them with a container like in a class for each pages. 
My example is applied to the www.amazon.com. Why I chose Amazon is that it is modern, well-known and it is very suitable for automation because almost every needed objects have a unique id.  

If you want to run it, you should type: 
python <module-name.py> 

If you want to run it just a class, you should type: 
python <module-name.py> <class-name> 


You can pull my repo from github and run it. Don't forget to change valid email and password line in users.py.


Comments

  1. I am getting the following error

    line 3, in
    from pages import *

    line 6, in
    import users, functions

    ReplyDelete
    Replies
    1. Jay clone the project in github and try on it (https://github.com/gunesmes/page-object-python-selenium). I also update there as well.

      Delete
  2. Your article is a super easy introductory explanation for the Page-Object Pattern. I have been looking for an article like yours a long time. I love it!
    If you have spare time, would it be possible to extend the content with the addition of a few examples using Behave (BDD) rather than Unittest? I would greatly appreciate

    ReplyDelete
    Replies
    1. Thanks for refreshing the idea. I will try to add a post for page-object pattern with BDD.

      Delete
  3. Why do you have these in the pages.py ? What is the use of it? I am just learning page object and sorry if this is something silly

    class HomePage(Page):
    pass

    class SignUpPage(Page):
    pass

    ReplyDelete
    Replies

    1. Misha, they are not implemented yet since this is just a demo but will be done if needed. Means that new pages should be added to that module.

      Delete

Post a Comment

Popular posts from this blog

Selenium Error "Element is not currently interactable and may not be manipulated"

Change Default Timeout and Wait Time of Capybara

Performance Testing on CI: Locust is running on Jenkins

How to Set Shared Preferences in Espresso Test for Kotlin and Java