pycharm的中自定义库和关键字_robot framework笔记(三):扩展SeleniumLibrary库 (自定义关键字)...

这篇博客介绍了如何在PyCharm中通过继承SeleniumLibrary来扩展自定义功能,特别是针对Chrome浏览器的启动参数进行定制。作者详细讲解了如何创建新的关键字`get_chrome_options`用于设置下载路径和禁用自动化控制提示,并展示了如何在Robot Framework中使用这些自定义关键字。此外,还提到了使用selenium grid的可能性。
摘要由CSDN通过智能技术生成

(一)自定义和浏览器相关的关键字

PYTHON 版本:3.6,不同的版本可能有区别,自己注意下。

SeleniumLibrary的扩展文档中提供了3种增加SeleniumLibrary功能的方式。

(1)Plugin API

(2)EventFiringWebDriver

(3)Extending SeleniumLibrary(实际就是继承SeleniumLibrary库)

这里采用继承SeleniumLibrary库的方式。

目录结构如下:这里我们将上一篇中说到的关键字加进来

BlogSeleniumLibrary.__init__.py  的代码

# #-*-coding:utf-8-*-

#

from robot.libraries import BuiltIn

from SeleniumLibrary.base import DynamicCore

from SeleniumLibrary.keywords import (AlertKeywords,

BrowserManagementKeywords,

CookieKeywords,

ElementKeywords,

FormElementKeywords,

FrameKeywords,

JavaScriptKeywords,

RunOnFailureKeywords,

ScreenshotKeywords,

SelectElementKeywords,

TableElementKeywords,

WaitingKeywords,

WebDriverCache,

WindowKeywords)

from SeleniumLibrary.locators import ElementFinder

from SeleniumLibrary.utils import Deprecated, LibraryListener, timestr_to_secs

from SeleniumLibrary import SeleniumLibrary

from BlogSeleniumLibrary.keywords import (

KeyboardKeywords)

class BlogSeleniumLibrary(SeleniumLibrary):

def __init__(self, timeout=5.0, implicit_wait=0.0,

run_on_failure='Capture Page Screenshot',

screenshot_root_directory=None):

self.timeout = timestr_to_secs(timeout)

self.implicit_wait = timestr_to_secs(implicit_wait)

self.speed = 0.0

self.run_on_failure_keyword \

= RunOnFailureKeywords.resolve_keyword(run_on_failure)

self._running_on_failure_keyword = False

self.screenshot_root_directory = screenshot_root_directory

libraries = [

AlertKeywords(self),

BrowserManagementKeywords(self),

CookieKeywords(self),

ElementKeywords(self),

FormElementKeywords(self),

FrameKeywords(self),

JavaScriptKeywords(self),

RunOnFailureKeywords(self),

ScreenshotKeywords(self),

SelectElementKeywords(self),

TableElementKeywords(self),

WaitingKeywords(self),

WindowKeywords(self),

KeyboardKeywords(self)

]

self._drivers = WebDriverCache()

DynamicCore.__init__(self, libraries)

self.ROBOT_LIBRARY_LISTENER = LibraryListener()

self._element_finder = ElementFinder(self)

_speed_in_secs = Deprecated('_speed_in_secs', 'speed')

_timeout_in_secs = Deprecated('_timeout_in_secs', 'timeout')

_implicit_wait_in_secs = Deprecated('_implicit_wait_in_secs',

'implicit_wait')

_run_on_failure_keyword = Deprecated('_run_on_failure_keyword',

'run_on_failure_keyword')

BlogSeleniumLibrary.keywords.__init__.py  的代码

from .keyboard import KeyboardKeywords

BlogSeleniumLibrary.keywords.keyboard.py  的代码

from SeleniumLibrary.base import keyword, LibraryComponent

from selenium.webdriver.chrome.options import Options

from selenium import webdriver

from SeleniumLibrary.locators import WindowManager

class KeyboardKeywords(LibraryComponent):

def __init__(self, ctx):

LibraryComponent.__init__(self, ctx)

self._window_manager = WindowManager(ctx)

@keyword()

def get_chrome_options(self, downloads_path):

'''

自定义chrome启动参数

:param downloads_path: 设置默认的文件下载路径

:return:

'''

chrome_options = Options()

prefs = {

"download.default_directory": str(downloads_path),

}

chrome_options.add_experimental_option('prefs', prefs) # 设置默认的文件下载路径

chrome_options.add_argument('disable-infobars') # chrome76以下禁用chrome受自动软件控制

# 下面2行chrome76及以上禁用chrome受自动软件控制

chrome_options.add_experimental_option("useAutomationExtension", False)

chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

return chrome_options

@keyword()

def open_browser_new(self, alias=None,**kwargs):

'''

:return:

'''

desired_caps = {

"platform": kwargs["platform"], #操作系统

# "platform":"LINUX",

"browserName": kwargs["browserName"], #浏览器

"version":kwargs["version"] #浏览器版本

}

driver = webdriver.Remote(command_executor=kwargs["remote_url"],

desired_capabilities=desired_caps,

options=kwargs["chrome_options"])

return self.ctx.register_driver(driver,alias)

最后,在RF中导入继承SeleniumLibrary后新建的库就行了,如下:

注意在RF中python 包名和类名一样的的话,导入库的时候就只需要填包名就行了,RF可以直接识别到。不一样的话就还需要加上.class名称,下面这个是不使用selenium grid的版本

*** Settings ***

Library BlogSeleniumLibrary #注意这一行不一样

Suite Teardown CLOSE BROWSER

*** Variables ***

