前几天逛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/的例子来学习。