Does Webdriver support pagefactory for Python?

大憨熊 提交于 2019-12-03 13:17:56

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

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.

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

class GoogleSearchPage(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

    def search(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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!