阅读 69

httprunner 2.x学习15 - response 返回 html 页面解码

前言

requests 发送请求返回的 html 页面,默认是按 "ISO-8859-1" 编码解码,经常会出现返回的 html 出现乱码的情况。
httprunner 3.x可以在debugtalk.py 写个hook函数解码返回的html内容

response 解码

requests 直接请求页面,返回的html里面有乱码

import requests

url = "https://home.cnblogs.com/u/yoyoketang/"
r = requests.get(url)
print(r.encoding)
print(r.text)

运行结果

ISO-8859-1


  
    
    
    ?????¢??-
    
    
    
    
    
  

解决办法可以指定 response 的解码格式

import requests
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

url = "https://home.cnblogs.com/u/yoyoketang/"
r = requests.get(url)
r.encoding = "utf-8"  # 解码方式
print(r.encoding)
print(r.text)

于是就能看到正常的html内容了

utf-8


  
    
    
    博客园
    
    
    
    
    

hrun 2.x问题描述

在httprunner2.x 中看到正则表达式没法提取乱码

FAIL: test_0000_000 (httprunner.api.TestSequense)
test demo case1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\api.py", line 63, in test
    test_runner.run_test(test_dict)
httprunner.exceptions.ValidationFailure:
validate: (.+?) equals 博客园(str)       ==> fail
?????¢??-(str) equals 博客园(str)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\api.py", line 65, in test
    self.fail(str(ex))
AssertionError:
validate: (.+?) equals 博客园(str)       ==> fail
?????¢??-(str) equals 博客园(str)

----------------------------------------------------------------------

teardown_hook解码

在debugtalk.py 中写一个hook函数解码返回的response内容

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

def response_decode(response):
    """解码返回的html内容"""
    print(response)
    print(response.resp_obj)  # requests.response
    response.resp_obj.encoding = "utf-8"

response.resp_obj 就是 requests 库的 response 对象

yaml脚本如下

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

config:
    name: yoyoketang

teststeps:
-
    name: yoyoketang
    request:
        url: https://home.cnblogs.com/u/yoyoketang/
        method: GET
        headers:
            User-Agent: Fiddler
            Content-Type: application/json
        verify: false
    teardown_hooks:
        - ${response_decode($response)}
    validate:
        - eq: [status_code, 200]
        - eq: [‘(.+?)‘, 博客园]

测试报告内容也会解码

原文:https://www.cnblogs.com/yoyoketang/p/14926423.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