Skip to content Skip to sidebar Skip to footer

Does Webdriver Support Pagefactory For Python?

I was reading about page objects and design patterns on the Webdriver project site and came across pagefactory. It doesn't look like the Webdriver for Python API includes pagefact

Solution 1:

I don't think there is any equivalents of the Java annotations (@Find(By.xxx) etc) in Python. But it doesn't mean that you can't use the PageObject pattern.

You can find good example on how to do here : https://github.com/SeleniumHQ/selenium/blob/master/py/test/selenium/webdriver/common/google_one_box.py

Solution 2:

Dynamically-typed languages like Python are less obsessed by design patterns to create objects - because it is trivially easy just create object of any type (with proper methods) and return it. Patterns are common solutions to common problems. If something is not a problem, you don't need a pattern to deal with it :-) OOP was initially a design pattern in C.

Edit, Dec 2017:

In our homegrown framework for page automation (for automated UI testing and other purposes), we do use pageobject design pattern, but had no need for a page factory. Old-school inheritance from our custom BasePage covered all our (quite diversified) needs. We do use few tricks to create page elements and make sure that proper page was instantiated, and based on that experience I like that our PageObject is simple.

Also, Python allows for multiple inheritance, if your needs grow more complicated.

In my experience (using Python, Selenium and WebDriver for more than 5 years now), lack of page factory pattern is a sign that you don't need it, not that it cannot be implemented.

Solution 3:

I have created a module called pageobject_support that implements PageFactory pattern in a pythonic way.

With this module, Google Search page could be modelled as follows:

from pageobject_support import cacheable, callable_find_by as find_by
from selenium.webdriver.common.by.By

classGoogleSearchPage(object):

    _search_box = find_by(how=By.NAME, using='q', cacheable=True)

    _search_button = find_by(name='btnK')

    def__init__(self, driver):
        self._driver = driver

    defsearch(self, keywords):
        self._search_box().click()
        self._search_box().send_keys(keywords)
        self._search_button().click()

Your feedback is appreciated. For more details, please visit https://jeremykao.wordpress.com/2015/06/10/pagefactory-pattern-in-python/

Post a Comment for "Does Webdriver Support Pagefactory For Python?"