SoapUI 免费版创建脚本库

Creating script library in SoapUI free version

我是 SoapUI 和 groovy 脚本

的新手

我想创建一个 groovy 脚本的存储库,可以在各种测试步骤中重复使用

我正在使用 SoapUI 免费版,以下是我的 SoapUI 项目的结构

Project
|-TestSuite
| |-TestCase
|   |-TestSteps
|     |-LocalScript (Groovy TestStep to reuse library scripts)
|     |-OtherTestStep (Run TestCase TestStep)
|-ScriptLibrary
  |-TestCase
    |-TestSteps
      |-GroovyScriptStep1 (Contain a class for commonly used functions)
      |-GroovyScriptStep2 (Contain another class for other functions)

这是我能够做到的:

我能够使用 this post 中提到的示例创建一个库。与 post 中的示例类似,我在库的测试步骤(按照上述结构的 GroovyScriptStep1)中的代码只是从外部文件中读取一些值,并用于其他 TestSuite 的测试步骤(上述结构中的 LocalScript 步骤)。

问题是:

现在我想创建一个新的 class 并向其添加一个函数,该函数需要来自 运行ning class 的信息并简单地打印它。这里的区别是一些值是在测试中生成的 运行 并且应该传递给库脚本以便 process/print 等

为了让我的问题更清楚,下面是代码片段

我将在这里使用一个简单的场景

示例objective:希望能够打印所有断言和状态(因为这将用于我想创建库的所有测试用例)

不使用库时相同的代码如下(这可以作为 groovy 脚本步骤)

def obj = context.testCase.getTestStepByName("Request 1");
def assertions = obj.getAssertionList()

//Loop on assertions
assertions.each{
    log.info(it.name +  ' --> ' + it.status)

在 Library TestSuite 的测试用例步骤中编写类似的代码

context.setProperty("Assertions", new Assertions());

class Assertions{
    
    def printAssertion(def someArgumentToGetAssertionlistforTestStepinAnotherTestSuite){
        
        
        def obj = ????
        
        def assertions = obj.getAssertionList()
        
        //Loop on assertions
        assertions.each{
            log.info(it.name +  ' --> ' + it.status)
        }
    }
        
}

我想调用此方法的代码(根据上述项目结构的 LocalScript)

scripts = testRunner.testCase.testSuite.project.testSuites["ScriptLibrary"]; 
scripts.testCases["Scripts"].testSteps["Assertions"].run(testRunner, context);

context.Assertions.printAssertion(ArgumentRequired);

这只是一个例子,我想创建一些更常见的脚本库,这些脚本在本地使用时会使用上下文变量

请帮我解决这个问题,如果还需要 information/clarification,请告诉我

应该这样做

 context.setProperty("Assertions", new Assertions());


class Assertions{

    def printAssertion( tStep){

        def assertions = tStep.getAssertionList()

        //Loop on assertions
        assertions.each{
            log.info(it.name +  ' --> ' + it.status)
        }
    }

}

然后这样称呼它

    TestStep=testRunner.testCase.testSuite.getTestCaseByName("yourTestCase").getTestStepByName("stepName")

    context.Assertions.printAssertion(TestStep)

对于断言:

将此脚本放入存储库

context.setProperty("Assertions", new Assertions());   
    class Assertions{    
    def printAssertion(tStep){    
        def assertions = tStep.getAssertionList()    
        //Loop on assertions           
    }    
}

在 SoapUI 中使用此脚本:

TestStep=testRunner.testCase.testSuite.getTestCaseByName("addTestCase").getTestStepByName("add")

    //context.Assertions.printAssertion(TestStep)

scripts = testRunner.testCase.testSuite.project.testSuites["ScriptLibrary"];
scripts.testCases["Demo"].testSteps["TestAssertion"].run(testRunner, context);

context.Assertions.printAssertion(TestStep).each{
     log.info(it.name +  ' --> ' + it.status)
    }

return null

我从你的问题中得到的是,你想在 SoapUI 中创建一个可以重复使用的代码库。 我认为最好的方法是创建 jar 文件并将其部署在 SoapUI

的 ext 文件夹中
  1. 使用 class 创建一个新的 groovy 脚本文件(遵循 java 文件命名标准,即 class 名称和文件名应该相同)
  2. 编译groovy代码文件
  3. 创建 jar 文件
  4. 在 SoapUI_Home/bin/ext 文件夹中部署 jar 文件
  5. 如果 SoapUI 已经打开,请重新启动它
  6. 创建 class 的对象并在 SoapUI 项目的任何地方使用这些方法

注意:如果您要将项目迁移到其他机器,请确保在项目中使用这些库时也迁移这些库

详细示例:

第 1 步: 创建一个具有 class 结构的新 groovy 脚本文件

我。考虑到 class ScriptLibrary 包含一个名为 printTestDetails 的方法,如下代码所示:

class ScriptLibrary  {

    def context
    def testRunner
    def log

    def printTestDetails(def PrintThisToo) {
        log.info 'Name of the test case is :'+testRunner.testCase.name
        log.info 'Name of the test suite is : '+testRunner.testCase.testSuite.name
        log.info PrintThisToo
    }
}

二。使用 class 名称保存文件,在本例中为 ScriptLibrary.groovy

第 2 步: 编译代码

我。打开命令提示符并触发以下命令(从保存 .groovy 文件的文件夹)

编译代码:

groovyc -d classes SimplePrint.groovy

步骤 3:创建 jar 文件

我。编译代码后,我们可以创建 jar 创建 jar 文件:

jar cvf SimplePrint.jar -C classes .

步骤 4: 将 jar 文件部署到 SoapUI_Home/bin/ext 文件夹

第 5 步:如果 SoapUI 已经打开,请重新启动它

第 6 步: 创建 class 的对象并在 SoapUI 项目的任何地方使用这些方法

我。创建对象

def scripts = new ScriptLibrary(context:context, log:log, testRunner:testRunner)

二。调用方法

scripts.printTestDetails(“This is my argument”)

我希望这能解决您的所有问题,这种方法将允许您在 SoapUI 中的任何地方自由使用代码,最重要的是将解决您获取 contextlogtestrunner 在外部代码中

您还可以使用您选择的任何 IDE 来创建代码库,并继续编译和创建 jar。

如果您有任何疑问或需要更多说明,请告诉我

在项目级别,您可以编写 "Load Script" 并且可以在项目级别上下文中保存实用程序 class 实例:

context.setProperty("ScriptLibrary", new ScriptLibrary())

并且在你的测试服中(例如在"Setup Script"中)你可以得到它:

testSuite.project.context.getProperty('ScriptLibrary')

或者在你的测试用例中:

testCase.testSuite.project.context.getProperty('ScriptLibrary')

或在您的 Groovy 测试步骤中:

testRunner.testCase.testSuite.project.context.getProperty('ScriptLibrary')