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实现向ppt文件里插入新幻灯片页面的方法
Apr 28 Python
编写Python脚本来实现最简单的FTP下载的教程
May 04 Python
编写Python小程序来统计测试脚本的关键字
Mar 12 Python
Python中json格式数据的编码与解码方法详解
Jul 01 Python
Python决策树分类算法学习
Dec 22 Python
对python pandas读取剪贴板内容的方法详解
Jan 24 Python
numpy.array 操作使用简单总结
Nov 08 Python
关于pandas的离散化,面元划分详解
Nov 22 Python
python 爬取B站原视频的实例代码
Sep 09 Python
Python timeit模块原理及使用方法
Oct 10 Python
使用gunicorn部署django项目的问题
Dec 30 Python
Python利用机器学习算法实现垃圾邮件的识别
Jun 28 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
使用zend studio for eclipse不能激活代码提示功能的解决办法
2009/10/11 PHP
PHP答题类应用接口实例
2015/02/09 PHP
浅谈PHP错误类型及屏蔽方法
2017/05/27 PHP
第一个JavaScript入门基础 document.write输出
2010/02/22 Javascript
js计算精度问题小结
2013/04/22 Javascript
JS逆序遍历实现代码
2014/12/02 Javascript
JS动态添加Table的TR,TD实现方法
2015/01/28 Javascript
JavaScript与ActionScript3两者的同性与差异性
2016/09/22 Javascript
Bootstrap基本组件学习笔记之进度条(15)
2016/12/08 Javascript
JavaScript和JQuery获取DIV值的方法示例
2017/03/07 Javascript
基于Jquery Ajax type的4种类型(详解)
2017/08/02 jQuery
layui点击按钮添加可编辑的一行方法
2018/08/15 Javascript
最适应的vue.js的form提交涉及多种插件【推荐】
2018/08/27 Javascript
vue 左滑删除功能的示例代码
2019/01/28 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
2020/12/08 Vue.js
vue3.0实现插件封装
2020/12/14 Vue.js
Python中的jquery PyQuery库使用小结
2014/05/13 Python
python避免死锁方法实例分析
2015/06/04 Python
详解Python pygame安装过程笔记
2017/06/05 Python
python实现感知器
2017/12/19 Python
python 中的list和array的不同之处及转换问题
2018/03/13 Python
Python企业编码生成系统之系统主要函数设计详解
2019/07/26 Python
python实点云分割k-means(sklearn)详解
2020/05/28 Python
《记承天寺夜游》教学反思
2014/02/16 职场文书
党员岗位承诺书
2014/03/25 职场文书
父母对孩子的寄语
2014/04/09 职场文书
医药营销个人求职信
2014/04/12 职场文书
小学家长学校培训材料
2014/08/24 职场文书
九一八事变演讲稿范文
2014/09/14 职场文书
2014最新实习证明模板
2014/10/02 职场文书
医院护士党的群众路线教育实践活动对照检查材料思想汇报
2014/10/04 职场文书
承诺书范本
2015/01/21 职场文书
办公室岗位职责
2015/02/04 职场文书
幼儿园小班开学寄语
2015/05/27 职场文书
2016年党校科级干部培训班学习心得体会
2016/01/06 职场文书
python如何查找列表中元素的位置
2022/05/30 Python