Take you to play JMeter serials easily through cases (19)

5.3.2 product list assertion 1 use response assertion 1) Right click the product list HTTP request and select "add - > post processor - > regular expression extractor". Set according to figure 15.

Figure 15 obtaining product list information

  • Modify the name to: obtain the product list information.
  • The reference name is: name.
  • The regular expression is: < TD; (.*?)<td>. Get the table information of name.
  • The template is: 1, which means there is 1 parameter in the regular expression.
  • The matching number is: - 1. Get all matches. (here, like the boundary extractor, 1 represents the first matching item; 2 represents the second matching item;...; n represents the nth matching item; 0 represents a matching item randomly assigned by JMeter; a negative number represents all matching items obtained).
  • The default value is null.

After running, all matching parameters can be obtained in the sampler for viewing the results, as shown in Figure 16.

Figure 16 list information of all matched products

  • name is null due to multiple pieces of information obtained.
  • name_1: The string matched to the first record is: "zhengshantang tea industry Yuanzheng Jianya Zhengshan small black tea gift box, Wuyishan tea gift".
  • name_1_g: The number of parameters matched to the first record is 1.
  • name_1_g0: regular expression, and name is used for the corresponding part_ 1. The result is: "< td > zhengshantang tea industry Yuanzheng Jianya Zhengshan small black tea gift box gift Wuyishan tea gift < td >".
  • name_1_g1 same as name_1. The string matched in the first record: "zhengshantang tea industry Yuanzheng Jianya Zhengshan small black tea gift box, Wuyishan tea gift".
  • name_2...,name_3...,name_4... And name_5... Are the values obtained from the string responses matching the 2nd, 3rd, 4th and 5th.
  • name_matchNr=5: indicates that the total number of matches is 5.

2) Right click the product list HTTP request and select "add - > assert - > respond to assert". Set according to figure 17.

Figure 17 product list information response assertion

  • Change the name to: product list information response assertion.
  • Apply to: JMeter Variable Name to use: name_matchNr.
  • Add the matching rule as "string", the content is: 5.

No exception occurred after running. Response assertions are described in detail in section 5.3-1. 2 use BeanShell assertion 1) Right click the product list HTTP request and select "add - > assert - > BeanShell assert". Write the assertion code as shown in Figure 18.

Figure 18 BeanShell assertion for product list

2) Modify the name: product list BeanShell assertion, and then enter the following code in the script.

//Get system variables
String name1 = vars.get("name_1");
String name2 = vars.get("name_2");
String name3 = vars.get("name_3");
String name4 = vars.get("name_4");
String name5 = vars.get("name_5");
String name_matchNr = vars.get("name_matchNr");

//Judge whether the return value is consistent with the expected value
Failure = false;
if (! name1.equals("Zhengshantang tea industry Yuanzheng simple and elegant Zhengshan small black tea gift box gift Wuyishan tea gift")){
Failure = true;
FailureMessage = "name1 Information does not meet expectations";
}else if(! name2.equals("Black tea Zhengshan race Wuyishan black tea 170 g Spring tea bag 170 g Bulk new tea")){
Failure = true;
FailureMessage = "name2 Information does not meet expectations";
}else if(! name3.equals("Jinpao nectar xiangzhengshan small black tea 300 g Kraft paper bag gift box Wuyishan tongmuguan package")){
Failure = true;
FailureMessage = &quot; name3 Information does not meet expectations&quot;;
}else if(! name4.equals("Zhengshan small black tea super new tea gift box with longan Fragrance Gift black tea warm stomach nourishing tea 250 g")){
Failure = true;
FailureMessage = "name4 Information does not meet expectations";
}else if(! name5.equals("2016 Spring tea Wuyi black tea tongmuguan wild black tea Zhengshan race parcel post office tea")){
Failure = true;
FailureMessage = "name5 Information does not meet expectations";
}else if(!name_matchNr.equals("5")){
Failure = true;
ailureMessage = "name_matchNr Information does not meet expectations";
}

