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 相关文章推荐
使用Django的模版来配合字符串翻译工作
Jul 27 Python
Python基于identicon库创建类似Github上用的头像功能
Sep 25 Python
详解用Python处理HTML转义字符的5种方式
Dec 27 Python
浅析python协程相关概念
Jan 20 Python
pygame游戏之旅 按钮上添加文字的方法
Nov 21 Python
python贪吃蛇游戏代码
Apr 18 Python
django之对FileField字段的upload_to的设定方法
Jul 28 Python
在Python中使用MySQL--PyMySQL的基本使用方法
Nov 19 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
Dec 10 Python
Python常用模块sys,os,time,random功能与用法实例分析
Jan 07 Python
Python爬虫实现百度翻译功能过程详解
May 29 Python
Python 实现绘制子图及子图刻度的变换等问题
May 31 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
php5.2.0内存管理改进
2007/01/22 PHP
php中定义网站根目录的常用方法
2010/08/08 PHP
浅析php中抽象类和接口的概念以及区别
2013/06/27 PHP
PHP连接局域网MYSQL数据库的简单实例
2013/08/26 PHP
完善CodeIgniter在IDE中代码提示功能的方法
2014/07/19 PHP
php 如何获取文件的后缀名
2016/06/05 PHP
CI框架AR数据库操作常用函数总结
2016/11/21 PHP
JQuery中使用Ajax赋值给全局变量异常的解决方法
2014/01/10 Javascript
JS替换字符串中字符即替换全部而不是第一个
2014/06/04 Javascript
基于jQuery实现的图片切换焦点图整理
2014/12/07 Javascript
jquery中filter方法用法实例分析
2015/02/06 Javascript
JavaScript面向对象之私有静态变量实例分析
2016/01/14 Javascript
基于Bootstrap仿淘宝分页控件实现代码
2016/11/07 Javascript
利用n 升级工具升级Node.js版本及在mac环境下的坑
2017/02/15 Javascript
使用C语言来扩展Python程序和Zope服务器的教程
2015/04/14 Python
详解Python中 __get__和__getattr__和__getattribute__的区别
2016/06/16 Python
pycharm下打开、执行并调试scrapy爬虫程序的方法
2017/11/29 Python
python抽取指定url页面的title方法
2018/05/11 Python
django静态文件加载的方法
2018/05/20 Python
对python程序内存泄漏调试的记录
2018/06/11 Python
Python基于Logistic回归建模计算某银行在降低贷款拖欠率的数据示例
2019/01/23 Python
Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析
2019/08/15 Python
在python tkinter界面中添加按钮的实例
2020/03/04 Python
如何安装并在pycharm使用selenium的方法
2020/04/30 Python
Html5定位终极解决方案
2020/02/05 HTML / CSS
施华洛世奇天猫官方旗舰店:SWAROVSKI
2017/04/17 全球购物
德国亚洲食品网上商店:asiafoodland.de
2019/12/28 全球购物
.NET是怎么支持多种语言的
2015/02/24 面试题
写演讲稿所需要注意的4个条件
2014/01/09 职场文书
2014高中生入党思想汇报范文
2014/09/13 职场文书
初中政治教学反思
2016/02/23 职场文书
MySQL infobright的安装步骤
2021/04/07 MySQL
C站最全Python标准库总结,你想要的都在这里
2021/07/03 Python
利用JavaScript写一个简单计算器
2021/11/27 Javascript
Python实现文字pdf转换图片pdf效果
2022/04/03 Python
Redis唯一ID生成器的实现
2022/07/07 Redis