使用SoapUI使用JavaCode在Groovy脚本中调用属性

施洛姆

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

  1. Teststep1(http请求):调用一个生成令牌的URL
  2. Teststep2(常规脚本):解析响应并将令牌保存为属性
  3. Teststep3(http请求):调用另一个URL
  4. Teststep4(常规脚本):解析repsonseHttpHeader,将statusHeader保存在testCase属性中,并检查其是否为'200','400','403'...
  5. Teststep5(常规脚本):每当电子邮件不是“ 200”时,都要写一封电子邮件

步骤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 = "[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方法用不同的消息调用不同的电子邮件脚本,但是更改电子邮件的内容会更好。

提前致谢!

Albciff

您必须在上一个常规脚本中更改类定义,而不是main定义一种方法来发送statusCodesendMailTLS类中作为参数的电子邮件然后在同一个普通脚本中,您在其中定义类时使用它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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SOAPUI:使用groovy脚本的timeStamp

使用groovy脚本在SOAPUI请求中设置参数的正确方法

如何使用Groovy在SoapUI中的属性值中添加或减去

使用groovy脚本从SoapUI中的测试用例捕获脚本日志

如何在soapUi中使用Groovy脚本循环多次

在SoapUI中使用Groovy脚本从xlsx文件读取值

使用groovy脚本更新soapui xml标记值

在SoapUI中的Groovy脚本之间传输参数

在SOAPUI脚本断言中的groovy脚本中使用环境变量

如何使用 OnRequest Mock Script (groovy) 在 SoapUI 中设置RequestContent

SoapUI使用Groovy在延迟步骤中更改毫秒值

使用Groovy在SoapUI中使用Xpath查询在XML节点中使用xsi获取属性值

在SoapUI中使用Groovy脚本将当前时间(IST)转换为UTC

使用SoapUI-Groovy脚本从JSON响应中提取子节点

使用groovy脚本soapui逐行比较失败的测试步骤对测试步骤“断言包含”的响应

在soapui免费版中使用groovy脚本从excel进行数据驱动的测试

从脚本中调用groovy方法

通过使用groovy脚本,不会在SOAP UI中更新本地属性值

在SoapUI Pro中使用groovy压缩目录

如何在SOAPUI中对HTTP参数使用全局属性?

在 nifi 如何使用 groovy 脚本调用休息服务

在SoapUI中的groovy脚本中加载外部jar

在SoapUI中的SOAP请求之后执行Groovy脚本

从Soapui Groovy脚本中的XML获取文本值

soapUI groovy脚本groovy.lang.MissingMethodException

使用Groovy脚本在SoapUI中生成动态JSON请求主体并将其传递给POST API请求

`null` 使用 while (soapui groovyscript) 在 groovy 计数器中返回

使用 groovy 从 SOAPUI 中的 JSON 内容解码 base64 字符串

如何从soapui groovy脚本执行shell脚本?