初识Web测试框架SeleniumBase
基于Selenium和unittest单元测试框架的一个测试框架SeleniumBase。
1、SeleniumBase支持 pip安装
> pip install seleniumbase
它依赖的库比较多,包括pytest、nose这些第三方单元测试框架,是为更方便的运行测试用例,因为这两个测试框架是支持unittest测试用例的执行的
2、SeleniumBase还生成了“seleniumbase”命令,主要是为了方便我们安装浏览器驱动。
> seleniumbase install chromedriver
> seleniumbase install geckodriver
> seleniumbase install edgedriver
> seleniumbase install iedriver
> seleniumbase install operadriver
3、在项目的examples/目录下面提供了丰富的例子。
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")
4、接下来是脚本的执行,你可以使用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
当用例运行失败时自动截图!