qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

jmeter之断言

18.5.5 BeanShell Assertion

The BeanShell Assertion allows the user to perform assertion checking using a BeanShell script.

For full details on using BeanShell, please see the BeanShell website.

Note that a different Interpreter is used for each independent occurence of the assertion in each thread in a test script, but the same Interpreter is used for subsequent invocations. This means that variables persist across calls to the assertion.

All Assertions are called from the same thread as the sampler.

If the property "beanshell.assertion.init" is defined, it is passed to the Interpreter as the name of a sourced file. This can be used to define common methods and variables. There is a sample init file in the bin directory: BeanShellAssertion.bshrc

The test element supports the ThreadListener and TestListener methods. These should be defined in the initialisation file. See the file BeanShellListeners.bshrc for example definitions.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree. The name is stored in the script variable LabelNo
Reset bsh.Interpreter before each callIf this option is selected, then the interpreter will be recreated for each sample. This may be necessary for some long running scripts. For further information, see Best Practices - BeanShell scripting .Yes
ParametersParameters to pass to the BeanShell script. The parameters are stored in the following variables:
  • Parameters - string containing the parameters as a single variable
  • bsh.args - String array containing parameters, split on white-space
No
Script fileA file containing the BeanShell script to run. This overrides the script. The file name is stored in the script variable FileNameNo
ScriptThe BeanShell script to run. The return value is ignored.Yes (unless script file is provided)

 

There's a sample script you can try.

Before invoking the script, some variables are set up in the BeanShell interpreter. These are strings unless otherwise noted:

  • log - the Logger Object. (e.g.) log.warn("Message"[,Throwable])
  • SampleResult - the SampleResult Object; read-write
  • Response - the response Object; read-write
  • Failure - boolean; read-write; used to set the Assertion status
  • FailureMessage - String; read-write; used to set the Assertion message
  • ResponseData - the response body (byte [])
  • ResponseCode - e.g. 200
  • ResponseMessage - e.g. OK
  • ResponseHeaders - contains the HTTP headers
  • RequestHeaders - contains the HTTP headers sent to the server
  • SampleLabel
  • SamplerData - data that was sent to the server
  • ctx - JMeterContext
  • vars - JMeterVariables - e.g. vars.get("VAR1"); vars.put("VAR2","value"); vars.putObject("OBJ1",new Object());
  • props - JMeterProperties (class java.util.Properties) - e.g. props.get("START.HMS"); props.put("PROP1","1234");

 

The following methods of the Response object may be useful:

  • setStopThread(boolean)
  • setStopTest(boolean)
  • String getSampleLabel()
  • setSampleLabel(String)

18.5.10 BSF Assertion

The BSF Assertion allows BSF script code to be used to check the status of the previous sample.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
LanguageThe BSF language to be usedYes
ParametersParameters to pass to the script. The parameters are stored in the following variables:
  • Parameters - string containing the parameters as a single variable
  • args - String array containing parameters, split on white-space
No
Script fileA file containing the script to run, if a relative file path is used, then it will be relative to directory referenced by "user.dir" System propertyNo
ScriptThe script to run.Yes (unless script file is provided)

 

The script (or file) is processed using the BSFEngine.exec() method, which does not return a value.

The following variables are set up for use by the script:

  • log - (Logger) - can be used to write to the log file
  • Label - the String Label
  • Filename - the script file name (if any)
  • Parameters - the parameters (as a String)
  • args[] - the parameters as a String array (split on whitespace)
  • ctx - ( JMeterContext ) - gives access to the context
  • vars - ( JMeterVariables ) - gives read/write access to variables: vars.get(key); vars.put(key,val); vars.putObject("OBJ1",new Object()); vars.getObject("OBJ2");
  • props - (JMeterProperties - class java.util.Properties) - e.g. props.get("START.HMS"); props.put("PROP1","1234");
  • SampleResult, prev - ( SampleResult ) - gives access to the previous SampleResult (if any)
  • sampler - (Sampler)- gives access to the current sampler
  • OUT - System.out - e.g. OUT.println("message")
  • AssertionResult - the assertion result

The script can check various aspects of the SampleResult . If an error is detected, the script should use AssertionResult.setFailureMessage("message") and AssertionResult.setFailure(true).

For futher details of all the methods available on each of the above variables, please check the Javadoc



8.5.12 Compare Assertion

 

Compare Assertion MUST NOT BE USED during load test as it consumes a lot of resources (memory and CPU). Use it only for either functional testing or during Test Plan debugging and Validation.

 

