1、配置chromedriver路径
chromedriver.exe下载地址:http://chromedriver.storage.googleapis.com/index.html
将下载好的chromedriver.exe放在python的安装目录,嗯我还有ie和火狐的driver也在这,看你喜欢用哪个自己去下。
2、下载安装HTMLTestRunner
上一个文章有汉化版下载地址,把HTMLTestRunner.py放到python的lib目录,
然后打开python命令行,执行import HTMLTestRunner命令,没什么提示就没问题
去网上扒的一份自动化测试实例源码:
from selenium import webdriver
import HTMLTestRunner,unittest,time,os
class BaiduTest(unittest.TestCase):
def setUp(self): #测试准备工作
self.driver = webdriver.Chrome() #加载驱动
self.driver.implicitly_wait(30) #隐式等待
self.base_url = "http://www.baidu.com" #url
def test_baidu(self): #测试用例
driver = self.driver
print("========【case_0001】 百度搜索=============")
driver.get(self.base_url + "/") #get发送url
driver.find_element_by_id("kw").clear() #清空
driver.find_element_by_id("kw").send_keys("测试") #搜索测试
driver.find_element_by_id("su").click() #点击
time.sleep(5)#休息5s
self.assertEqual("shuzf_百度搜索", driver.title) # 断言判断是否相等
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
if not os.path.exists('result/image/'): os.makedirs('result/image/') #判断当前路径是否存在,没有则创建文件夹
pic_path = 'result/image/' + now + '.png'
driver.save_screenshot(pic_path) #生成图片
def tearDown(self): #释放资源
self.driver.quit() #退出
if __name__ == "__main__":
testunit = unittest.TestSuite() #构造测试套件
testunit.addTest(BaiduTest("test_baidu")) #添加测试用例
#HtmlFile = "c:/"+now+"run.html"
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
if not os.path.exists('result/'): os.makedirs('result/') #判断当前路径是否存在,没有则创建文件夹
HtmlFile = "result/" + now + ".html"
fp = open(HtmlFile, "wb") #生成报告
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="百度测试报告", description="用例测试情况")
runner.run(testunit) #执行套件
fp.close() #关闭
执行结果: