jmeter之断言

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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree. The name is stored in the script variable Label

No

Reset bsh.Interpreter before each call

If 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

Parameters

Parameters 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 file

A file containing the BeanShell script to run. This overrides the script. The file name is stored in the script variable FileName

No

Script

The 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Language

The BSF language to be used

Yes

Parameters

Parameters 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 file

A 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 property

No

Script

The 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Compare Content

Whether or not to compare the content (response data)

Yes

Compare Time

If 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 Filters

Filters 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

doctype

omit/auto/strict/loose

Yes

Format

HTML, XHTML or XML

Yes

Errors only

Only take note of errors?

Yes

Error threshold

Number of errors allowed before classing the response as failed

Yes

Warning threshold

Number of warnings allowed before classing the response as failed

Yes

Filename

Name of file to which report is written

No

 

18.5.6 MD5Hex Assertion

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

Control Panel

Parameters

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

MD5 sum

32 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

Attribute

Description

Required

Name

Descriptive 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 bytes

The number of bytes to use in testing the size of the response (or value of the JMeter variable).

Yes

Type of Comparison

Whether 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Verify Signature

If selected, the asertion will verify if it is a valid signature according to the parameters defined in the Signer Certificate box.

Yes

Message not signed

Whether or not to expect a signature in the message

Yes

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 Position

The 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

File Name

Specify XML Schema File Name

Yes

 

 

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 

Attribute

Description

Required

Name

Descriptive 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 to match anywhere in the response
  • /title[text()='Text to match'] - matches Text to match at root level in the response

Control Panel

Parameters

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Use Tidy (tolerant parser)

Use Tidy, i.e. be tolerant of XML/HTML errors

Yes

Quiet

Sets the Tidy Quiet flag

If Tidy is selected

Report Errors

If a Tidy error occurs, then set the Assertion accordingly

If Tidy is selected

Show warnings

Sets the Tidy showWarnings option

If Tidy is selected

Use Namespaces

Should namespaces be honoured?

If Tidy is not selected

Validate XML

Check the document against its schema.

If Tidy is not selected

Ignore Whitespace

Ignore Element Whitespace.

If Tidy is not selected

Fetch External DTDs

If selected, external DTDs are fetched.

If Tidy is not selected

XPath Assertion

XPath to match in the document.

Yes

True if nothing matches

True if a XPath expression is not matched

No

 

 

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

Attribute

Description

Required

Name

Descriptive 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 Test

Instructs 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 status

Instructs 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 Rules

Indicates 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 Test

A 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

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Duration in Milliseconds

The maximum number of milliseconds each response is allowed before being marked as failed.

Yes

 

 

源文档

 

你可能感兴趣的:(Jmeter)