Web測試框架SeleniumBase


前幾天逛GitHub發現一個基於Selenium和unittest單元測試框架的一個測試框架SeleniumBase。

Github地址:https://github.com/seleniumbase/SeleniumBase

大概看了一個它的API,它的設計思想與我的pyse很像。

GitHub地址:https://github.com/defnngj/pyse

但是,提供了更加豐富的API,和一些強大的功能。


首先,SeleniumBase支持 pip安裝:
> pip install seleniumbase

它依賴的庫比較多,包括pytest、nose這些第三方單元測試框架,是為更方便的運行測試用例,因為這兩個測試框架是支持unittest測試用例的執行的。

SeleniumBase還生成了“seleniumbase”命令,主要是為了方便我們安裝瀏覽器驅動。

你可以通過下面的命令安裝不同的瀏覽器驅動。

seleniumbase install chromedriver

seleniumbase install geckodriver

seleniumbase install edgedriver

seleniumbase install iedriver

seleniumbase install operadriver


在項目的examples/目錄下面提供了豐富的例子。其中my_first_test.py如下:
from seleniumbase import BaseCase


class MyTestClass(BaseCase):

    def test_basic(self):
        self.open("https://xkcd.com/353/")  
        self.assert_element('img[alt="Python"]')  
        self.click('a[rel="license"]')  
        self.assert_text("free to copy", "div center")  
        self.open("https://xkcd.com/1481/")
        title = self.get_attribute("#comic img", "title")  
        self.assert_true("86,400 seconds per day" in title)
        self.click("link=Blag")  
        self.assert_text("The blag of the webcomic", "h2")
        self.update_text("input#s", "Robots!\n")  
        self.assert_text("Hooray robots!", "#content")
        self.open("https://xkcd.com/1319/")
        self.assert_exact_text("Automation", "#ctitle")

如果你很熟悉Selenium的話,我想這些API對你來說並沒什么難度。腳本中的元素定位默認使用的CSS。


接下來是腳本的執行,你可以使用pytest或nose,因為SeleniumBase已經幫你裝好了:
> pytest my_first_test.py --browser=chrome

> nosetests my_first_test.py --browser=firefox 

它還提供的有 ```—demo_mode``` 模式,使腳本執行的過程變得很慢,而且還會讓操作的元素高亮顯示,方便你查看和定位問題。
pytest my_first_test.py --demo_mode


在調試Selenium腳本的時候,我們希望錯誤時可以暫停腳本,那么可以加 ```--pdb -s``` 參數。
pytest my_first_test.py --pdb -s

當腳本報錯時是這樣的:

上面的代碼將使瀏覽器窗口保持打開狀態,以防出現故障。你可以繼續輸入命令:
“c”:繼續
“s”:步驟
“n”: 下一步


你還可以利用pytest 的 pytest-thml插件生成測試報告。
pytest test_suite.py --html=report.html

當用例運行失敗時自動截圖!

其他就沒什么亮點了,不過提供的API 非常豐富,而且作者非常積極的在維護項目。你可以在項目說明中查看,或者通過提供的examples/的例子來學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM