我已经开始使用SoapUI 5(非专业版)构建服务监视器。服务监视器应该能够:
步骤1到4工作没有任何问题,并且通过执行我的脚本发送电子邮件(步骤5)正在运行,但我想更改电子邮件内容和statusHeader。例如:
404
:The requested resource could not be found
403
:It is forbidden. Please check the token generator
解析和保存httpHeaderStatusCode的代码:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
// get responseHeaders of ServiceTestRequest
def httpResponseHeaders = context.testCase.testSteps["FeatureServiceTestRequest"].testRequest.response.responseHeaders
// get httpResonseHeader and status-value
def httpStatus = httpResponseHeaders["#status#"]
// extract value
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
// log httpStatusCode
log.info("HTTP status code: " + httpStatusCode)
// Save logged token-key to next test step or gloabally to testcase
testRunner.testCase.setPropertyValue("httpStatusCode", httpStatusCode)
发送电子邮件的代码:
// From http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "yourUsername@gmail.com";
final String password = "yourPassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yourSendFromAddress@domain.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yourRecipientsAddress@domain.com"));
message.setSubject("Status alert");
message.setText("Hey there,"
+ "\n\n There is something wrong with the service. The httpStatus from the last call was: ");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我想做什么:我想在我上一个groovy脚本中发送保存在我的第一个groovy脚本(最后一行)上的testCase httpStatusCode
属性,我发送电子邮件。有什么东西可以处理吗?
我已经搜索了两个小时,但我找不到有用的东西。可能的解决方法是我必须使用if语句和testRunner.runTestStepByName
方法使用不同的消息调用不同的电子邮件脚本,但更改电子邮件的内容会更好。
提前致谢!
答案 0 :(得分:1)
以下是您可以做的事情:
//use package, and imports also if associated
SendMailTLS mail = new SendMailTLS()
//assuming that you move the code from main method to sendEmail method
mail.sendEmail(context.expand('${#TestCase#httpStatusCode}')
答案 1 :(得分:1)
您必须在上一个groovy脚本中更改类定义,而不是main
定义一种方法,以便在statusCode
类中发送包含sendMailTLS
参数的电子邮件。然后在定义类的同一个groovy脚本中使用def statusCode = context.expand('${#TestCase#httpStatusCode}');
获取属性值,然后创建类的实例并调用您的方法将属性statusCode
传递给:
// create new instance of your class
def mailSender = new SendMailTLS();
// send the mail passing the status code
mailSender.sendMail(statusCode);
你的groovy脚本中的所有内容都必须如下:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// define the class
class SendMailTLS {
// define a method which recive your status code
// instead of define main method
public void sendMail(String statusCode) {
final String username = "yourUsername@gmail.com";
final String password = "yourPassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yourSendFromAddress@domain.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yourRecipientsAddress@domain.com"));
message.setSubject("Status alert");
// THERE YOU CAN USE STATUSCODE FOR EXAMPLE...
if(statusCode.equals("403")){
message.setText("Hey there,"
+ "\n\n You recive an 403...");
}else{
message.setText("Hey there,"
+ "\n\n There is something wrong with the service. The httpStatus from the last call was: ");
}
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
// get status code
def statusCode = context.expand('${#TestCase#httpStatusCode}');
// create new instance of your class
def mailSender = new SendMailTLS();
// send the mail passing the status code
mailSender.sendMail(statusCode);
我测试了这段代码,它正常工作:)
。
希望这有帮助,