在Python中实现PageFactory模式
2023-09-14 08:57:56 时间
例子,http://libin0019.iteye.com/blog/1260090
Python(Selenium)中没有这个类。 PageFactory 的概念和Page Object应该类似,属于一种设计模式。所以并不局限于语言及场景。于是,好奇,既然Java有,那Python也应该有类似的玩法。还真让我给找到了类似的实现。
原文在此:https://jeremykao.wordpress.com/2015/06/10/pagefactory-pattern-in-python/
于是,就借助谷歌翻译加代码运行,弄懂了这哥们想要利用PageFactory 模式实现个什么东西,为了便于你的理解,我这里搬运过来给下结论。
selenium in python中的元素定位是这样的:
find_element_by_id("kw") find_element_by_xpath("//*[@id=kw]")
或者是这样的:
from selenium.webdriver.common.by import By find_element(By.ID,"kw") find_element(By.XPATH,"//*[@id=kw]")
通过PageFactory 模式的实现可以把元素定位变成这样的:
from pageobject_support import callable_find_by as find_by find_by(id_="kw") find_by(xpath="//*[@id=kw]")
别看小小的改动,它其实使代码更容易的阅读和理解。
核心实现就是pageobject_support.py文件:
__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 = self._elements_cache key = id(lookup) if key not in cache: cache[key] = lookup(self) return cache[key] return func cacheable = cacheable_decorator _strategy_kwargs = [id_, xpath, link_text, partial_link_text, name, tag_name, class_name, css_selector] def _callable_find_by(how, using, multiple, 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: ctx = getattr(self, driver_attr) # how AND using take precedence over 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 are not specified, one and only one of the following " "valid keyword arguments should be 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 lookup(value) return cacheable_decorator(func) if cacheable else func def callable_find_by(how=None, using=None, multiple=False, cacheable=False, context=None, driver_attr=_driver, **kwargs): return _callable_find_by(how, using, multiple, cacheable, 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, context, driver_attr, **kwargs))
然后,我再帖一下具体的例子:
from pageobject_support import callable_find_by as find_by from selenium import webdriver class BaiduSearchPage(object): def __init__(self, driver): self._driver = driver search_box = find_by(id_="kw") search_button = find_by(id_=su) def search(self, keywords): self.search_box().clear() self.search_box().send_keys(keywords) self.search_button().click()
driver.close()
同样封装了8种定位方法:
id_ (为避免与内置的关键字ID冲突,所以多了个下划线的后缀) class_name css_selector tag_name xpath link_text partial_link_text
当然,这只是PageFactory 模式的一种表现形式而已。除此之外,我还找到了另外一个PageFactory模式的例子。
https://github.com/mattfair/SeleniumFactory-for-Python
这哥们是利用PageFactory模式把驱动的创建做了封装,感兴趣可以了解一下。
搬运完了,准备过年。新年快了~!!!
相关文章
- Python之单例模式的多种实现
- python实现简单工厂模式
- Python实现单例模式
- python+rabbitMQ实现生产者和消费者模式
- python-面向对象-12_模块和包
- Python使用设计模式中的责任链模式与迭代器模式的示例
- python:ERROR: No matching distribution found for Pillow==9.1.0的处理(Python 3.6.8)
- Atitit web httphandler的实现 java python node.js c# net php 目录 1.1. Java 过滤器 servelet1 1.2. Python的
- Python编程:利用python编程实现对基于时间序列的数据(dataframe格式)按照指定时间范围进行单方向关联,不存在的日期补充为默认的NaN
- Python:利用python代码编程实现将视频的avi格式转换为MP4格式
- Python编程语言学习:python的列表的特殊应用之一行命令实现if判断中的两类判断
- Python:利用python语言实现18位身份证号码和15位身份证号码相互转换
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(text.insert/link各种事件)
- Python之API:基于python语言调用华为云API(华为网站)实现特定功能
- 〖Python接口自动化测试实战篇⑧〗- 小案例 - 使用python实现接口请求 [查询天行数据]
- 从零开始学python | 使用Python映射,过滤和缩减函数:所有您需要知道的
- Python设计模式中单例模式的实现及在Tornado中的应用
- python之使用@classmethod实现单例模式(亲测可用)
- Python只需要三十行代码,打造一款简单的人工语音对话
- python 设计模式之 (Chain of Responsibility)责任链模式
- python 设计模式之备忘录模式
- python 设计模式之状态模式
- python基础===八大排序算法的 Python 实现
- 设计模式(Python语言)----观察者模式
- Python编程语言学习:python中浅复制/深复制(或浅拷贝/深拷贝)的简介、案例应用注意事项之详细攻略
- 〖Python自动化办公篇⑲〗 - python实现邮件自动化 - 邮件发送
- 〖Python自动化办公篇⑳〗 - python实现邮件自动化 - 发送html邮件和带附件的邮件