我已经开始使用SoapUI 5(非Pro版)构建服务监视器。服务监视器应该能够:
步骤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 = "[email protected]";
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("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
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);
}
}
}
我想做什么:我想访问httpStatusCode
保存在我发送电子邮件的最后一个groovy脚本中的第一个groovy脚本(最后一行)上的testCase属性。有什么可以解决的吗?
我搜索了两个小时,但没有找到有用的东西。可能的解决方法是,我必须使用if语句和testRunner.runTestStepByName
方法用不同的消息调用不同的电子邮件脚本,但是更改电子邮件的内容会更好。
提前致谢!
您必须在上一个常规脚本中更改类定义,而不是main
定义一种方法来发送statusCode
在sendMailTLS
类中作为参数的电子邮件。然后在同一个普通脚本中,您在其中定义类时使用它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);
常规脚本中的所有内容都必须看起来像:
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 = "[email protected]";
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("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
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);
我测试了这段代码,它可以正常工作:)
。
希望这可以帮助,
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句