(一)自定义和浏览器相关的关键字python
如下代码GitHub 版本库地址: https://github.com/blairwind/blog_rfgit
SeleniumLibrary的扩展文档中提供了3种增长SeleniumLibrary功能的方式。github
(1)Plugin APIweb
(2)EventFiringWebDriverchrome
(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 CustomizeSeleniumLibrary.keywords import ( KeyboardKeywords) class CustomizeSeleniumLibrary(SeleniumLibrary): def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): SeleniumLibrary.__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 的代码ui
from .keyboard import KeyboardKeywords
BlogSeleniumLibrary.keywords.keyboard.py 的代码
1 from SeleniumLibrary.base import keyword, LibraryComponent 2 from selenium.webdriver.chrome.options import Options 3 from selenium import webdriver 4 from SeleniumLibrary.locators import WindowManager 5 6 class KeyboardKeywords(LibraryComponent): 7 8 def __init__(self, ctx): 9 LibraryComponent.__init__(self, ctx) 10 self._window_manager = WindowManager(ctx) 11 12 @keyword() 13 def get_chrome_options(self, downloads_path): 14 ''' 15 自定义chrome启动参数 16 :param downloads_path: 设置默认的文件下载路径 17 :return: 18 ''' 19 chrome_options = Options() 20 prefs = { 21 "download.default_directory": str(downloads_path), 22 } 23 chrome_options.add_experimental_option('prefs', prefs) # 设置默认的文件下载路径 24 chrome_options.add_argument('disable-infobars') # chrome76如下禁用chrome受自动软件控制 25 # 下面2行chrome76及以上禁用chrome受自动软件控制 26 chrome_options.add_experimental_option("useAutomationExtension", False) 27 chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) 28 return chrome_options 29 30 @keyword() 31 def open_browser_new(self, alias=None,**kwargs): 32 ''' 33 :return: 34 ''' 35 desired_caps = { 36 "platform": kwargs["platform"], #操做系统 37 # "platform":"LINUX", 38 "browserName": kwargs["browserName"], #浏览器 39 "version":kwargs["version"] #浏览器版本 40 } 41 42 driver = webdriver.Remote(command_executor=kwargs["remote_url"], 43 desired_capabilities=desired_caps, 44 options=kwargs["chrome_options"]) 45 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,确定会考虑并发执行用例,以及合并测试报告的问题,这里暂不考虑这个。
1 @keyword() 2 def open_browser_new(self, alias=None,**kwargs): 3 ''' 4 :return: 5 ''' 6 desired_caps = { 7 "platform": kwargs["platform"], #操做系统 8 # "platform":"LINUX", 9 "browserName": kwargs["browserName"], #浏览器 10 "version":kwargs["version"] #浏览器版本 11 } 12 13 driver = webdriver.Remote(command_executor=kwargs["remote_url"], 14 desired_capabilities=desired_caps, 15 options=kwargs["chrome_options"]) 16 return self.ctx.register_driver(driver,alias)
在RF中调用这个关键字去启动浏览器就好了。固然前提是你要有一个配好的selenium grid环境,remote_url填本身selenium grid的地址。
1 *** Settings *** 2 Library BlogSeleniumLibrary 3 Suite Teardown CLOSE BROWSER 4 5 *** Variables *** 6 ${platform} WINDOWS 7 ${browser} chrome 8 ${version} 79 9 ${remote_url} http://192.168.63.1:4444/wd/hub 10 ${login_url} https://account.cnblogs.com/signin 11 12 13 *** Test Cases *** 14 登陆-XXXXXX 15 登陆-打开浏览器并进入登陆页面 16 17 18 *** Keywords *** 19 登陆-打开浏览器并进入登陆页面 20 ${options}= GET CHROME OPTIONS D:/projectname/testdata/downloads #这里是写死的路径,实际项目中应该动态去获取工程路径/testdata/downloads 21 OPEN BROWSER NEW platform=${platform} browserName=${browser} version=${version} 22 ... chrome_options=${options} remote_url=${remote_url} 23 GO TO ${login_url} 24 SET SELENIUM IMPLICIT WAIT 10 25 MAXIMIZE BROWSER WINDOW
(三)自定义和浏览器无关的关键字(例如:和数据库相关的关键字)
若是有一些关键字用不到selenium 的webdriver,能够考虑独立出来。例如数据库相关的关键字,实现方式以及在RF中的导入方式,能够参考上一篇的mykeyword 关键字的写法。