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实现给字典添加条目的方法
Sep 25 Python
实例讲解Python的函数闭包使用中应注意的问题
Jun 20 Python
Python将json文件写入ES数据库的方法
Apr 10 Python
Python分支语句与循环语句应用实例分析
May 07 Python
python实现XML解析的方法解析
Nov 16 Python
Python爬取腾讯视频评论的思路详解
Dec 19 Python
解决Django Haystack全文检索为空的问题
May 19 Python
k-means 聚类算法与Python实现代码
Jun 01 Python
在pycharm中使用matplotlib.pyplot 绘图时报错的解决
Jun 01 Python
Python变量及数据类型用法原理汇总
Aug 06 Python
PYTHON使用Matplotlib去实现各种条形图的绘制
Mar 22 Python
python实现商品进销存管理系统
May 30 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项目的方法
2006/10/09 PHP
支持中文的php加密解密类代码
2011/11/27 PHP
laravel 实现根据字段不同值做不同查询
2019/10/23 PHP
Thinkphp极验滑动验证码实现步骤解析
2020/11/24 PHP
JS正则表达式获取分组内容的方法详解
2013/11/15 Javascript
js 获取时间间隔实现代码
2014/05/12 Javascript
jQuery实现伸展与合拢panel的方法
2015/04/30 Javascript
JavaScript优化专题之Loading and Execution加载和运行
2016/01/20 Javascript
浅谈JavaScript前端开发的MVC结构与MVVM结构
2016/06/03 Javascript
分享一个原生的JavaScript拖动方法
2016/09/25 Javascript
bootstrap table复杂操作代码
2016/11/01 Javascript
[原创]javascript typeof id==='string'?document.getElementById(id):id解释
2016/11/02 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
详解javascript中对数据格式化的思考
2017/01/23 Javascript
jquery实现超简单的瀑布流布局【推荐】
2017/03/08 Javascript
jQuery dateRangePicker插件使用方法详解
2017/07/28 jQuery
Angular中支持SCSS的方法
2017/11/18 Javascript
浅谈js闭包理解
2019/03/28 Javascript
jquery实现进度条状态展示
2020/03/26 jQuery
javascript读取本地文件和目录方法详解
2020/08/06 Javascript
jQuery实现简单弹幕制作
2020/12/10 jQuery
[02:22]《新闻直播间》2017年08月14日
2017/08/15 DOTA
[53:50]CHAOS vs Mineski 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
Python中使用md5sum检查目录中相同文件代码分享
2015/02/02 Python
python使用numpy读取、保存txt数据的实例
2018/10/14 Python
python pytest进阶之fixture详解
2019/06/27 Python
python django model联合主键的例子
2019/08/06 Python
python 模拟登录B站的示例代码
2020/12/15 Python
THE OUTNET美国官网:国际设计师品牌折扣网站
2017/03/07 全球购物
教师自我评价范例
2013/09/24 职场文书
平面设计师的工作职责
2013/11/21 职场文书
十八大感想感言
2014/02/10 职场文书
学生干部学习的自我评价
2014/02/18 职场文书
《我为你骄傲》教学反思
2014/02/20 职场文书
班班通校本培训方案
2014/03/12 职场文书
诺贝尔奖获得者名言100句:句句启人心智,值永久收藏
2019/08/09 职场文书