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 pagefactory. Is this true?
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/
来源:https://stackoverflow.com/questions/8469530/does-webdriver-support-pagefactory-for-python