HttpRunner learning 4-- use regular expressions to extract data

Foreword

In HttpRunner we extract by extracting data, the response result when the structure of JSON, use content in combination . Operator means, such as content.code , very convenient to use, but if the response result to text / html structure, then You can not be so used.

At this time, we can not use regular expressions to extract it? The answer is yes.

testing scenarios

Here, I will visit TesterHome home page, then to extract title by title in the positive response data.

Access TesterHome

Fiddler capture

extract and extract assertions of regular

Capture from the above results, we can see that if you want to extract the title of TesterHome keywords, you can use regular expressions as: <title> </ title> (+.?) . YAML format used is as follows:

- test:
    name: visit TesterHome
    request:
      url: https://testerhome.com/
      method: GET
    extract:
      - title: <title>(.+?)</title>
    validate:
      - eq: [status_code, 200]
      - eq: [$title, "TesterHome"]

Regular use of data extraction, into the variable title , if you need to use the back, through the $ title referenced.

validate the use of regular

In addition to using regular response when extracting data extract, we can also use regular direct assertion. YAML format used is as follows:

- test:
    name: visit TesterHome 2
    request:
      url: https://testerhome.com/
      method: GET
    validate:
      - eq: [status_code, 200]
      - eq: ['<title>(.+?)</title>', "TesterHome"]

I use a regular here, the direct use <title> (. +?) </ Title> will complain, we add can be single or double quotes, such as '<title> (. +? ) </ Title>' or "<title> (. +?) </ title>" .

Complete use cases:

- config:
    name: re test

- test:
    name: visit TesterHome
    request:
      url: https://testerhome.com/
      method: GET
    extract:
      - title: <title>(.+?)</title>
    validate:
      - eq: [status_code, 200]
      - eq: [$title, "TesterHome"]

- test:
    name: visit TesterHome 2
    request:
      url: https://testerhome.com/
      method: GET
    validate:
      - eq: [status_code, 200]
      - eq: ['<title>(.+?)</title>', "TesterHome"]

Guess you like

Origin www.cnblogs.com/wintest/p/11801547.html