请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,报错PermissionError: [Errno 13] Permission denied:

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,只管输入

import sys

sys.path.append(‘F:\test_project’)

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)

#把整体的结构分解出来,再一步步填入

def main():

first = FirstCase()

first.test_login_email_error()

first.test_login_code_error()

first.test_login_password_error()

first.test_login_username_error()

first.test_login_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)

正在回答

2回答

你是不是用错api了。https://img1.sycdn.imooc.com//szimg/5f2a7ff10943c71308410550.jpg

你这个咋没有返回对应的res呢  可以尝试更换阿里云的验证码试别api

1 回复 有任何疑惑可以回复我~
  • 提问者 仲夏rww #1
    老师我用的验证码接口是这个:https://route.showapi.com/932-2。好像没有问题。
    回复 有任何疑惑可以回复我~ 2020-08-05 18:27:58
  • 提问者 仲夏rww #2
    def code_online(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()
        print(res.text)
        text = res.json()['showapi_res_body']['Result']
        print(text) # 返回信息
        return text #把验证码返回回去
    
    
    
    然后这样调用的。
    回复 有任何疑惑可以回复我~ 2020-08-05 18:28:38
提问者 仲夏rww 2020-08-05 16:17:29

E:\Python3.7.1\python.exe F:/test_project/test1/register_code.py

{'x': 506, 'y': 527}

                {

                  "showapi_res_error": "",

                  "showapi_res_id": "5f2a6ab28d57ba676ebd80e1",

                  "showapi_res_code": 0,

Traceback (most recent call last):

                  "showapi_res_body": {"image":"data:image/PNG;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAAUCAIAAABeYcl+AAABkElEQVR42u3XT0+DMBgG8H0OL5716NF49AOY+EG8+Rk7hIEbG9K6ghsgm/ybrJvcVm3SkEGAQediYvIeoKff4eF9Sg9pBrSA7TmBmgUWQghL4QeWydMEy4lzcf5YHHerfwHb0sjQ0/E4loAZSIEffyZGPzUHquJFo2jysnTNpa2mjobHZJOgZy9J57EJQ8+ebmQ0ADPgrvupguZ+ZBFlKvtQXkDdVsYyWK2yKH33X3GIM2m0CMLry1tJtYZkTmYEKtteBXrPusZZd/TN1RmfJmjKpRO9aQegh1DzdUcBK9uLGXqhGy3QeWsFmhE7oWk8hKOr47GHZq80Hu3RLBtF9N3DfXFK0bWZLqJZpgWjS8VsOqLZswC0AY08mvuK8ShFN4zHt/UHzbeHeHTD7dHkQ6Tbg7sFoFk2fhndNR6i0LXlwuMhYHucBM339AGN2A5duvJao3mN0zkBmtY4c9fePRgxj6bxYIdtGrHJyjsSun2N76Hz5fKX0A1rvOPVVDyaZTovFn6fPhb6qD8B/+gToXf8aexfoGOwlwAAAABJRU5ErkJggg==","ret_code":"0","sid":"1290924430560813057"}

                }

  File "F:/test_project/test1/register_code.py", line 60, in <module>

                


    run_main()

  File "F:/test_project/test1/register_code.py", line 55, in run_main

    text = code_online(file_name)

  File "F:/test_project/test1/register_code.py", line 42, in code_online

    text = res.json()['showapi_res_body']['Result']

KeyError: 'Resul


老师,问题原因找到了,终究还是识别不了验证码!

0 回复 有任何疑惑可以回复我~
  • 加油。试别不了,你去看一下res.json() 这个打印的是什么
    回复 有任何疑惑可以回复我~ 2020-08-05 17:44:45
  • 提问者 仲夏rww 回复 Mushishi #2
    老师,打印的信息如下:
    {'showapi_res_error': '', 'showapi_res_id': '5f2a89ff8d57ba666e4c3424', 'showapi_res_code': 0, 'showapi_res_body': {'image': 'data:image/PNG;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAAUCAIAAABeYcl+AAABoElEQVR42u3WwW6CQBAGYF+mL9FLD0166a3nvkHfoEmTPp4RqSAUUEAr1EVXBBGpeOsk25CJqxHoVtqkyX9gOX2QmdlpWZ1YtUx/4+hUy6zIMGauPpS09E33VDmZDjIpzI3hzl5bGxqY6kyNMtc2h4k9V/XZIosDShYDp7+NrbG2JGE8CmyDrF7l9IVOte186X6Y8jpTaCRHaTJ1ep4W5n2i+B6VloOkPYkCXe0HHdJePTzfdXfdCYlH8dxXdu2xly0ku0fzcar5eUSjrfkedFYuCVuNoMHHUqDZ8RehGegfXRd983hbvjyYiUdDMPrq+mkvFdD3F5eQk2iIQDQvZimFZmKc76OhERkRo2F6sAeMxuVRAV2UB3Y3goaaZi8r17RYNKQxNBNXndM8GqZH8TG4jgGNj3Vq+jxovhcr/GlsPTY96qFhemAij96b05UbsXD/NBpGnjA0nh4C0TA9eDRbmMAnAA2NeBDNxLXRx7a8gzfLiRvxy1caLXw13XQVvhFLofnwNV38aRwh6JoLUxk0vlxwGkPXXk3/JPqc5fEJRD/lMFMTW38AAAAASUVORK5CYII=', 'ret_code': '0', 'sid': '1290958037304172545'}}
    
    
    
    老师看看我调用的api是没问题的吧?
    接入点说明:
    随机生成验证码,返回图片base64编码。secure=false时,返回code值可直接用来校验,也可调用“校验有效性”接口进行校验。
    接口地址:
    https://route.showapi.com/932-2 (注意:接口也支持http协议,请根据自己的情况进行选择。)
    返回格式:
    json
    请求方式:
    HTTP协议,支持GET/POST方法。
    回复 有任何疑惑可以回复我~ 2020-08-05 18:30:56
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信