Web測試框架SeleniumBase

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

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

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

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

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



首先,SeleniumBase支持 pip安裝:chrome

> pip install seleniumbase



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

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/的例子來學習。

相關文章
相關標籤/搜索