如何在响应断言失败时将 JMeter 变量写入文件
How to write JMeter variables to file on Response Assertion failure
我在 HTTP 请求下有一个响应断言,它检查用户登录时是否显示“访问不足”。这是一个简单的文本断言。
仅在失败时,我想将用户凭据写入文件。我正在尝试通过 JS223 PostProcessor 执行此操作,到目前为止,我只能找到有关如何使用它写入文件的信息,但找不到有关如何首先检查响应断言的任何信息
FileWriter fstream = new FileWriter("C:\Loadtest\log.txt",true); //Create New file with name "subid"
BufferedWriter out = new BufferedWriter(fstream);
// Need logic to check if response has failed and then do below steps
if (assertionhasfailed)
{
out.write(vars.get("account"));
out.write(System.getProperty("line.separator"));//insert new line
}
out.close();
fstream.close();
编辑
我通过使用 JS223 断言解决了这个问题:
Boolean result = prev.getResponseDataAsString().contains("Insufficient privileges")
FileWriter fstream = new FileWriter("C:\Loadtest\log.txt",true); //Create New file with name "subid"
BufferedWriter out = new BufferedWriter(fstream);
if (result)
{
out.write(vars.get("account"));//write value of variable 1
out.write(System.getProperty("line.separator"));//insert new line
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("User has insufficient privileges");
}
out.close();
fstream.close();
现在的问题是这是否是最好的方法?我问是因为我注意到 运行 测试在处理断言时有明显的延迟。这可能会扭曲性能指标。
在惯用的 Groovy 中,您的代码如下所示:
new File("C:\Loadtest\log.txt").withWriter{ out ->
boolean result = prev.responseDataAsString.contains 'Insufficient privileges'
if (result) {
out.println vars.account
AssertionResult.failure = true
AssertionResult.failureMessage = 'User has insufficient privileges'
}
}
根据 JMeter 测试元素 execution order:
- Configuration elements
- Pre-Processors
- Timers
- Sampler
- Post-Processors (unless SampleResult is null)
- Assertions (unless SampleResult is null)
- Listeners (unless SampleResult is null)
Post-处理器在断言之前执行,因此您无权访问采样器的实际状态。
您需要切换到JSR223 Listener,这样您就可以检查采样器是否成功,如下所示:
if (!prev.isSuccessful()) {
//your code here
}
其中 prev
代表 previous SampleResult
更多信息:Top 8 JMeter Java Classes You Should Be Using with Groovy
我在 HTTP 请求下有一个响应断言,它检查用户登录时是否显示“访问不足”。这是一个简单的文本断言。
仅在失败时,我想将用户凭据写入文件。我正在尝试通过 JS223 PostProcessor 执行此操作,到目前为止,我只能找到有关如何使用它写入文件的信息,但找不到有关如何首先检查响应断言的任何信息
FileWriter fstream = new FileWriter("C:\Loadtest\log.txt",true); //Create New file with name "subid"
BufferedWriter out = new BufferedWriter(fstream);
// Need logic to check if response has failed and then do below steps
if (assertionhasfailed)
{
out.write(vars.get("account"));
out.write(System.getProperty("line.separator"));//insert new line
}
out.close();
fstream.close();
编辑
我通过使用 JS223 断言解决了这个问题:
Boolean result = prev.getResponseDataAsString().contains("Insufficient privileges")
FileWriter fstream = new FileWriter("C:\Loadtest\log.txt",true); //Create New file with name "subid"
BufferedWriter out = new BufferedWriter(fstream);
if (result)
{
out.write(vars.get("account"));//write value of variable 1
out.write(System.getProperty("line.separator"));//insert new line
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("User has insufficient privileges");
}
out.close();
fstream.close();
现在的问题是这是否是最好的方法?我问是因为我注意到 运行 测试在处理断言时有明显的延迟。这可能会扭曲性能指标。
在惯用的 Groovy 中,您的代码如下所示:
new File("C:\Loadtest\log.txt").withWriter{ out ->
boolean result = prev.responseDataAsString.contains 'Insufficient privileges'
if (result) {
out.println vars.account
AssertionResult.failure = true
AssertionResult.failureMessage = 'User has insufficient privileges'
}
}
根据 JMeter 测试元素 execution order:
- Configuration elements
- Pre-Processors
- Timers
- Sampler
- Post-Processors (unless SampleResult is null)
- Assertions (unless SampleResult is null)
- Listeners (unless SampleResult is null)
Post-处理器在断言之前执行,因此您无权访问采样器的实际状态。
您需要切换到JSR223 Listener,这样您就可以检查采样器是否成功,如下所示:
if (!prev.isSuccessful()) {
//your code here
}
其中 prev
代表 previous SampleResult
更多信息:Top 8 JMeter Java Classes You Should Be Using with Groovy