1.使用方法:
driver.save_screenshot(screen_path+'/'+now+'.png')
其中:screen_path为具体路径, now是加了个时间点, png是以png格式保存
test01.py
run_all_case.py
具体代码操作(可粘贴):
import unittest
import time
from seleniumimport webdriver
import unittest
import os
import HTMLTestRunner
import time
import smtplib
from email.mime.multipartimport MIMEMultipart
from email.mime.textimport MIMEText
from email.headerimport Header
import datetime
screen_path=os.path.join(os.getcwd(), "screen")
now=time.strftime("%Y-%m-%d %H_%M_%S")
class Test(unittest.TestCase):
##calssmethod使用, 所有用例执行前执行一次,所有用例执行后执行一次
@classmethod
def setUpClass(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url ="http://vue-fop-master-hwkp.prod.kp.tcpjw.org/#/login"
@classmethod
def tearDownClass(self):
self.driver.quit()
print("end")
def test01(self):
driver =self.driver
driver.get(self.base_url +"/")
driver.maximize_window()
print("执行测试用例01")
try:
driver.find_element_by_xpath('XXXXXXX').send_keys('XXXXXXX')
print('成功')
except Exception:
driver.save_screenshot(screen_path+'/'+now+'.png')
print('失败')
@unittest.skip("直接跳过测试")
def test02(self):
print("执行测试用例02")
def test03(self):
print("执行测试用例03")
def test04(self):
print("执行测试用例04")
if __name__ =="__mian__":
unittest.main()
代码2run_all_case.py部分代码粘贴
# coding:utf-8
import unittest
import os
import HTMLTestRunner
import time
import smtplib
from email.mime.multipartimport MIMEMultipart
from email.mime.textimport MIMEText
from email.headerimport Header
import datetime
now = time.strftime("%Y-%m-%d %H_%M_%S")
# 用例路径
case_path = os.path.join(os.getcwd(), "testcase")
#报告存放文件夹路径
test_report ="D:\\pythonproject\\Autotest\\report"
# 报告存放路径
report_path = os.path.join(os.getcwd(), "D:\\pythonproject\\Autotest\\report\\"+now+"report.html")
##用例集
def all_case():
discover = unittest.defaultTestLoader.discover(case_path,
pattern="test*.py",
top_level_dir=None)
print(discover)
return discover
if __name__ =='__main__':
runner=unittest.TextTestRunner()
# ##生成报告
# fp = open(report_path,"wb")
# runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='这是我的自动化测试报告', description='用例执行情况:')
# ##执行所有用例
runner.run(all_case())
# fp.close()
#
# new_report =new_report(test_report)
# print("hehe"+ new_report)
# send_mail(new_report)
网友评论