PageFactory设计模式基于python实现


Posted in Python onApril 14, 2020

前言

pageFactory的设计模式能在java里执行的原因是java自带了PageFactory类,而在python中没有这样的包,但是已经有人写好了pageFactory在python的包,可以拿来用

pageFactory 用于python支持的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 list(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 = list(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 by
from selenium import webdriver
from time import sleep
class BaiduSearchPage(object):
  def __init__(self, driver):
    self._driver = driver #初始化浏览器的api
  search_box = by(id_="kw")
  search_button = by(id_='su')
  def search(self, keywords):
    self.search_box().clear()
    self.search_box().send_keys(keywords)
    self.search_button().click()

支持的定位api

  • id_ (为避免与内置的关键字ID冲突,所以多了个下划线的后缀)
  • name
  • class_name
  • css_selector
  • tag_name
  • xpath
  • link_text
  • partial_link_text

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python进阶教程之异常处理
Aug 30 Python
windows下Virtualenvwrapper安装教程
Dec 13 Python
Python编程使用*解包和itertools.product()求笛卡尔积的方法
Dec 18 Python
Python代码缩进和测试模块示例详解
May 07 Python
python sort、sort_index方法代码实例
Mar 28 Python
Python跳出多重循环的方法示例
Jul 03 Python
python动态文本进度条的实例代码
Jan 22 Python
python 遗传算法求函数极值的实现代码
Feb 11 Python
django日志默认打印request请求信息的方法示例
May 17 Python
Python3内置函数chr和ord实现进制转换
Jun 05 Python
零基础学Python之前需要学c语言吗
Jul 21 Python
OpenCV读取与写入图片的实现
Oct 13 Python
Jupyter notebook 远程配置及SSL加密教程
Apr 14 #Python
jupyter note 实现将数据保存为word
Apr 14 #Python
Python连接Hadoop数据中遇到的各种坑(汇总)
Apr 14 #Python
jupyter notebook 调用环境中的Keras或者pytorch教程
Apr 14 #Python
Python用5行代码实现批量抠图的示例代码
Apr 14 #Python
在jupyter notebook中调用.ipynb文件方式
Apr 14 #Python
使用jupyter notebook将文件保存为Markdown,HTML等文件格式
Apr 14 #Python
You might like
php解析json数据实例
2014/08/19 PHP
举例详解PHP脚本的测试方法
2015/08/05 PHP
是 WordPress 让 PHP 更流行了 而不是框架
2016/02/03 PHP
function, new function, new Function之间的区别
2007/03/08 Javascript
javascript 写类方式之八
2009/07/05 Javascript
jQuery源码分析之Event事件分析
2010/06/07 Javascript
JS 精确统计网站访问量的实例代码
2013/07/05 Javascript
使用 JavaScript 进行函数式编程 (一) 翻译
2015/10/02 Javascript
基于JS实现数字+字母+中文的混合排序方法
2016/06/06 Javascript
jQuery拖拽通过八个点改变div大小
2020/11/29 Javascript
以BootStrap Tab为例写一个前端组件
2017/07/25 Javascript
简单快速的实现js计算器功能
2017/08/17 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
webpack配置proxyTable时pathRewrite无效的解决方法
2018/12/13 Javascript
使用 vue 实例更好的监听事件及vue实例的方法
2019/04/22 Javascript
python获取当前日期和时间的方法
2015/04/30 Python
浅析python协程相关概念
2018/01/20 Python
python递归全排列实现方法
2018/08/18 Python
Python为何不能用可变对象作为默认参数的值
2019/07/01 Python
nginx黑名单和django限速,最简单的防恶意请求方法分享
2019/08/09 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
如何理解Python中包的引入
2020/05/29 Python
Python局部变量与全局变量区别原理解析
2020/07/14 Python
美国玛丽莎收藏奢华时尚商店:Marissa Collections
2016/11/21 全球购物
英国当代时尚和街头服饰店:18montrose
2018/12/15 全球购物
吉尔德利巧克力公司:Ghirardelli Chocolate Company
2019/03/27 全球购物
介绍java中初始化块的使用
2012/09/11 面试题
自考毕业生自我鉴定
2013/11/04 职场文书
会计专业大学生求职信范文
2014/01/28 职场文书
中药学专业求职信
2014/05/31 职场文书
全国法制宣传日活动总结
2015/05/05 职场文书
民事诉讼代理词
2015/05/25 职场文书
钢琴师观后感
2015/06/12 职场文书
2015国庆节感想
2015/08/04 职场文书
nginx负载功能+nfs服务器功能解析
2022/02/28 Servers
Java实现添加条码或二维码到Word文档
2022/06/01 Java/Android