Exit a loop when Assertion is true

孤者浪人 提交于 2019-12-10 17:27:51

问题


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:


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();
}


来源:https://stackoverflow.com/questions/38109597/exit-a-loop-when-assertion-is-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!