Hello Community!
Today we have another task for you to help contribute to our collection of useful ReadyAPI content.
Here is the task: Create a Groovy script that will send an email when an assertion fails
Difficulty:
The idea of the script is to run a send email test step when the assertion fails. Please note that the email should be sent if any assertion for any test step in a project fails.
To complete the task you should write a script for one of the events.
Good luck😊
Solved! Go to Solution.
Task: Create a Groovy script that will send an email when an assertion fails
This is a solution created for [TechCorner Challenge #6]
I thought I posted a reply but it disappeared.
// set project reference
def project = context.project;
// set testTypes. Can expand for more test step types that allow assertions.
def testTypes = [com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep, com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep];
// start an error list.
def errors = [];
// Walk through the test suites, test cases, and test steps.
for (ts in project.getTestSuiteList())
{
for (tc in ts.getTestCaseList())
{
for (testType in testTypes)
{
for (step in tc.getTestStepsOfType(testType))
{
for (assertion in step.getAssertionList())
{
if (assertion.errors != null)
{
def errorMsg = ts.getName() + " - " + tc.getName() + " - " + step.getName();
errorMsg += " has failed with the error: " + assertion.errors.toString();
errors.add(errorMsg);
}
}
}
}
}
}
if (errors != [])
{
// def sendMail = context.project.testSuites["TestSuiteWithSendMail"].testCases["TestCaseWithSendMail"].testSteps["Send Mail"];
def body = "";
for (error in errors)
{
body+= error + "\n";
}
def sendMail = context.testCase.testSteps["Send Mail"];
sendMail.setMessage(body);
sendMail.setSubject("Project run Errors");
sendMail.run(context.testRunner, context);
}
Hi @nmrao,
Yes, I agree with you that this task isn't so realistic 😉
But, maybe, this is a regression test for some basic functionality that always should pass.
Or, we can make a task more accurate - send an email only if an assertion for the "Very Important" Test Step fails.
Thank you for the comment Rao!
Who else would like to participate?🙂
@msiadak @richie @krogold @HimanshuTayal @Radford
Task: Create a Groovy script that will send an email when an assertion fails
This is a solution created for [TechCorner Challenge #6]
I thought I posted a reply but it disappeared.
// set project reference
def project = context.project;
// set testTypes. Can expand for more test step types that allow assertions.
def testTypes = [com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep, com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep];
// start an error list.
def errors = [];
// Walk through the test suites, test cases, and test steps.
for (ts in project.getTestSuiteList())
{
for (tc in ts.getTestCaseList())
{
for (testType in testTypes)
{
for (step in tc.getTestStepsOfType(testType))
{
for (assertion in step.getAssertionList())
{
if (assertion.errors != null)
{
def errorMsg = ts.getName() + " - " + tc.getName() + " - " + step.getName();
errorMsg += " has failed with the error: " + assertion.errors.toString();
errors.add(errorMsg);
}
}
}
}
}
}
if (errors != [])
{
// def sendMail = context.project.testSuites["TestSuiteWithSendMail"].testCases["TestCaseWithSendMail"].testSteps["Send Mail"];
def body = "";
for (error in errors)
{
body+= error + "\n";
}
def sendMail = context.testCase.testSteps["Send Mail"];
sendMail.setMessage(body);
sendMail.setSubject("Project run Errors");
sendMail.run(context.testRunner, context);
}
Hey @sonya_m
I wont be trying this - this is way beyond my coding skills - I wouldn't know where to start - its just too different to what I've tried before - (however, I'm more than happy to steal @msiadak's code for future reference) - but I cant wait for next week's task!
nice one,
rich
@msiadak great solution!
As for the disappeared comment - the spam filter got you. This was not normal behavior and I made sure that never happens to you again🙂
@richie Glad to hear this! I am about to post a new task🙂
User | Count |
---|---|
7 | |
6 | |
6 | |
2 | |
1 |
Subject | Author | Latest Post |
---|---|---|