1、commom·公共配置,
【1】包含common1·公共方法、
from webdriver_manager.chrome import ChromeDriverManager
from seleniumwire import webdriver
import xlrd
class common2:
def __init__(self):
#调用webdeiver
self.driver=webdriver.Chrome(ChromeDriverManager().install())
def set_header(self, request):
#添加header
request.headers['***']='***'
def URL(self):
#调用系统地址
return "https://"
def login(self):
#调试窗口大小、设置隐式等待、添加herder
self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.driver.request_interceptor = self.set_header
self.driver.get(self.URL())
#调用cookie
wb=xlrd.open_workbook('cookie.xlsx')
sheel=wb.sheet_by_name('sheet1')
for row in range(1,sheel.nrows):
self.driver.add_cookie({'name':sheel.cell_value(row,0),'value':sheel.cell_value(row,1),'domain':sheel.cell_value(row,2),'path':sheel.cell_value(row,3)})
#刷新页面
self.driver.refresh()
return self.driver
【2】cookie·登陆跳过验证获取、
import time
import xlwt
from webdriver_manager.chrome import ChromeDriverManager
from seleniumwire import webdriver
#前置部署
driver=webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://")
#30秒时间手动登陆够不够
time.sleep(60)
#把新生成的cookie写入xlsx
workbook=xlwt.Workbook(encoding='utf-8')
worksheet=workbook.add_sheet('sheet1')
worksheet.write(0,0,'name')
worksheet.write(0,1,'value')
worksheet.write(0,2,'domain')
worksheet.write(0,3,'path')
i=1
for cookie in driver.get_cookies():
worksheet.write(i,0,cookie['name'])
worksheet.write(i,1,cookie['value'])
worksheet.write(i,2,cookie['domain'])
worksheet.write(i,3,cookie['path'])
i=i+1
workbook.save('cookie.xlsx')
#退出页面
driver.quit()
【3】HTMLTestReportCN·网页版测试报告
2、reports·存放测试报告
3、testcases·存放测试用例
4、run_all_cases.py·执行所有测试用例并保存测试报告到reports
import os
import unittest
from common import HTMLTestReportCN
#导入所有case
case_path=os.path.dirname(__file__)+'/testcases/'
discover=unittest.defaultTestLoader.discover(start_dir=case_path,pattern='test_*.py',top_level_dir=case_path)
all_case_suite=unittest.TestSuite()
all_case_suite.addTest(discover)
#把运行结果导入文件中
report_path=os.path.dirname(__file__)+'/reports/'
reports_dir=HTMLTestReportCN.ReportDirectory(report_path)
reports_dir.create_dir('***自动化测试项目')
report_html_path=HTMLTestReportCN.GlobalMsg.get_value('report_path')
html_report_file=open(report_html_path,'wb')
test_runner=HTMLTestReportCN.HTMLTestRunner(stream=html_report_file,title='***自动化测试项目',description='testV1.0',tester='老拐')
test_runner.run(all_case_suite)