(一)自定義和瀏覽器相關的關鍵字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 關鍵字的寫法。