介紹:
pyse基於selenium(webdriver)進行了簡單的二次封裝,比selenium所提供的方法操作更簡潔。
特點:
- 默認使用CSS定位,同時支持多種定位方法(id\name\class\link_text\xpath\css)。
- 本框架只是對selenium(webdriver)原生方法進行了簡單的封裝,精簡為大約30個方法,這些方法基本能夠勝任於我們的web自動化測試。
- 以測試類為單位,自動打開和關閉瀏覽器,減少瀏覽器的打開/關閉次數,節省時間。
- 自動生成/report/目錄,以及HTML測試報告生成。
- 自帶斷言方法,斷言title、URL 和 text。
安裝說明:
- Python3.5+ : https://www.python.org/
- Selenium3.6.0+ : https://pypi.python.org/pypi/selenium
進入pyse/目錄,執行:
> python setup.py install
例子:
請查看demo/test_case.py目錄
import pyse class BaiduTest(pyse.TestCase): def test_baidu(self): ''' baidu search key : pyse ''' self.open("https://www.baidu.com/") self.type("#kw", "pyse") self.click("#su") self.assertTitle("pyse_百度搜索") if __name__ == '__main__': runner = pyse.TestRunner() runner.run()
運行測試用例說明:
- 測試用例文件命名必須以“__test__”開頭。
- 默認情況下使用 __Chrome__ 瀏覽器運行測試用例。
- 元素定位方式默認使用 CSS 語法 `#kw`, 也可以顯示的使用 `css=>#kw`。
- pyse的TestCase類中默認封裝了`assertTitle`、`assertUrl` 和 `assertText`等斷言。
- `TestRunner()` 默認匹配當前目錄下"test*.py"的文件並執行。當然也可以指定測試目錄,例如:TestRunner("path/you/project/test_case/") # 注意用斜線"/"表示路徑。
- 執行`run()`方法運行測試用例並生成測試報告,在調試測試用例過程中可以使用 `debug()` 方法將不會生成HTML測試報告。
支持的瀏覽器及驅動:
指定運行的瀏覽器:
import pyse class YouTest(pyse.TestCase): @classmethod def setUpClass(cls): cls.driver = Pyse("chrome") def test_case(self): #……
支持的瀏覽器:
cls.driver = Pyse("firefox") #Firefox
cls.driver = Pyse("chrome") # Chrome
cls.driver = Pyse("ie") #IE
cls.driver = Pyse("opera") #Opera
cls.driver = Pyse("edge") #Edge
cls.driver = Pyse("chrome_headless") #Chrome headless模式
瀏覽器驅動下載地址:
geckodriver(Firefox):https://github.com/mozilla/geckodriver/releases
Chromedriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home
IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html
operadriver(Opera):https://github.com/operasoftware/operachromiumdriver/releases
MicrosoftWebDriver(Edge):https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver
==========================================================
#### 元素定位:
pyse支持多種定位方式,id、name、class、link text、xpath和css。把定位方法與定位內容一體,寫起更加簡潔。
<form id="form" class="fm" action="/s" name="f">
<span class="bg s_ipt_wr quickdelete-wrap">
<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd">
定位方式(推薦使用 CSS):
# 默認支持CSS語法
driver.type(".s_ipt","pyse") #css
driver.type("#su","pyse") #css
driver.type("id=>kw", "pyse") #id
driver.type("class=>s_ipt", "pyse") #class定位
driver.type("name=>wd", "pyse") #name
driver.type("xpath=>//*[@class='s_ipt']","pyse") #xpath
driver.type("xpath=>//*[@id='kw']","pyse") #xpath
driver.click_text("link_text=>新聞") #link text (點擊百度首頁上的"新聞"鏈接)
==========================================================
css選擇器參考手冊:
http://www.w3school.com.cn/cssref/css_selectors.asp
#### 測試報告

