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实现telnet客户端的方法
Apr 15 Python
详解设计模式中的工厂方法模式在Python程序中的运用
Mar 02 Python
Python网络爬虫实例讲解
Apr 28 Python
numpy 对矩阵中Nan的处理:采用平均值的方法
Oct 30 Python
python3 深浅copy对比详解
Aug 12 Python
python实现猜数字游戏
Mar 25 Python
如何使用python传入不确定个数参数
Feb 18 Python
keras slice layer 层实现方式
Jun 11 Python
Matplotlib 折线图plot()所有用法详解
Jul 28 Python
python 星号(*)的多种用途
Sep 21 Python
使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)
Oct 12 Python
Python用摘要算法生成token及检验token的示例代码
Dec 01 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防止SQL注入详解及防范
2013/11/12 PHP
PHP使用内置dir类实现目录遍历删除
2015/03/31 PHP
php提高网站效率的技巧
2015/09/29 PHP
php实现压缩合并js的方法【附demo源码下载】
2016/09/22 PHP
详解PHP神奇又有用的Trait
2019/03/25 PHP
php设计模式之装饰模式应用案例详解
2019/06/17 PHP
laravel 解决ajax异步提交数据,并还回填充表格的问题
2019/10/15 PHP
JQuery在光标位置插入内容的实现代码
2010/06/18 Javascript
jQuery 属性选择器element[herf*='value']使用示例
2013/10/20 Javascript
初识SmartJS - AOP三剑客
2014/06/08 Javascript
JQuery包裹DOM节点的方法
2015/06/11 Javascript
使用jquery实现仿百度自动补全特效
2015/07/23 Javascript
jQuery插件zTree实现的基本树与节点获取操作示例
2017/03/08 Javascript
ES6中参数的默认值语法介绍
2017/05/03 Javascript
十大 Node.js 的 Web 框架(快速提升工作效率)
2017/06/30 Javascript
JS图片轮播与索引变色功能实例详解
2017/07/06 Javascript
js/jQuery实现全选效果
2019/06/17 jQuery
jquery使用echarts实现有向图可视化功能示例
2019/11/25 jQuery
vue webpack build资源相对路径的问题及解决方法
2020/06/04 Javascript
JSON stringify方法原理及实例解析
2020/10/23 Javascript
详解python使用Nginx和uWSGI来运行Python应用
2018/01/09 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
2018/07/11 Python
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
2018/11/30 Python
Python实现判断一个整数是否为回文数算法示例
2019/03/02 Python
python rsync服务器之间文件夹同步脚本
2019/08/29 Python
keras实现多GPU或指定GPU的使用介绍
2020/06/17 Python
python实现简单的五子棋游戏
2020/09/01 Python
Yahoo-PHP面试题4
2012/05/05 面试题
中医药大学市场营销专业自荐信
2013/09/29 职场文书
试用期转正鉴定评语
2014/01/27 职场文书
财务总经理岗位职责
2014/02/16 职场文书
校庆标语集锦
2014/06/25 职场文书
竞选班干部演讲稿500字
2014/08/20 职场文书
公司新人试用期自我评价
2014/09/17 职场文书
小学中队活动总结
2015/05/11 职场文书
《藏戏》教学反思
2016/02/23 职场文书