【发布时间】:2015-01-17 05:11:49
【问题描述】:
我已经开始使用 SoapUI 5(非专业版)构建服务监视器。服务监视器应该能够:
- Teststep1(http 请求):调用 URL,生成令牌
- Teststep2(groovy 脚本):解析响应并将令牌保存为属性
- Teststep3(http 请求):调用另一个 URL
- Teststep4(groovy 脚本):解析repsonseHttpHeader,将statusHeader保存在testCase-property中并检查它是'200'、'400'、'403'...
- Teststep5(groovy 脚本):只要不是“200”就写一封电子邮件
第 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 方法调用具有不同消息的不同电子邮件脚本,但更改电子邮件的内容会更好。
提前致谢!
【问题讨论】: