用Python寫Robot Framework測試

來源:互聯網
上載者:User

標籤:pen   AC   ring   resource   var   writer   百度搜   print   button   

Robot Framework 架構是基於 Python 語言開發的,所以,它本質上是 Python 的一個庫。

百度搜尋執行個體

建立 py_robot.py 檔案,代碼如下:

from robot.api import TestSuitefrom robot.api import ResultWriterfrom robot.model import Keyword# 百度搜尋測試class BaiduSearchTest: def __init__(self, name, librarys=["SeleniumLibrary"]): # 建立測試套件 self.suite = TestSuite(name) # 匯入SeleniumLibrary for lib in librarys: self.suite.resource.imports.library(lib) # 定義變數 def create_variables(self): variables = { "${baidu}": "https://www.baidu.com", "${browser}": "Chrome", "${search_input}": "id=kw", "${search_btn}": "id=su" } for k, v in variables.items(): self.suite.resource.variables.create(k, v) # 測試案例:啟動瀏覽器 def open_browsers(self): test_01 = self.suite.tests.create("啟動瀏覽器") test_01.keywords.create("Open Browser", args=["${baidu}", "${browser}"]) test_01.keywords.create("Title Should Be", args=["百度一下,你就知道"]) # 測試案例:百度搜尋測試 def search_word(self): test_02 = self.suite.tests.create("百度搜尋測試") test_02.keywords.create("Input Text", args=["${search_input}", "測試教程網"]) test_02.keywords.create("Click Button", args=["${search_btn}"]) test_02.keywords.create("Sleep", args=["5s"]) # 測試案例:斷言驗證搜尋結果標題 def assert_title(self): test_03 = self.suite.tests.create("斷言驗證搜尋結果標題") test_03.keywords.create("Title Should Be", args=["測試教程網_百度搜尋"]) # 測試案例:關閉測試案例 def close_browsers(self): test_04 = self.suite.tests.create("關閉瀏覽器") test_04.keywords.create("Close All Browsers") # 運行 def run(self): self.create_variables() self.open_browsers() self.search_word() self.assert_title() self.close_browsers() # 運行套件 result = self.suite.run(critical="百度搜尋", output="output.xml") # 組建記錄檔、報告檔案 ResultWriter(result).write_results( report="report.html", log="log.html")if __name__ == "__main__": print("用Python寫Robot Framework測試") suite = BaiduSearchTest("百度搜尋測試套件") suite.run()

這段代碼的運行通過 python 命令來執行。

> python py_robot.py用Python寫Robot Framework測試==============================================================================百度搜尋測試套件==============================================================================啟動瀏覽器DevTools listening on ws://127.0.0.1:12950/devtools/browser/bcbf14bb-ebc4-425c-882f-44531afd9689啟動瀏覽器                                                            | PASS |------------------------------------------------------------------------------百度搜尋測試                                                          | PASS |------------------------------------------------------------------------------斷言驗證搜尋結果標題                                                  | PASS |------------------------------------------------------------------------------關閉瀏覽器                                                            | PASS |------------------------------------------------------------------------------百度搜尋測試套件                                                      | PASS |0 critical tests, 0 passed, 0 failed4 tests total, 4 passed, 0 failed==============================================================================Output:  D:\rf_test\robotSe\output.xml

Robot Framework 用的好,Python 少不了!所以,我的建議是要想用好 Robot Framework 必須要學習和掌握 Python 語言。

用Python寫Robot Framework測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.