No exception occurred after running. Thus, BeanShall asserts that although it is difficult to master, its function is more powerful. The bean shell assertion is described in detail in section 5.3-2. Note: in normal operation, you can choose between response assertion and BeanShell assertion, and you don't need to do it all. But I personally think it is recommended to use bean shell assertion here. 3.3 new version product list In the new version, only the XML file is downloaded from the server, and then the HTML file is parsed. The XML file is in the following form.

CATALOG>
<GOOD>
<ID>1</ID>
<NAME>Zhengshantang tea industry Yuanzheng simple and elegant Zhengshan small black tea gift box gift Wuyishan tea gift</NAME>
<PRICE>¥238.0</PRICE>
</GOOD>
<GOOD>
<ID>2</ID>
<NAME>Black tea Zhengshan race Wuyishan black tea 170 g Spring tea bag 170 g Bulk new tea</NAME>
<PRICE>¥25.0</PRICE>
</GOOD>
<GOOD>
<ID>3</ID>
<NAME>Jinpao nectar xiangzhengshan small black tea 300 g Kraft paper bag gift box Wuyishan tongmuguan package</NAME>
<PRICE>¥188.0 </PRICE>
<GOOD>
<GOOD>
<ID>4</ID>
<NAME>Zhengshan small black tea super new tea gift box with longan Fragrance Gift black tea warm stomach nourishing tea 250 g</NAME>
<PRICE>¥238.12</PRICE>
</GOOD>
<GOOD>
<ID&>5</ID>
<NAME>2016 Spring tea Wuyi black tea tongmuguan wild black tea Zhengshan race parcel post office tea</NAME>
<PRICE>¥68.0</PRICE>
</GOOD>
</CATALOG>

We store it in Tomcat. The request address is: http://127.0.0.1:8080/sec/48/goods.xml . 1) Establish an HTTP request. As shown in Figure 19.

Figure 19 new version product list HTTP request

  • Name: product list (new).
  • HTTP request: GET.
  • Path: / sec/48/goods.xml.

Automatic redirection. 2) Right click the product list HTTP request (new) and select "add - > assertion - > XML assertion". Set according to figure 20.

Figure 20 XML assertion

XML assertion, just to ensure that the XML format is correct.

3) Right click the product list HTTP request (new) and select "add - > assert - > XPath assert". Set according to figure 21.

Figure 21 product list (new)XPath assertion

  • Modify the name: product list (new)XPath assertion.
  • Apply to: Main sample only.
  • XPath: //PRICE[text()="¥238.12"]. (there is < price > ¥ 238.12 < / price >).

Finally, click [verify] to ensure that the XPath format is entered correctly. We introduce XPath assertions in section 5.8. 4) XPath is now available in version 2.0. Therefore, we can also use XPath2 Assertion to assert. Right click the product list HTTP request (new) and select "add - > assert - > XPath2 Assertion". Set according to figure 22.

Figure 22 new xpath2 assertion

  • Modify the name: product list (new) XPath2 Assertion.
  • Apply to: Main sample only.
  • XPath: //PRICE[text()='¥68.0']. (there is < price > ¥ 68.0 < / price >).

Finally, click [Validate xpath expression] to ensure that the XPath format is entered correctly. We introduced XPath 2 assertion in section 5.9.

Welcome Meng's evening boat home

Female Meng, I am only in April and have been alone for more than a thousand days.

Tonight, Canada returns to China and the whole country is boiling to welcome Mulan.

If China wants to rise, educational science must be stronger.

To be among the world's leaders, we need to work harder.

Nine nine six to sincere valuable, mind limbs flesh body.

Scientific management of enterprises, brain movement, twice the result with half the effort, showing divine power.

Posted by The14thGOD on Tue, 02 Nov 2021 02:57:07 -0700