1.用到的环境/工具/技术:
pycharm、python、unittest、HTMLTestRunner
2.结构示例:
3.代码示例
3.1 调度执行文件
import os
import time
import unittest
import HTMLTestRunner
'''
执行当前目录下所有Test开头的py文件
'''
# report_path = os.getcwd() # 设置保存报告的路径,这儿设置的是与执行文件在同一个目录下
report_path = os.path.join(os.getcwd(), "report") #设置报告路径,指定当前目录下的子目录名称名称
now = time.strftime("%Y-%m-%d-%H-%M", time.localtime(time.time())) # 获取当前时间
title = u"自动化测试报告__" # 标题
report_abspath = os.path.join(report_path, title + now + ".html") # 设置报告存放和命名
# 导入用例
def all_case():
#case_path = os.getcwd() # 用例路径,这儿的用例和执行文件在同一目录下
case_path = os.path.join(os.getcwd(), "testcase") #用例路径,指定当前目录下的子目录名称名称
testunit = unittest.defaultTestLoader.discover(case_path,pattern='Test*.py',top_level_dir = None)
print(testunit)
return testunit
if __name__ == "__main__":
fp = open(report_abspath, "wb") # 保存报告文件
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=title)
runner.run(all_case()) # 执行用例
fp.close()
3.2 case文件1
from selenium import webdriver
import unittest
import time
class BaiduLinks(unittest.TestCase):
def setUp(self): #所有案例执行前,都先执行该方法,初始化
print("执行开始")
base_url = 'https://www.baidu.com'
self.driver = webdriver.Chrome('D:\p-y-thon\chromedriver_win32\chromedriver.exe')
self.driver.implicitly_wait(10)
self.driver.get(base_url)
def tearDown(self): #所有案例执行后,执行该方法
self.driver.close()
self.driver.quit()
print("执行结束")
def test_baidu_03(self):
driver = self.driver ##unittest
# 获取当前窗口的句柄
hand = driver.current_window_handle
print(hand)
time.sleep(5)
driver.find_element_by_link_text('新闻').click() ##unittest
time.sleep(5) ##unittest
print(driver.title)
print('----')
time.sleep(5)
#获取所有标签页窗口的句柄
all_hand = driver.window_handles
print(all_hand)
time.sleep(5)
for item in all_hand:
if item != hand:
driver.switch_to_window(item)
print(driver.title)
time.sleep(5)
self.assertIn(driver.title, u'百度新闻——海量中文资讯平台') ##unittest
def test_baidu_02(self):
driver = self.driver
#get current handle
hand = driver.current_window_handle # 获取当前窗口的句柄
print(hand)
time.sleep(5)
driver.find_element_by_link_text('hao123').click()
time.sleep(5)
print(driver.title)
print('----')
all_hand = driver.window_handles
print(all_hand)
time.sleep(5)
for item in all_hand:
if item != hand:
driver.switch_to_window(item)
print(driver.title)
time.sleep(5)
self.assertEqual(driver.title, u'hao123_上网从这里开始')
def test_baidu_01(self):
u"""百度贴吧"""
driver = self.driver
#get current handle
hand = driver.current_window_handle # 获取当前窗口的句柄
print(hand)
time.sleep(5)
driver.find_element_by_link_text('贴吧').click() #点击测试链接
time.sleep(5)
#print title
print(driver.title)
print('----')
#get all handle
all_hand = driver.window_handles
print(all_hand)
time.sleep(5)
for item in all_hand:
if item != hand:
driver.switch_to_window(item)
print(driver.title)
time.sleep(5)
# 断言
self.assertIn(driver.title, u'百度贴吧——全球最大的中文社区')
if __name__ == '__main__':
unittest.main()
print('百度链接跳转成功' + '----')
3.2 case文件2
from selenium import webdriver
import unittest
import time
class BaiduLinks(unittest.TestCase):
def setUp(self):
base_url = 'https://www.baidu.com'
self.driver = webdriver.Chrome('D:\p-y-thon\chromedriver_win32\chromedriver.exe')
self.driver.implicitly_wait(10)
self.driver.get(base_url)
def tearDown(self):
self.driver.close()
self.driver.quit()
def test_baidu_04(self):
driver = self.driver ##unittest
driver.find_element_by_link_text('新闻').click() ##unittest
time.sleep(5) ##unittest
self.assertIn(driver.title, u'百度新闻') ##unittest
def test_baidu_05(self):
driver = self.driver
driver.find_element_by_link_text('hao123').click()
time.sleep(5)
self.assertEqual(driver.title, u'hao123_上网从这里开始')
def test_baidu_06(self):
driver = self.driver
driver.find_element_by_link_text('贴吧').click()
time.sleep(5)
self.assertIn(driver.title, u'百度贴吧——全球最大的中文社区')
if __name__ == '__main__':
unittest.main()
print('百度链接跳转成功' + '----')
4. 报告样例
注意事项:
在调度文件的目录,导入HTMLTestRunner.py包