1、在HTML测试报告的错误是这样的,改了很多地方都没解决:
ft1.1: Traceback (most recent call last):
File “F:/test_project/case/first_case.py”, line 62, in test_login_success
success = self.login.user_base(‘14@qq.com’,‘user111’,‘111111’,self.file_name)
File “F:\test_project\business\register_business.py”, line 13, in user_base
self.register_h.send_user_code(file_name)
File “F:\test_project\handle\register_handle.py”, line 26, in send_user_code
code = get_code_text.code_online(file_name)
File “F:\test_project\util\get_code.py”, line 25, in code_online
self.get_code_image(file_name)
File “F:\test_project\util\get_code.py”, line 17, in get_code_image
im = Image.open(file_name)
File “E:\Python3.7.1\lib\site-packages\PIL\Image.py”, line 2878, in open
fp = builtins.open(filename, “rb”)
PermissionError: [Errno 13] Permission denied: 'F:/test_project/test_images’
Traceback (most recent call last):
File “F:/test_project/case/first_case.py”, line 62, in test_login_success
success = self.login.user_base(‘14@qq.com’,‘user111’,‘111111’,self.file_name)
File “F:\test_project\business\register_business.py”, line 13, in user_base
self.register_h.send_user_code(file_name)
File “F:\test_project\handle\register_handle.py”, line 26, in send_user_code
code = get_code_text.code_online(file_name)
File “F:\test_project\util\get_code.py”, line 25, in code_online
self.get_code_image(file_name)
File “F:\test_project\util\get_code.py”, line 17, in get_code_image
im = Image.open(file_name)
File “E:\Python3.7.1\lib\site-packages\PIL\Image.py”, line 2878, in open
fp = builtins.open(filename, “rb”)
PermissionError: [Errno 13] Permission denied: ‘F:/test_project/test_images’
2、代码如下,和老师写的没什么两样,不知为何报错:
“get_code”
#coding = utf-8
from PIL import Image
from test1.ShowapiRequest import ShowapiRequest
import time
class GetCode:
def init(self,driver):
self.driver = driver
# 获取图片
def get_code_image(self, file_name):
self.driver.save_screenshot(file_name) # 直接保存了这张图片
code_element = self.driver.find_element_by_id(‘getcode_num’)
# print(code_element.location) # {“x”:123,“y”:345}
left = code_element.location[‘x’]
top = code_element.location[‘y’]
right = code_element.size[‘width’] + left
height = code_element.size[‘height’] + top
im = Image.open(file_name)
# crop的作用是按照一定的坐标裁剪
img = im.crop((left, top, right, height))
img.save(file_name) # 只有一个文件
time.sleep(2)
# 解析图片获取验证码
def code_online(self, file_name):
self.get_code_image(file_name)
r = ShowapiRequest("https://route.showapi.com/932-2", "308084", "e4c61de0223d44fdb9b71714ca2ea81f")
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addFilePara("image", file_name) # 文件上传时设置
res = r.post()
time.sleep(2)
text = res.json()['showapi_res_body']
code = text['Result']
return code
“ RegisterBusiness”
# 验证码错误
def login_code_error(self, email, name, password, file_name):
self.user_base(email, name, password, file_name)
if self.register_h.get_user_text(‘code_text_error’, ‘验证码错误’) == None:
print(‘验证码检验不成功’)
return True
else:
return False
“first_case”
#coding=utf-8
#PO模式:case、页面、数据全部分离开来
#下面是一条条的用例,case,只管输入
from selenium import webdriver
from business.register_business import RegisterBusiness
import unittest
import warnings
import os
import HTMLTestRunner
class FirstCase(unittest.TestCase):
#前置是直接生成,此处就不用构造函数了
#self.driver 是全局的
def setUp(self):
warnings.simplefilter(‘ignore’, ResourceWarning)
self.driver = webdriver.Chrome()
self.driver.get(‘http://www.5itest.cn/register’)
self.login = RegisterBusiness(self.driver)
self.file_name = “F:/test_project/test_images”
#后置做的事情仅仅是关闭浏览器
def tearDown(self):
for method_name,error in self._outcome.errors:
if error:
case_name = self._testMethodName
file_path = os.path.join(os.path.abspath(os.path.dirname(os.getcwd())) + ‘/report/’+ case_name + ‘.png’)
self.driver.save_screenshot(file_path)
self.driver.close()
print('这个是case的后置条件')
#判断邮箱检验是否正确
def test_login_email_error(self):
email_error = self.login.login_email_error('1423','user111','111111',self.file_name)
# if email_error == True:
# print('注册成功,此条case失败')
#通过assert 判断是否是 error
return self.assertTrue(email_error, "test_login_email_error用例执行了")
# 判断用户名检验是否正确
def test_login_username_error(self):
username_error = self.login.login_name_error('14@qq.com', '@#$', '111111', self.file_name)
# if username_error == True:
# print('注册成功,此条case失败')
self.assertTrue(username_error)
# 判断密码检验是否正确
def test_login_password_error(self):
password_error = self.login.login_password_error('14@qq.com', 'user111', '0', self.file_name)
# if password_error == True:
# print('注册成功,此条case失败')
self.assertTrue(password_error)
# 判断验证码检验是否正确
def test_login_code_error(self):
code_error = self.login.login_code_error('14@qq.com', 'user111', '1', self.file_name)
# if code_error == True:
# print('注册成功,此条case失败')
self.assertTrue(code_error)
# 是否注册成功,true是成功
def test_login_success(self):
success = self.login.user_base('14@qq.com','user111','111111',self.file_name)
if self.login.register_sucess()== True:
print("注册成功")
# self.assertTrue( success)
#把整体的结构分解出来,再一步步填入
if name == ‘main’:
#不需要加’/report/’,会报错
file_path = os.path.join(os.path.abspath(os.path.dirname(os.getcwd()))+’/report/’+‘first_case.html’)
#把file_path打开,而且要以读写的模式
f = open(file_path,‘wb’)
suite = unittest.TestSuite()
# suite.addTest(FirstCase(‘test_login_email_error’))
# suite.addTest(FirstCase(‘test_login_username_error’))
# suite.addTest(FirstCase(‘test_login_password_error’))
# suite.addTest(FirstCase(‘test_login_code_error’))
suite.addTest(FirstCase(‘test_login_success’))
# unittest.TextTestRunner().run(suite)
runner = HTMLTestRunner.HTMLTestRunner(stream = f,title=‘this is first report’,description=‘这是我们第一次测试报告’,verbosity=2)
runner.run(suite)