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与js)两种实现
Feb 21 Python
在Python中利用Pandas库处理大数据的简单介绍
Apr 07 Python
python装饰器初探(推荐)
Jul 21 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
Sep 06 Python
python的dataframe转换为多维矩阵的方法
Apr 11 Python
python按时间排序目录下的文件实现方法
Oct 17 Python
Python读取mat文件,并保存为pickle格式的方法
Oct 23 Python
解决vscode python print 输出窗口中文乱码的问题
Dec 03 Python
numpy中的meshgrid函数的使用
Jul 31 Python
Python读取实时数据流示例
Dec 02 Python
Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年
Apr 16 Python
浅谈keras中自定义二分类任务评价指标metrics的方法以及代码
Jun 11 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添加MySQL数据记录代码
2008/06/07 PHP
用php实现的下载css文件中的图片的代码
2010/02/08 PHP
js+php实现静态页面实时调用用户登陆状态的方法
2015/01/04 PHP
php抓取网站图片并保存的实现方法
2015/10/29 PHP
php实现跨域提交form表单的方法【2种方法】
2016/10/17 PHP
降低PHP Redis内存占用
2017/03/23 PHP
jquery写个checkbox——类似邮箱全选功能
2013/03/19 Javascript
简单的JavaScript互斥锁分享
2014/02/02 Javascript
jquery mobile的触控点击事件会多次触发问题的解决方法
2014/05/08 Javascript
jquery+ajax验证不通过也提交表单问题处理
2014/12/12 Javascript
jQuery简单实现隐藏以及显示特效
2015/02/26 Javascript
js时间控件只显示年月
2017/01/08 Javascript
详解axios在node.js中的post使用
2017/04/27 Javascript
一个可复用的vue分页组件
2017/05/15 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
Angular4 中内置指令的基本用法
2017/07/31 Javascript
AngularJS 仿微信图片手势缩放的实例
2017/09/28 Javascript
JS实现带导航城市列表以及输入搜索功能
2018/01/04 Javascript
搭建Python的Django框架环境并建立和运行第一个App的教程
2016/07/02 Python
Python机器学习之SVM支持向量机
2017/12/27 Python
Python lambda函数基本用法实例分析
2018/03/16 Python
浅谈python3.6的tkinter运行问题
2019/02/22 Python
python3实现钉钉消息推送的方法示例
2019/03/14 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
Python爬虫教程之利用正则表达式匹配网页内容
2020/12/08 Python
英国Radley包德国官网:Radley London德国
2019/11/18 全球购物
内业资料员岗位职责
2014/01/04 职场文书
2014爱耳日宣传教育活动总结
2014/03/09 职场文书
2014银行授权委托书样本
2014/10/04 职场文书
公司委托书格式范文
2014/10/09 职场文书
初中运动会闭幕词范本3篇
2019/12/09 职场文书
MySQL之高可用集群部署及故障切换实现
2021/04/22 MySQL
解决Golang time.Parse和time.Format的时区问题
2021/04/29 Golang
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby
Python探索生命起源 matplotlib细胞自动机动画演示
2022/04/21 Python
PostgreSQL怎么创建分区表详解
2022/06/25 PostgreSQL