【问题标题】:Call properties in Groovy-Script with JavaCode using SoapUI使用 SoapUI 使用 JavaCode 在 Groovy-Script 中调用属性
【发布时间】:2015-01-17 05:11:49
【问题描述】:

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

  1. Teststep1(http 请求):调用 URL,生成令牌
  2. Teststep2(groovy 脚本):解析响应并将令牌保存为属性
  3. Teststep3(http 请求):调用另一个 URL
  4. Teststep4(groovy 脚本):解析repsonseHttpHeader,将statusHeader保存在testCase-property中并检查它是'200'、'400'、'403'...
  5. Teststep5(groovy 脚本):只要不是“200”就写一封电子邮件

第 1 步到第 4 步正常工作,并且通过执行我的脚本发送电子邮件(第 5 步)也正常工作,但我想根据 statusHeader 更改电子邮件内容。例如:

  • 404: The 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 方法调用具有不同消息的不同电子邮件脚本,但更改电子邮件的内容会更好。

提前致谢!

【问题讨论】:

    标签: java groovy soapui


    【解决方案1】:

    您必须在最后一个 groovy 脚本中更改您的类定义,而不是 main 定义一个方法来发送在您的 sendMailTLS 类中具有 statusCode 作为参数的电子邮件。然后在定义类的同一个 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);
    

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

    希望这会有所帮助,

    【讨论】:

    • 这正是我想要的。对我来说,不清楚如何调用 java 类以及如何使用 statusCode 参数创建 this 的实例。非常感谢,周末愉快! :)
    【解决方案2】:

    你可以这样做:

    1. 在您的 java 类中添加一个映射,其中包含所有预期的 您想要的响应代码和响应消息 电子邮件的主题或内容。
    2. 我建议您使用除 main 之外的其他方法,以便 你初始化类对象并从soapui的groovy调用方法 脚本,当然你也可以用 main 来做。
    3. 方法应将响应代码作为参数。
    4. 用它作为key从map中获取相关值并放到 电子邮件。
    5. 为你的类创建一个 jar 文件并将 jar 文件放在 $SOAPUI_HOME/bin/ext 目录
    6. 在您的 soapui 测试用例中,对于测试 step5 (groovy),调用您的方法 来自您编写的课程,就像您在 java 中的调用方式一样。例如:如何 从下面给出的soapui groovy调用你的java
    //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}')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多