使用SoapUI使用JavaCode调用Groovy-Script中的属性

时间:2015-01-14 13:41:38

标签: java groovy soapui

我已经开始使用SoapUI 5(非专业版)构建服务监视器。服务监视器应该能够:

  1. Teststep1(http请求):调用生成令牌的URL
  2. Teststep2(groovy script):解析响应并将令牌保存为属性
  3. Teststep3(http请求):调用其他网址
  4. Teststep4(groovy脚本):解析repsonseHttpHeader,将statusHeader保存在testCase-property中,检查它是否为' 200' 400' 403' 403&# 39; ...
  5. Teststep5(groovy script):只要不是' 200'
  6. 就写一封电子邮件

    步骤1到4工作没有任何问题,并且通过执行我的脚本发送电子邮件(步骤5)正在运行,但我想更改电子邮件内容和statusHeader。例如:

    • 404The requested resource could not be found
    • 403It 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方法使用不同的消息调用不同的电子邮件脚本,但更改电子邮件的内容会更好。

    提前致谢!

2 个答案:

答案 0 :(得分:1)

以下是您可以做的事情:

  1. 在java类中添加一个包含所有预期内容的地图 您想要的响应代码和响应消息 电子邮件的主题或内容。
  2. 我建议你把它放在主要以外的方法中 你从soapui的groovy中实现了类对象和调用方法 脚本,当然你也用main做。
  3. 方法应将响应代码作为参数。
  4. 使用它作为键从地图中获取相关值并将其放入 电子邮件。
  5. 为您的类创建一个jar文件,并将jar文件放在其下 $ SOAPUI_HOME / bin / ext目录
  6. 在您的soapui测试用例中,对于测试步骤5(groovy),请调用您的方法 从您编写的类中,就像您在java中调用的一样。例如:如何 从下面给出的soapui groovy中调用你的java
  7. //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);

我测试了这段代码,它正常工作:)

希望这有帮助,