断言为真时退出循环 [英] Exit a loop when Assertion is true

查看:172
本文介绍了断言为真时退出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MailReaderSampler,它从给定的电子邮件ID中获取所有消息.所有电子邮件中都有一个验证令牌.目前,令牌是在邮件的主题上传递的.

I'm using a MailReaderSampler which gets all the messages from given email ID. All the emails have a verification token in it. For now, the token is being passed on the subject of the mail.

因此响应数据将具有:

Date:-- To: -- From: -- Subject: token : someToken

Date:-- To: -- From: -- Subject: token : someToken

我已经在User Defined Variables中定义了令牌值,并且正在使用Response assertion检查邮件中是否包含令牌.

I have defined a Token value in User Defined Variables and I'm using Response assertion to check if the mail contains the Token.

因此它检查所有子结果(所有电子邮件).现在,每当一封电子邮件包含令牌时,我要停止测试,并且断言条件不应检入其他电子邮件,因为它已经找到了具有令牌值的电子邮件.

So it checks for all the sub-results(all emails). Now whenever an email contains the token, then I want to stop the test and the assertion condition should not check in other emails as it already found the email which has token value.

有关如何操作的任何建议?

Any suggestions on how to do it?

推荐答案

这应该可以解决问题,但是我还没有尝试过...在您的邮件采样器后面添加BeanShell采样器,并使用以下代码.

This should do the trick, but I haven't tried it yet... Add BeanShell sampler after your mail sampler with following code.

import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContextService;

SampleResult[] subResults = JMeterContextService.getContext().getPreviousResult().getSubResults();
for(SampleResult sr : subResults) {
    if (sr.getResponseDataAsString().contains(vars.get("Token").toString()))
        JMeterContextService.getContext().getEngine().stopTest();
}

这篇关于断言为真时退出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