简体   繁体   English

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

[英]How to define a global class in SoapUI as a groovy script?

I want to define a class in a groovy script that i could reuse trough other groovy script inside SoapUI. 我想在一个groovy脚本中定义一个类,我可以通过SoapUI中的其他groovy脚本重用它。

I alredy tried to define my class inside a TestSuite property but it doesn't work. 我想尝试在TestSuite属性中定义我的类,但它不起作用。 I would like to avoid defining the class in a JAR because we work in team and every one would have to import the JAR in their SoapUI in order to run my tests. 我想避免在JAR中定义类,因为我们在团队中工作,每个人都必须在他们的SoapUI中导入JAR才能运行我的测试。 I use SoapUI 3.6.1 我使用SoapUI 3.6.1

Here's how my TestSuite is made : 以下是我的TestSuite的制作方法:

TestSuite
  TestCase
    TestSteps
       Init         (Groovy Script)
       GetResponse1 (Test Request)
       Test1        (Groovy Script)
       GetResponse2 (Test Request)
       Test2        (Groovy Script)

To simplify me tests, i defined a class in 'Test1' and i would like to reuse this class in 'Test2'. 为了简化测试,我在'Test1'中定义了一个类,我想在'Test2'中重用这个类。 So ideally i would define that class in 'Init' and it would be accessible to any other groovy script. 理想情况下,我会在'Init'中定义该类,并且任何其他groovy脚本都可以访问它。

How can i achieve that? 我怎样才能实现这一目标?

Based on @Abhey Rathore 's link, here is how to define a global class in SoapUI's groovy script: 基于@Abhey Rathore的链接,这里是如何在SoapUI的groovy脚本中定义一个全局类:

Step 1: Declare the global class 第1步:声明全局类

  • Create a project called MyRepository 创建一个名为MyRepository的项目
  • Under this project create a disabled TestSuite called lib 在此项目下,创建一个名为lib禁用 TestSuite
  • Under this test suite create a TestCase called MyClass 在此测试套件下,创建一个名为MyClass的TestCase
  • Under this test case create a groovy script called class 在这个测试用例下,创建一个名为class的groovy脚本

Your project tree should look like the image below: 您的项目树应如下图所示:
用于在SoapUI中声明全局类的项目树

Step 2: Write the global class code 第2步:编写全局类代码

Inside the groovy script called class , copy and and paste the code below: 在名为class的groovy脚本中,复制并粘贴下面的代码:

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) )

Step 3: Import the global class 第3步:导入全局类

From any project, you can import the global class by using the following snippet: 在任何项目中,您都可以使用以下代码段导入全局类:

// 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()

SoapUI 5.2.1 SoapUI 5.2.1

try this, I think will help you in reusable code. 试试这个,我认为可以帮助你重用代码。

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

I am pretty sure that you will have to create a JAR file and put it in \\bin\\ext. 我很确定你必须创建一个JAR文件并将其放在\\ bin \\ ext中。

SoapUI will automatically pick it up on restart (you should see it mentioned in the startup stuff). SoapUI会在重启时自动获取它(你应该在启动时看到它提到)。

You basically just create a Java or Groovy project, Export it (with Eclipse) and it will work. 你基本上只是创建一个Java或Groovy项目,导出它(使用Eclipse),它将工作。 SoapUI will probably have your dependencies covered, but if not you can add those JARs too (safer than creating a runnable JAR since SoapUI might use different versions of what you use). SoapUI可能会覆盖您的依赖项,但如果没有,您也可以添加这些JAR(比创建可运行的JAR更安全,因为SoapUI可能使用您使用的不同版本)。

If you need help, post related questions. 如果您需要帮助,请发布相关问题。

In SoapUI 5.3.0 you can use properties that can be set and get on the context variable. 在SoapUI 5.3.0中,您可以使用可以设置的属性并获取上下文变量。 That variable is available in script assertion snippet. 该变量在脚本断言片段中可用。

For example: context.setProperty("prop", "value"); 例如: context.setProperty("prop", "value");

And from different test script you can get the value by: context.getProperty("prop"); 从不同的测试脚本中,您可以通过以下方式获取值: context.getProperty("prop");

You can use the property from when you define property as a step of test suite or as a value for field in header. 将属性定义为测试套件的步骤或标题中的字段值时,可以使用该属性。 You can do it by ${=context.getProperty("prop");} 你可以通过${=context.getProperty("prop");}来做到这一点

Write this into your Project's load script, then reload your project or just run it. 将其写入Project的加载脚本,然后重新加载项目或运行它。

com.eviware.soapui.support.ClasspathHacker.addFile( project.context.expand('${projectDir}') )

After it has run, you can now put .groovy files into the same folder as your soapui project xml. 运行后,您现在可以将.groovy文件放入与soapui项目xml相同的文件夹中。 Plus, there is no need to compile anything. 另外,没有必要编译任何东西。

eg in MyClass.groovy: 例如在MyClass.groovy中:

class MyClass {String name}

You can import those classes as normal in all your other scripts: 您可以在所有其他脚本中正常导入这些类:

import MyClass
assert new MyClass(name:"me") instanceof MyClass

The only caveat is that your load script (the one setting the classpath), cannot have those imports, because the script will fail to compile. 唯一需要注意的是,您的加载脚本(设置类路径的脚本)不能具有这些导入,因为脚本将无法编译。 If you want to import something during your project load script, just have your load script 'evaluate' another groovy script (in other words, run it), in which you can use the imports: 如果你想在项目加载脚本中导入一些东西,只需让你的加载脚本'评估'另一个groovy脚本(换句话说,运行它),你可以在其中使用导入:

evaluate (new File (project.context.expand('${projectDir}') + 'myProjectLoadScript.groovy'))

//in myProjectLoadScript.groovy:
import MyClass
/* Do things using MyClass... */

This seems the easiest way to me, so I'm not sure why I couldn't find this answer elsewhere online. 这对我来说似乎是最简单的方法,所以我不确定为什么我在网上其他地方找不到这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM