Go to Python to implement Pagefactory mode

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/fnng/p/5092383.html

The concept of pagefactory is primarily the Pagefactory class built into Java.

Import org.openqa.selenium.support.PageFactory;  ......

Example, http://libin0019.iteye.com/blog/1260090

This class is not in Python (Selenium). The concept of Pagefactory and Page object should be similar and belong to a design pattern. So it is not confined to language and scenes. So, curious, since Java has, that Python should also have similar gameplay. And really let me find a similar implementation.

The original is here: https://jeremykao.wordpress.com/2015/06/10/pagefactory-pattern-in-python/

So, with the help of Google Translate plus code to run, to understand this buddy want to use Pagefactory mode to achieve something, in order to facilitate your understanding, I moved here to jump to conclusions.

The element positioning in the selenium in Python is this:

find_element_by_id ("kw") Find_element_by_xpath ("//*[@id = ' kw ']")

Or something like this:

From selenium.webdriver.common.by import byfind_element (by.id, "kw") find_element (By.xpath, "//*[@id = ' kw ']")

With the implementation of the pagefactory pattern, the element positioning can be changed to this:

From Pageobject_support import callable_find_by as find_byfind_by (id_= "kw") find_by (xpath= "//*[@id = ' kw ']")

Although small changes, it actually makes the code easier to read and understand.

The core implementation is the pageobject_support.py file:

__all__ = [' cacheable ', ' callable_find_by ', ' property_find_by ']def cacheable_decorator (lookup): def func (self): If not hasattr (self, ' _elements_cache '): Self._elements_cache = {} # {callable_id:element (s)} cache = S  Elf._elements_cache key = ID (lookup) If key not in cache:cache[key] = lookup (self) return Cache[key] return funccacheable = Cacheable_decorator_strategy_kwargs = [' id_ ', ' XPath ', ' link_text ', ' partial _link_text ', ' name ', ' tag_name ', ' class_name ', ' Css_selector ']def _callable_find_by (how, using, MULTIPL  E, cacheable, context, driver_attr, **kwargs): def func (self): # context-driver or a certain element if Context:ctx = Context () if callable (context) else context.__get__ (self) # or property else:c             tx = GetAttr (self, driver_attr) # "How" and ' using ' take precedence through keyword arguments if how and using: Lookup = CTX. find_elements if multiple else ctx.find_element return lookup (how, using) If Len (kwargs)! = 1 or Kwargs . Keys () [0] not in _strategy_kwargs:raise valueerror ("If ' how ' and ' using ' is not specified,        One and only one of the following "" Valid keyword arguments should is provided:%s. "% _strategy_kwargs" Key = Kwargs.keys () [0]; Value = kwargs[key] suffix = key[:-1] If key.endswith ('_') else key # find_element (s) _by_xxx prefix = ' Find_ Elements_by ' If multiple Else ' find_element_by ' lookup = getattr (ctx, '%s_%s '% (prefix, suffix)) return loo Kup (value) return Cacheable_decorator (func) if cacheable else funcdef callable_find_by (How=none, Using=none, multiple=f Alse, Cacheable=false, Context=none, driver_attr= ' _driver ', **kwargs): Return _callable_find_by (how, using, multiple, C Acheable, Context, Driver_attr, **kwargs) def property_find_by (How=none, Using=none, Multiple=false, Cacheable=False, Context=none, driver_attr= ' _driver ', **kwargs): Return Property (_callable_find_by (how, using, multiple, cacheable, con Text, driver_attr, **kwargs))

Take a look at specific examples:

From Pageobject_support import callable_find_by as Find_byfrom Selenium import Webdriverclass baidusearchpage (object): C2/>def __init__ (Self, driver):        self._driver = driver    Search_box = find_by (id_= "kw")    Search_button = Find_ by (id_= ' Su's)     def search (self, Keywords):        self.search_box (). Clear ()        Self.search_box (). Send_keys ( Keywords)        self.search_button (). Click () if __name__ = = ' __main__ ':    driver = webdriver. Chrome ()    driver.get ("https://www.baidu.com")    baidusearchpage (Driver). Search ("Selenium")    Driver.close ()

8 positioning methods are also encapsulated:

    • Id_ (to avoid conflicts with built-in keyword IDs, so there is an underscore suffix)
    • Name
    • Class_name
    • Css_selector
    • Tag_name
    • Xpath
    • Link_text
    • Partial_link_text

Of course, this is only a form of expression of the pagefactory pattern. In addition, I found another example of a pagefactory pattern.

Https://github.com/mattfair/SeleniumFactory-for-Python

Go to Python to implement Pagefactory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.