大家對selenium這個自動化測試工具一定不陌生,關于selenium就不多說啦,最近項目要使用robotframework做自動化回歸測試,小編每日和文檔上的小螞蟻斗智斗勇,下面給大家分享一下戰斗的成果,介紹一下seleniumlibrary庫的關鍵字,體會一下selenium和seleniumlibrary使用的差異:
Close Browser:close_browser(self):調用當前瀏覽器的quit命令,將該瀏覽器實例放入cache的closed集合中。
Close All Browser:close_all_browsers(self):調用當前所有打開的瀏覽器的quit命令,清空整個cache。
Switch Browser:switch_browser(self, index_or_alias):切換當前瀏覽器,可以用Open Browser返回的值(index,從1開始)或者Open Browser時指定的{alias}參數來指明切換到哪個瀏覽器。
Close Window:close_window(self):關掉當前彈出窗口,其實彈出窗口相當于一個緩存在cache中的browser,關閉時調用該browser的close方法。如果沒有當前broswer,會Raise?RuntimeError('No browser is open')異常。
Get Window Identifiers:get_window_identifiers(self):返回當前browser關聯的所有dom對象window的id屬性數組,同時會調用_LoggingKeywords的_log_list(self, items, what='item')把ids數組作為info寫入robot的日志。如果沒有當前broswer,會Raise?RuntimeError('No browser is open')異常。
Get Window Names:get_window_names(self):返回當前browser關聯的所有dom對象window的name屬性數組,同樣會調用_LoggingKeywords的_log_list(self, items, what='item')把names數組作為info寫入robot的日志。如果沒有當前broswer,會Raise?RuntimeError('No browser is open')異常。window對象的name屬性一般在js函數-window.open中指定,如果沒有指定(undefined)并且返回的name數組長度為1,說明當前browser就打開了一個窗口,undefined會被替換成selenium_main_app_window
Get Window titles:get_window_identifiers(self):返回當前browser關聯的所有dom對象document的title屬性數組,同時會調用_LoggingKeywords的_log_list(self, items, what='item')把titles數組作為info寫入robot的日志。如果沒有當前broswer,會Raise?RuntimeError('No browser is open')異常。
Maximize Browser Window:maximize_browser_window(self):最大化當前瀏覽器窗口。
Select Frame:select_frame(self, locator):用傳入的locator作為參數調用_ElementKeywords的_element_find(self, locator, first_only, required, tag=None)方法得到符合locator條件的第一個dom元素,然后調用當前瀏覽器的SWITCH_TO_FRAME命令,命令參數名為id,參數值即為locator得到的dom元素。frame的locator支持id和name兩種方式。
Select Window:select_window(self, locator=None):用傳入的locator去匹配當前瀏覽器的所有dom對象window里面的屬性,如果匹配到了,就調用當前瀏覽器的SWITCH_TO_WINDOW命令,命令參數名為name,參數值即為locator得到的window handler對象。這里的locator不是selenium的locator,而是rf自己是實現的WindowManager,支持通過title,name,url的方式查詢window。
Unselect Frame:unselect_frame(self):調用當前瀏覽器的SWITCH_TO_FRAME命令,命令參數名為id,參數值為None。
Click button[locator]:點擊按鈕,locator是按鈕位置
Click element[]:點擊元素,locator是元素位置
Click element at coordinates[]:點擊元素,該元素以x/y坐標為準
Click image[]:點擊圖片
Click link[]:點擊鏈接
Delete all cookies[]:刪除所有cookie
Delete cookie[name]:刪除名字為name的cookie,如果沒有匹配到,不會執行任何操作
Double click element[]:雙擊元素
Element should be disabled[locator]:驗證元素是否可用
Element should be visible[]:驗證元素是否可見
好啦,直接上代碼:
RF寫法
*** Settings ***
LibrarySeleniumLibrary
*** Test Cases ***
測試百度
Open Browser? ? ??http://www.baidu.com ???chrome
input textid=kw ???robot framework
click button? ?id=su
close Browser
Python寫RF測試代碼
# 測試用例:啟動瀏覽器
def?open_browsers(self):
test_01=self.suite.tests.create("啟動瀏覽器")
test_01.keywords.create("Open Browser",args=["https://www.baidu.com"
,"chrome"])
test_01.keywords.create("Title Should Be",args=["百度一下,你就知道"])
以上是關鍵字的使用代碼示例,不同的寫法
根據文檔翻譯的,暫時先翻譯到這,后續會給大家繼續更新,感興趣的小伙伴關注一下,謝謝大家的支持,你們的支持就是小編的動力
sunny