jmeter regular expression writing

In the testing process, there are often the following scenarios, such as the return value of interface A, which is used in interface B, and the account that A logs in, the sid value still changes each time you log in. So in practice, how can we extract parameters from interface A to interface B? Next we can use regular expressions to solve this problem.

The scenario is as follows, a login interface, after logging in, you need to use the account to purchase the target, and use the login information value of A when buying the target. Through the interface capture, we analyze the return value as follows:

We know the value of sid to extract. Next, we briefly explain the use of regular expressions:

illustrate:

(1) Reference name: The name of the parameter to be referenced in the next request. If Atask is filled in, it can be referenced by ${Atask}.

(2) Regular expression:

    (): The enclosed part is to be extracted.

    .: matches any string.
    +: One or more times.
    ?: Don't be too greedy, stop after the first match is found.

(3) Template: refer to it with $$. If there are multiple regular expressions in the regular expression, it can be $2$$3$, etc., which means the parsed value is given to the title. For example: $1$ represents the first value parsed

(4) Matching numbers: 0 means random value, 1 means all values, usually fill in 0

(5) Default value: If the parameter does not get a value, then a value is given by default to let it get it, and I fill in Error.

There is also a way to judge whether our regular expression is wrong.

Tool name: RegexTester, download address: Baidu, many.

Screenshot of how to use:

Used in jmeter as follows:

 

 Additional example:

case:

After sending an Http request through jmeter, how to get the content value of the response result?

Common scenario 1: For example, a request for a bid is sent, and the return is a string of JSON strings. Because you want to judge whether the response result is correct, you can obtain the value of the result (because when result = 1, the bid is successful), and the format of the response result is As follows: {"errorMsg":null,"result":1,"data":{"result":"2","msg":"bidding successful!"}}, or you can judge the msg content.

Common scenario two:

There are two interfaces, A and B. The input parameters of the B interface are derived from the response results of the A interface. At this time, the response field value of the A interface also needs to be extracted.

Closer to home, how to get it? Usually you can use "regular expression extractor", Beanshell PostProcessor, etc., here is the use of regular expressions to extract,

 

1. Add a new Http request, set as follows:

 

2. Under the Http request, add a regular expression extractor, the specific settings are shown in the following figure:

 

 

Glossary:

Apply to: Scope of application

Response Fields to Check: Sample Data Source

Reference name: variable name when referenced elsewhere, reference method: ${reference name}

Regular Expressions: Data Extractors

  • . matches any character

  • + one or more times

  • ? stop when the first match succeeds

Template: Indicates the use of the first few values ​​extracted:
$-1$:表示取所有值
$0$:表示随机取值
$1$:表示取第1个
$2$:表示取第二个
以此类推:$n$:表示取第n个

 

匹配数字:0 代表随机取值,1 代表全部取值

缺省值:如果正则表达式没有搜找到值,则使用此缺省值

 

3. Add a Debug Sampler,

4. After the execution, the result is as follows:

 

 

Remarks: The result of regular expression extraction is stored in result,

result_g0 : All content matched according to the matching rule;

result_g1: Match the content that meets the conditions according to the matching rules;

 

4. Then you can use the extracted data, use ${result}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750928&siteId=291194637