${browser} Chrome

${login_url} https://account.cnblogs.com/signin

*** Test Cases ***

登录-XXXXXX

登录-打开浏览器并进入登录页面

*** Keywords ***

登录-打开浏览器并进入登录页面

${options}= GET CHROME OPTIONS D:/projectname/testdata/downloads

CREATE WEBDRIVER ${browser} chrome_options=${options}

GO TO ${login_url}

SET SELENIUM IMPLICIT WAIT 10

MAXIMIZE BROWSER WINDOW

(二)如果要使用selenium grid呢

上篇中说到在RF中使用selenium grid ,在这里说明下。(为什么不使用RF自带的open browser,原因是个人觉得这种方式更方便添加不同的参数。)可以看到这里新加了一个关键字

当然,既然用了selenium grid,肯定会考虑并发执行用例,以及合并测试报告的问题,这里暂不考虑这个。

@keyword()

def open_browser_new(self, alias=None,**kwargs):

'''

:return:

'''

desired_caps = {

"platform": kwargs["platform"], #操作系统

# "platform":"LINUX",

"browserName": kwargs["browserName"], #浏览器

"version":kwargs["version"] #浏览器版本

}

driver = webdriver.Remote(command_executor=kwargs["remote_url"],

desired_capabilities=desired_caps,

options=kwargs["chrome_options"])

return self.ctx.register_driver(driver,alias)

在RF中调用这个关键字去启动浏览器就行了。当然前提是你要有一个配好的selenium grid环境,remote_url填自己selenium grid的地址。

*** Settings ***

Library BlogSeleniumLibrary

Suite Teardown CLOSE BROWSER

*** Variables ***

${platform} WINDOWS

${browser} chrome

${version} 79

${remote_url} http://192.168.63.1:4444/wd/hub

${login_url} https://account.cnblogs.com/signin

*** Test Cases ***

登录-XXXXXX

登录-打开浏览器并进入登录页面

*** Keywords ***

登录-打开浏览器并进入登录页面

${options}= GET CHROME OPTIONS D:/projectname/testdata/downloads #这里是写死的路径,实际项目中应该动态去获取工程路径/testdata/downloads

OPEN BROWSER NEW platform=${platform} browserName=${browser} version=${version}

... chrome_options=${options} remote_url=${remote_url}

GO TO ${login_url}

SET SELENIUM IMPLICIT WAIT 10

MAXIMIZE BROWSER WINDOW

(三)自定义和浏览器无关的关键字(例如:和数据库相关的关键字)

如果有一些关键字用不到selenium 的webdriver,可以考虑独立出来。例如数据库相关的关键字,实现方式以及在RF中的导入方式,可以参考上一篇的mykeyword 关键字的写法。

robot framework笔记(一):环境配置(基于python3)+在pycharm中编写及运行robot脚本

(一)使用pip安装robotframework框架和seleniumlibrary库 pip install --upgrade robotframework pip install --upgra ...

Robot Framework(三)创建测试用例

2.2.1测试用例语法 基本语法 测试用例由关键字在测试用例表中构建.关键字可以从测试库或资源文件导入,也可以在测试用例文件本身的关键字表中创建. 测试用例表中的第一列包含测试用例名称.测试用例从包含 ...

robot framework笔记(二):在RF中自定义chrome启动参数

(一)在RF中自定义chrome启动参数 这里主要是实现下面2个功能 1.禁用chrome正受自动测试软件控制的提示 2.设置默认的下载路径(一些导出.下载类的功能,将文件下载到指定路径下) 自定义一 ...

robot framework用python扩展编写自定义library

我的utils.py文件 #!/usr/bin/env python #-*- coding:utf8 -*- __version__ = '0.1' import sys reload(sys) s ...

robot framework 笔记(三),RF安装

背景: 本来robot framework的安装应该放在一开始写的,因写博客的时候已经装过了,恰巧重装系统又重装了一遍RF RF推荐使用python2, 使用3的话会遇到一些页面非友好的问题 需要的安 ...

Robot Framework - 1 - 测试用例与测试库

01- 关于测试库(Test libraries) Test libraries provide the actual testing capabilities to Robot Framework ...

Robot Framework自动化测试三(selenium API)

Robot  Framework  Selenium  API 说明: 此文档只是将最常用的UI 操作列出.更多方法请查找selenium2Library 关键字库. 一.浏览器驱动 通过不同的浏览器 ...

robot framework 笔记(一)

背景: 平时使用rf时会用到一些方法,长时间不用就会忘记,本文用来记录当做自己的小笔记 内容持续更新中········ 一.robot framework 大小写转换 1.转换小写: ${low} E ...

robot framework 使用三:他们主动浏览器的兼容性

robot framework 浏览器兼容性测试 上图中黄色圈的地方默认什么都不写,是firefox浏览器.写上ie就是ie浏览器了 firefox最新版本号即可,ie须要设置: 1. IE选项设置的 ...

随机推荐

深入研究C语言 第四篇

这里更多探究的是指针的机制. 用debug对下面程序进行分析,记录每一条C语句运行后,相关内存单元的值. 程序a.c 注意理解指针机制 我们编写如下代码: 编译加载进debug查看: 我们先看其反汇编 ...

Codeforces Round #354 (Div. 2)-B

B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...

React组件生命周期过程说明【转】

实例化 首次实例化 getDefaultProps getInitialState componentWillMount render componentDidMount 实例化完成后的更新 getI ...

thinkphp ajax检测是否数据可用

模板文件:如 index.html 模板文件如:(index.html)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值