Forum Discussion
Hi All.
Thanks for comments/help on this. Thanks to people in other posts too for part of the email code.
Here is my full solution that I hope will help others too :-)
This will loop through each Test Suite in a project getting the Test Suite name and its status, then send a summary email.
Put the code below in your PROJECT Teardown Script.
Also, if/when you Launch TestRunner, select to output the report to a root folder (which will be included in the email).This code could be improved by taking the input parameters and setting the report output location dynamically. Each environment that you have set up will output to a different location (You may need to create the .../EnvironmentName/ProjectName/ directories in the root location)
I set the To Email Address(es) as a Project Property but have hardcoded in the code below.
I set up a scheduled job which calls a .bat file to Launch the Test Runner so that this runs test checking our environments regularly. It only emails when it detects failures but you can change this easily. I intend to move this to using QAComplete and the Scheduler.
I am using ReadyAPI with SoapUI Pro.
Cheers,
Paul
-------------------------------------------------------------------------------------------------------
import javax.mail.internet.*;
import javax.mail.*
import javax.activation.*
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.model.support.PropertiesMap;
import com.eviware.soapui.model.testsuite.TestCase;
import com.eviware.soapui.model.testsuite.TestRunner;
import com.eviware.soapui.model.testsuite.TestRunner.Status;
import com.eviware.soapui.model.testsuite.TestSuite;
def testCaseName = ""
def testSuiteNames = ""
def failedCount = 0
def results = "" //project details and failed results
def passedResults = ""
def message = ""
def testSuiteRunner = context["#TestSuiteRunner#"]
results += "ReadyAPI Project : <b>" + project.name + "</b></br>"
results += "Environment : <b>" + project.activeEnvironment.name + "</b></br>"
results += "Project File : " + project.path + "</br>"
results += "\n<a href=C:\\Your\\Report\\Output\Root\\Folder\\Results\\" + project.activeEnvironment.name + "\\" + project.name + "\\index.html" + ">See the Latest report here</a></br></br>";
results += "<b>Failed Test Suites" + "</b>" + "</br>"
passedResults = "<b>Completed Test Suites</b>" + "</br>"
if( testSuiteRunner != null ){
for(r in testSuiteRunner.results )
testSuiteNames = testSuiteRunner.results.testSuite.name
for (int i = 0; i < testSuiteNames.size(); i++) {
String testSuiteStatus = testSuiteRunner.results.get(i).status
if (testSuiteStatus.equals("FAILED")){
testCaseName = (testSuiteNames.get(i)) + " <b style=color:red>" + testSuiteStatus + "</b></br>";
failedCount ++;
results += testCaseName;
} else {
testCaseName = (testSuiteNames.get(i)) + " <b style=color:green>" + testSuiteStatus + "</b></br>";
passedResults += testCaseName;
}
}
}
if (failedCount > 0){
subject = project.activeEnvironment.name + " Environment : " + project.name + " Testing Failed"
toAddress = project.getPropertyValue( "SendEmailFailuresTo" )
fromAddress = "Automated_Tests@Yoursmtphost.com"
host = "Yoursmtphost.com"
port = "25" //You may use a different port and possibly username/pwd
Properties mprops = new Properties();
mprops.setProperty("mail.transport.protocol","smtp");
mprops.setProperty("mail.host",host);
mprops.setProperty("mail.smtp.port",port);
message += results + "</br>";
message += passedResults
Session lSession = Session.getDefaultInstance(mprops,null);
MimeMessage msg = new MimeMessage(lSession);
//tokenize out the recipients in case they came in as a list
StringTokenizer tok = new StringTokenizer(toAddress,";");
ArrayList emailTos = new ArrayList();
while(tok.hasMoreElements()){
emailTos.add(new InternetAddress(tok.nextElement().toString()));
}
InternetAddress[] to = new InternetAddress[emailTos.size()];
to = (InternetAddress[]) emailTos.toArray(to);
msg.setRecipients(MimeMessage.RecipientType.TO,to);
InternetAddress fromAddr = new InternetAddress(fromAddress);
msg.setFrom(fromAddr);
msg.setFrom(new InternetAddress(fromAddress));
msg.setSubject(subject);
msg.setText(message, "UTF-8", "html");
Transport transporter = lSession.getTransport("smtp");
transporter.connect();
transporter.send(msg);
}
Related Content
- 5 years agounderqualified
- 5 years ago678