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插入排序算法的实现代码
Nov 21 Python
Python抽象类的新写法
Jun 18 Python
基于Python __dict__与dir()的区别详解
Oct 30 Python
Python实现最大子序和的方法示例
Jul 05 Python
详解Python 字符串相似性的几种度量方法
Aug 29 Python
使用Fabric自动化部署Django项目的实现
Sep 27 Python
浅析python内置模块collections
Nov 15 Python
python3连接mysql获取ansible动态inventory脚本
Jan 19 Python
python离线安装外部依赖包的实现
Feb 13 Python
通过实例了解Python异常处理机制底层实现
Jul 23 Python
Python爬取微信小程序通用方法代码实例详解
Sep 29 Python
使用python对excel表格处理的一些小功能
Jan 25 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 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)
2010/05/16 PHP
兼容性比较好的PHP生成缩略图的代码
2011/01/12 PHP
php调用nginx的mod_zip模块打包ZIP文件
2014/06/11 PHP
PHP查询快递信息的方法
2015/03/07 PHP
大家都应该掌握的PHP关联数组使用技巧
2015/12/25 PHP
PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
2017/09/17 PHP
基于laravel where的高级使用方法
2019/10/10 PHP
JavaScript 事件系统
2010/07/22 Javascript
自己实现string的substring方法 人民币小写转大写,数字反转,正则优化
2012/09/02 Javascript
一张表格告诉你windows.onload()与$(document).ready()的区别
2014/05/16 Javascript
jQuery中DOM树操作之复制元素的方法
2015/01/23 Javascript
js实现横向百叶窗效果网页切换动画效果的方法
2015/03/02 Javascript
一看就懂:jsonp详解
2015/06/01 Javascript
基于javascript制作微博发布栏效果
2016/04/04 Javascript
基于RequireJS和JQuery的模块化编程——常见问题全面解析
2016/04/14 Javascript
AngularJS基础 ng-paste 指令简单示例
2016/08/02 Javascript
基于jquery实现弹幕效果
2016/09/29 Javascript
纯javascript版日历控件
2016/11/24 Javascript
js+canvas实现动态吃豆人效果
2017/03/22 Javascript
React-Native做一个文本输入框组件的实现代码
2017/08/10 Javascript
vue数字类型过滤器的示例代码
2017/09/07 Javascript
JavaScript for循环 if判断语句(学习笔记)
2017/10/11 Javascript
Nodejs实现多文件夹文件同步
2018/10/17 NodeJs
微信小程序开发之转发分享功能
2019/10/22 Javascript
pytorch 共享参数的示例
2019/08/17 Python
Python爬虫教程知识点总结
2020/10/19 Python
linux mint中搜狗输入法导致pycharm卡死的问题
2020/10/28 Python
英国护发和美妆在线商店:Klip Shop
2019/03/24 全球购物
下面这个程序执行后会有什么错误或者效果
2014/11/03 面试题
汽车检测与维修应届毕业生求职信
2013/10/19 职场文书
超市营业员岗位职责
2013/12/20 职场文书
优秀驾驶员先进事迹材料
2014/05/04 职场文书
幼儿园推普周活动总结
2015/05/07 职场文书
交通安全月活动总结
2015/05/08 职场文书
CSS3 Tab动画实例之背景切换动态效果
2021/08/23 HTML / CSS
零基础学java之循环语句的使用
2022/04/10 Java/Android