Exit a loop when Assertion is true

2019-06-27 18:57发布

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.

So the Response Data will have:

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

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?

1条回答
Explosion°爆炸
2楼-- · 2019-06-27 19:29

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();
}
查看更多
登录 后发表回答