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 相关文章推荐
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
Dec 04 Python
Python获取单个程序CPU使用情况趋势图
Mar 10 Python
星球大战与Python之间的那些事
Jan 07 Python
Python学生信息管理系统修改版
Mar 13 Python
Tensorflow使用tfrecord输入数据格式
Jun 19 Python
Python 判断奇数偶数的方法
Dec 20 Python
详解Python函数式编程—高阶函数
Mar 29 Python
实例详解Python装饰器与闭包
Jul 29 Python
selenium+python实现自动登陆QQ邮箱并发送邮件功能
Dec 13 Python
解决python中显示图片的plt.imshow plt.show()内存泄漏问题
Apr 24 Python
解决keras使用cov1D函数的输入问题
Jun 29 Python
Python实现http接口自动化测试的示例代码
Oct 09 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
使用网络地址转换实现多服务器负载均衡
2006/10/09 PHP
PHP之生成GIF动画的实现方法
2013/06/07 PHP
php实现的递归提成方案实例
2015/11/14 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
2015/12/31 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
2016/10/20 PHP
php mysql like 实现多关键词搜索的方法
2016/10/29 PHP
基于prototype的validation.js发布2.3.4新版本,让你彻底脱离表单验证的烦恼
2006/12/06 Javascript
强悍无比的WEB开发好助手FireBug(Firefox Plugin)
2007/01/16 Javascript
Fastest way to build an HTML string(拼装html字符串的最快方法)
2011/08/20 Javascript
React组件的三种写法总结
2017/01/12 Javascript
详解Node项目部署到云服务器上
2017/07/12 Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
2017/10/31 Javascript
vuejs router history 配置到iis的方法
2018/09/20 Javascript
支付宝小程序自定义弹窗dialog插件的实现代码
2018/11/30 Javascript
Vue 利用指令实现禁止反复发送请求的两种方法
2019/09/15 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
vue打开子组件弹窗都刷新功能的实现
2020/09/21 Javascript
python比较两个列表是否相等的方法
2015/07/28 Python
将Python代码打包为jar软件的简单方法
2015/08/04 Python
wxpython中自定义事件的实现与使用方法分析
2016/07/21 Python
pandas使用apply多列生成一列数据的实例
2018/11/28 Python
解决Python3用PIL的ImageFont输出中文乱码的问题
2019/08/22 Python
python 浮点数四舍五入需要注意的地方
2020/08/18 Python
python批量合成bilibili的m4s缓存文件为MP4格式 ver2.5
2020/12/01 Python
HTML5 和小程序实现拍照图片旋转、压缩和上传功能
2018/10/08 HTML / CSS
仿酷狗html5手机音乐播放器主要部分代码
2013/05/15 HTML / CSS
英国领先的运动物理治疗供应公司:Vivomed
2018/07/14 全球购物
医学生实习自荐信
2013/10/01 职场文书
自我鉴定怎么写
2013/12/05 职场文书
财务主管自我鉴定
2014/01/17 职场文书
天河观后感
2015/06/11 职场文书
七一活动主持词
2015/06/29 职场文书
2015年度考核个人工作总结
2015/10/24 职场文书
Vue详细的入门笔记
2021/05/10 Vue.js
前端vue+express实现文件的上传下载示例
2022/02/18 Vue.js
《金肉人》米特&《航海王》阿鹤声优松岛实因胰脏癌去世 享寿81岁
2022/04/13 日漫