我已经开始使用 SoapUI 5(非专业版)构建服务监视器。服务监视器应该能够:
- 测试步骤1(http 请求):调用 URL,生成 token
- Teststep2(groovy 脚本):解析响应并将 token 保存为属性
- Teststep3(http 请求):调用另一个 URL
- Teststep4(groovy 脚本):解析 repsonseHttpHeader,将 statusHeader 保存在 testCase-property 中并检查它是否为“200”、“400”、“403”...
- Teststep5(groovy 脚本):只要不是“200”,就写一封电子邮件
步骤 1 到 4 工作正常,没有任何问题,并且通过执行我的脚本发送电子邮件(步骤 5)也工作正常,但我想根据状态标题更改电子邮件内容。例如:
404
:找不到请求的资源
403
:这是禁止的。请检查 token 生成器
- ...
解析并保存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 方法调用具有不同消息的不同电子邮件脚本,但更改电子邮件的内容会更好。
提前致谢!
最佳答案
您必须更改最后一个 groovy 脚本中的类定义,而不是定义一个方法来发送电子邮件,该方法将 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);
您的常规脚本中的所有内容必须看起来像:
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);
我测试了这段代码,它工作正常:)
。
希望这有帮助,
关于java - 使用 SoapUI 通过 JavaCode 调用 Groovy-Script 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27944357/