The Compare Assertion can be used to compare sample results within its scope. Either the contents or the elapsed time can be compared, and the contents can be filtered before comparison. The assertion comparisons can be seen in the Comparison Assertion Visualizer .

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Compare ContentWhether or not to compare the content (response data)Yes
Compare TimeIf the value is >=0, then check if the response time difference is no greater than the value. I.e. if the value is 0, then the response times must be exactly equal.Yes
Comparison FiltersFilters can be used to remove strings from the content comparison. For example, if the page has a time-stamp, it might be matched with: "Time: \d\d:\d\d:\d\d" and replaced with a dummy fixed time "Time: HH:MM:SS".No

 




8.5.7 HTML Assertion

The HTML Assertion allows the user to check the HTML syntax of the response data using JTidy.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
doctypeomit/auto/strict/looseYes
FormatHTML, XHTML or XMLYes
Errors onlyOnly take note of errors?Yes
Error thresholdNumber of errors allowed before classing the response as failedYes
Warning thresholdNumber of warnings allowed before classing the response as failedYes
FilenameName of file to which report is writtenNo

 


18.5.6 MD5Hex Assertion

The MD5Hex Assertion allows the user to check the MD5 hash of the response data.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
MD5 sum32 hex digits representing the MD5 hash (case not significant)Yes

 



18.5.3 Size Assertion

The Size Assertion tests that each response contains the right number of bytes in it. You can specify that the size be equal to, greater than, less than, or not equal to a given number of bytes.

 

Since JMeter 2.3RC3, an empty response is treated as being 0 bytes rather than reported as an error.

 

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Apply to:This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
  • Main sample only - assertion only applies to the main sample
  • Sub-samples only - assertion only applies to the sub-samples
  • Main sample and sub-samples - assertion applies to both.
  • JMeter Variable - assertion is to be applied to the contents of the named variable
Yes
Size in bytesThe number of bytes to use in testing the size of the response (or value of the JMeter variable).Yes
Type of ComparisonWhether to test that the response is equal to, greater than, less than, or not equal to, the number of bytes specified.Yes

 




18.5.13 SMIME Assertion

The SMIME Assertion can be used to evaluate the sample results from the Mail Reader Sampler. This assertion verifies if the body of a mime message is signed or not. The signature can also be verified against a specific signer certificate. As this is a functionality that is not necessarily needed by most users, additional jars need to be downloaded and added to JMETER_HOME/lib : 
  • bcmail-xxx.jar (BouncyCastle SMIME/CMS)
  • bcprov-xxx.jar (BouncyCastle Provider)
These need to be downloaded from BouncyCastle.

If using the Mail Reader Sampler , please ensure that you select "Store the message using MIME (raw)" otherwise the Assertion won't be able to process the message correctly.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Verify SignatureIf selected, the asertion will verify if it is a valid signature according to the parameters defined in the Signer Certificate box.Yes
Message not signedWhether or not to expect a signature in the messageYes
Signer Cerificate"No Check" means that it wil not perform signature verification. "Check values" is used to verify the signature against the inputs provided. And "Certificate file" will perform the verification against a specific certificate file.Yes
Message PositionThe Mail sampler can retrieve multiple messages in a single sample. Use this field to specify which message will be checked. Messages are numbered from 0, so 0 means the first message. Negative numbers count from the LAST message; -1 means LAST, -2 means penultimate etc.Yes

 


18.5.9 XML Schema Assertion

The XML Schema Assertion allows the user to validate a response against an XML Schema.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
File NameSpecify XML Schema File NameYes


18.5.4 XML Assertion

The XML Assertion tests that the response data consists of a formally correct XML document. It does not validate the XML based on a DTD or schema or do any further validation.

Control Panel

Parameters 

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No


18.5.8 XPath Assertion

The XPath Assertion tests a document for well formedness, has the option of validating against a DTD, or putting the document through JTidy and testing for an XPath. If that XPath exists, the Assertion is true. Using "/" will match any well-formed document, and is the default XPath Expression. The assertion also supports boolean expressions, such as "count(//*error)=2". See http://www.w3.org/TR/xpath for more information on XPath.

Some sample expressions:
  • //title[text()='Text to match'] - matches <text>Text to match</text> anywhere in the response
  • /title[text()='Text to match'] - matches <text>Text to match</text> at root level in the response

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Use Tidy (tolerant parser)Use Tidy, i.e. be tolerant of XML/HTML errorsYes
QuietSets the Tidy Quiet flagIf Tidy is selected
Report ErrorsIf a Tidy error occurs, then set the Assertion accordinglyIf Tidy is selected
Show warningsSets the Tidy showWarnings optionIf Tidy is selected
Use NamespacesShould namespaces be honoured?If Tidy is not selected
Validate XMLCheck the document against its schema.If Tidy is not selected
Ignore WhitespaceIgnore Element Whitespace.If Tidy is not selected
Fetch External DTDsIf selected, external DTDs are fetched.If Tidy is not selected
XPath AssertionXPath to match in the document.Yes
True if nothing matchesTrue if a XPath expression is not matchedNo

 

 

