HttpRunner automated testing, get response data & extract value to variable

Get response data

extract: extract

Note: extract should be at the same level as request

1. Response line, response header; extract the response data through extract and store it in a variable, as shown in the figure below:

Note: There must be - in front of the variable name 

# 获取响应数据: 响应行(200,ok)\响应头
- config:
    name: 测试百度网站
    base_url: https://www.baidu.com

- test:
    name: 接口名称 百度接口
    request:
      url: /
      method: GET
    extract:  # 提取值存储到变量中
      - code: status_code  # 响应码
      - info: reason       # ok
      - header_Content: headers.Content-Type  # 响应头部
    validate:
      - eq: [$code,200]  # 引用变量  $变量名
      - eq: [$info,"OK"]
      - eq: [$header_Content,'text/html']

2. Get the response text

extract parses the response body (regular support)

Extract the data of the response body through extract and store it in a variable (regular extraction can be used), as shown in the following figure:

Note: If the assertion is Chinese, just add the Accept-Language of the headers, and there are specially written Chinese garbled codes later to solve the problem of decoding

# 获取响应数据  响应正文(支持正则)
- config:
    name: 百度
    base_url: https://www.baidu.com

- test:
    name: 百度主页
    request:
        url: /
        method: GET
        headers:  # 如果断言为中文的话,加上headers的Accept-Language即可
          Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
          Accept-Encoding: gzip, deflate, br
          Accept-Language: zh-CN,zh;q=0.9
          User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
    extract:
      - title: <title>(.+?)</title>  # 可以使用正则表达式提取
    validate:
      - eq: [$title,"百度一下,你就知道"]

extract parses the response body (supports json)

Extract the data of the response body through extract and store it in a variable (extract json data), the format takes content as the root node: content.key.key.key; as shown below:

Note: The json view can be displayed more clearly through https://www.bejson.com/

# 获取响应数据  响应正文 (支持json)
- config:
    name: 用户标签管理
    base_url: https://api.weixin.qq.com

- test:
    name: 百度主页
    request:
        url: /cgi-bin/tags/get
        method: GET
        params:
          access_token: 49_lsdk_pQJJ4R5IWdWVcDTQu3bHyVOsHDlAcuA99UtVwsmzrtHhSGJKgSPMi3i3TdOQrGeuzZdB62K1uhcKJQAk6eKjzlBL7HgWvAmw7gfiRTp00QnLdSZzN7ul9f2TMPex-Iz2tCg-ZWsSPLbJTJdABAYIY
    extract:
      - id: content.tags.0.id
      - name: content.tags.0.name
    validate:
      - eq: [$id,2]
      - eq: [$name,"星标组"]

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/129836628