如何在SoapUI中将全局类定义为groovy脚本?

Gab*_*Gab 5 groovy soapui

我想在一个groovy脚本中定义一个类,我可以通过SoapUI中的其他groovy脚本重用它.

我想尝试在TestSuite属性中定义我的类,但它不起作用.我想避免在JAR中定义类,因为我们在团队中工作,每个人都必须在他们的SoapUI中导入JAR才能运行我的测试.我使用SoapUI 3.6.1

以下是我的TestSuite的制作方法:

TestSuite
  TestCase
    TestSteps
       Init         (Groovy Script)
       GetResponse1 (Test Request)
       Test1        (Groovy Script)
       GetResponse2 (Test Request)
       Test2        (Groovy Script)
Run Code Online (Sandbox Code Playgroud)

为了简化测试,我在'Test1'中定义了一个类,我想在'Test2'中重用这个类.理想情况下,我会在'Init'中定义该类,并且任何其他groovy脚本都可以访问它.

我怎样才能实现这一目标?

Ste*_*han 7

基于 @Abhey Rathore的链接,这里是如何在SoapUI的groovy脚本中定义一个全局类:

第1步:声明全局类

  • 创建一个名为的项目 MyRepository
  • 在这个项目下创建一个被禁用的 TestSuitelib
  • 在此测试套件下创建一个名为TestCase MyClass
  • 在这个测试用例下,创建一个名为的groovy脚本 class

您的项目树应如下图所示:
用于在SoapUI中声明全局类的项目树

第2步:编写全局类代码

在调用的groovy脚本中class,复制并粘贴下面的代码:

class MyClass {
     // The three following fields are MANDATORY
     def log
     def context
     def testRunner

     // The constructor below is MANDATORY
     def MyClass(logIn, contextIn, testRunnerIn) {
        this.log = logIn
        this.context = contextIn
        this.testRunner = testRunnerIn
     }

     def myMethod() {
        this.log.info 'Hello World!'
     }
}

// Other scripts elsewhere in SoapUI will access the reused class through their context
context.setProperty( "MyClass", new MyClass(log, context, testRunner) )
Run Code Online (Sandbox Code Playgroud)

第3步:导入全局类

在任何项目中,您都可以使用以下代码段导入全局类:

// Import the class
def currentProject = testRunner.testCase.testSuite.project
currentProject
    .testSuites["lib"]
       .testCases["MyClass"]
          .testSteps["class"]
             .run(testRunner, context)

// Now use it
context.MyClass.myMethod()
Run Code Online (Sandbox Code Playgroud)

SoapUI 5.2.1


小智 5

试试这个,我认为可以帮助你重用代码.

http://forum.soapui.org/viewtopic.php?t=15744


chr*_*ead 1

我非常确定您必须创建一个 JAR 文件并将其放入 \bin\ext 中。

SoapUI 将在重新启动时自动选择它(您应该在启动内容中看到它)。

基本上,您只需创建一个 Java 或 Groovy 项目,将其导出(使用 Eclipse),它就会工作。SoapUI 可能会覆盖您的依赖项,但如果没有,您也可以添加这些 JAR(比创建可运行的 JAR 更安全,因为 SoapUI 可能使用您所使用的不同版本)。

如果您需要帮助,请发布相关问题。