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 01 Python
搞笑的程序猿:看看你是哪种Python程序员
Jun 12 Python
Python本地与全局命名空间用法实例
Jun 16 Python
Python 12306抢火车票脚本
Feb 07 Python
python如何修改装饰器中参数
Mar 20 Python
python自动循环定时开关机(非重启)测试
Aug 26 Python
如何基于Python实现电子邮件的发送
Dec 16 Python
Python 项目转化为so文件实例
Dec 23 Python
如何通过Django使用本地css/js文件
Jan 20 Python
Python tornado上传文件的功能
Mar 26 Python
python实现俄罗斯方块小游戏
Apr 24 Python
Django多个app urls配置代码实例
Nov 26 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 字符串分割和比较
2009/10/06 PHP
PHP多个版本的分析解释
2011/07/21 PHP
php设置编码格式的方法
2013/03/05 PHP
php单态设计模式(单例模式)实例
2014/11/18 PHP
php+xml编程之xpath的应用实例
2015/01/24 PHP
php制作的简单验证码识别代码
2016/01/26 PHP
php实现给二维数组中所有一维数组添加值的方法
2017/02/04 PHP
js中将多个语句写成一个语句的两种方法小结
2007/12/08 Javascript
基于jquery实现一张图片点击鼠标放大再点缩小
2013/09/29 Javascript
动态加载JS文件的三种方法
2013/11/08 Javascript
JS延迟加载加快页面打开速度示例代码
2013/12/30 Javascript
jquery实现鼠标滑过显示二级下拉菜单效果
2015/08/24 Javascript
javascript实现对表格元素进行排序操作
2015/11/18 Javascript
大型JavaScript应用程序架构设计模式
2016/06/29 Javascript
详解刷新页面vuex数据不消失和不跳转页面的解决
2018/01/30 Javascript
微信小程序实现人脸识别
2018/05/25 Javascript
javascript导出csv文件(excel)的方法示例
2019/08/25 Javascript
小程序使用watch监听数据变化的方法详解
2019/09/20 Javascript
javascript运行机制之执行顺序理解
2020/08/03 Javascript
微信小程序实现倒计时功能
2020/11/19 Javascript
为python设置socket代理的方法
2015/01/14 Python
Python基于Flask框架配置依赖包信息的项目迁移部署
2018/03/02 Python
深入浅析Python2.x和3.x版本的主要区别
2018/11/30 Python
python中实现控制小数点位数的方法
2019/01/24 Python
pandas数据筛选和csv操作的实现方法
2019/07/02 Python
html5 Canvas画图教程(7)—canvas里画曲线之quadraticCurveTo方法
2013/01/09 HTML / CSS
优秀党员主要事迹
2014/01/19 职场文书
高中历史教学反思
2014/02/08 职场文书
法制宣传标语
2014/06/23 职场文书
社团活动总结怎么写
2014/06/30 职场文书
党的群众路线教育实践活动查摆问题自查报告
2014/10/10 职场文书
干部考核工作总结2015
2015/07/24 职场文书
先进个人主要事迹范文
2015/11/04 职场文书
CSS 实现多彩、智能的阴影效果
2021/05/12 HTML / CSS
python使用pymysql模块操作MySQL
2021/06/16 Python
关于mysql中string和number的转换问题
2022/06/14 MySQL