The non-tolerant parser can be quite slow, as it may need to download the DTD etc.

 

 

As a work-round for namespace limitations of the Xalan XPath parser implementation on which JMeter is based, you can provide a Properties file which contains mappings for the namespace prefixes:
  • prefix1=Full Namespace 1
  • prefix2=Full Namespace 2
  • ...
You reference this file in jmeter.properties file using the property:
  • xpath.namespace.config

 

18.5.1 Response Assertion

The response assertion control panel lets you add pattern strings to be compared against various fields of the response. The pattern strings are:

  • Contains, Matches: Perl5-style regular expressions
  • Equals, Substring: plain text, case-sensitive

 

A summary of the pattern matching characters can be found at ORO Perl5 regular expressions.

You can also choose whether the strings will be expected to match the entire response, or if the response is only expected to contain the pattern. You can attach multiple assertions to any controller for additional flexibility.

Note that the pattern string should not include the enclosing delimiters, i.e. use Price: \d+ not /Price: \d+/ .

By default, the pattern is in multi-line mode, which means that the "." meta-character does not match newline. In multi-line mode, "^" and "$" match the start or end of any line anywhere within the string - not just the start and end of the entire string. Note that \s does match new-line. Case is also significant. To override these settings, one can use the extended regular expression syntax. For example:

    (?i) - ignore case     (?s) - treat target as single line, i.e. "." matches new-line     (?is) - both the above     These can be used anywhere within the expression and remain in effect until overriden.  e.g.     (?i)apple(?-i) Pie - matches "ApPLe Pie", but not "ApPLe pIe"     (?s)Apple.+?Pie - matches Apple followed by Pie, which may be on a subsequent line.     Apple(?s).+?Pie - same as above, but it's probably clearer to use the (?s) at the start.    

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Apply to:This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
  • Main sample only - assertion only applies to the main sample
  • Sub-samples only - assertion only applies to the sub-samples
  • Main sample and sub-samples - assertion applies to both.
  • JMeter Variable - assertion is to be applied to the contents of the named variable
Yes
Response Field to TestInstructs JMeter which field of the Response to test.
  • Text Response - the response text from the server, i.e. the body, excluding any HTTP headers.
  • Document (text) - the extract text from various type of documents via Apache Tika (see View Results Tree Document view section).
  • URL sampled
  • Response Code - e.g. 200
  • Response Message - e.g. OK
  • Response Headers, including Set-Cookie headers (if any)
Yes
Ignore statusInstructs JMeter to set the status to success initially.

The overall success of the sample is determined by combining the result of the assertion with the existing Response status. When the Ignore Status checkbox is selected, the Response status is forced to successful before evaluating the Assertion.

HTTP Responses with statuses in the 4xx and 5xx ranges are normally regarded as unsuccessful. The "Ignore status" checkbox can be used to set the status successful before performing further checks. Note that this will have the effect of clearing any previous assertion failures, so make sure that this is only set on the first assertion.
Yes
Pattern Matching RulesIndicates how the text being tested is checked against the pattern.
  • Contains - true if the text contains the regular expression pattern
  • Matches - true if the whole text matches the regular expression pattern
  • Equals - true if the whole text equals the pattern string (case-sensitive)
  • Substring - true if the text contains the pattern string (case-sensitive)
Equals and Substring patterns are plain strings, not regular expressions. NOT may also be selected to invert the result of the check.
Yes
Patterns to TestA list of patterns to be tested. Each pattern is tested separately. If a pattern fails, then further patterns are not checked. There is no difference between setting up one Assertion with multiple patterns and setting up multiple Assertions with one pattern each (assuming the other options are the same). However, when the Ignore Status checkbox is selected, this has the effect of cancelling any previous assertion failures - so make sure that the Ignore Status checkbox is only used on the first Assertion.Yes

 

The pattern is a Perl5-style regular expression, but without the enclosing brackets.

Assertion Examples

 


Figure 14 - Test Plan

 

 


Figure 15 - Assertion Control Panel with Pattern

 

 


Figure 16 - Assertion Listener Results (Pass)

 

 


Figure 17 - Assertion Listener Results (Fail)

 

18.5.2 Duration Assertion

The Duration Assertion tests that each response was received within a given amount of time. Any response that takes longer than the given number of milliseconds (specified by the user) is marked as a failed response.

Control Panel

Parameters

AttributeDescriptionRequired
NameDescriptive name for this element that is shown in the tree.No
Duration in MillisecondsThe maximum number of milliseconds each response is allowed before being marked as failed.Yes

 


posted on 2014-03-07 11:35 顺其自然EVO 阅读(3361) 评论(0)  编辑  收藏 所属分类: jmeter


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