在SoapUI免费版中创建脚本库 [英] Creating script library in SoapUI free version

查看:55
本文介绍了在SoapUI免费版中创建脚本库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SoapUI和groovy脚本的新手

I am new in SoapUI and groovy scripting

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

I would like to create a repository of groovy scripts that can be reused at various test steps

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

I am using SoapUI Free version and following is the structure of my SoapUI Project

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)

这就是我所能做的:

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

I was able to create a library using the sample mentioned in this post. Similar to example in the post, my code in test step (GroovyScriptStep1 as per above structure) of library was just reading some value from external file and is used in test step of other TestSuite (LocalScript step in above structure).

问题出在这里

现在,我想创建一个新类并向其中添加一个函数,该函数将需要运行类的信息并简单地打印它.此处的区别在于,某些值是在测试运行中生成的,应将其传递给库脚本以进行处理/打印等.

Now I want to create a new class and add a function to it which will need info from running class and simply print it. The difference here is that some values are generated in the test run and should be passed to library script inorder to process/print etc.

为了使我的问题更清楚,以下是代码段

To make my question more clear following is the code snippet

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

I will be using a simple scenario here

样本目标:希望能够打印所有断言和状态(因为这将在我要创建库的所有测试用例中使用)

Sample objective: Want to be able to print all the assertions and status (since this will be used in all the test cases I want to create a library)

不使用库时相同的代码如下(可以通过常规脚本步骤进行操作)

Code for same when not using library will be as under(this can go as groovy script step)

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

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

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

Code something similar in Library TestSuite's Test case step

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)

Code from where I want to call this method (LocalScript as per above project structure)

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

context.Assertions.printAssertion(ArgumentRequired);

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

This is just one example, I want to create libraries of some more common scripts that use context variable when used locally

请帮助我,如果需要更多信息/说明,请告诉我

Kindly help me with this and please let me know if some more information/clarification is required

推荐答案

我从您的问题中得到的是您想在SoapUI中创建一个可重用的代码库. 我认为最好的方法是创建jar文件并将其部署在SoapUI的ext文件夹中

What I get from your questions is that you want to create a code library in SoapUI that can be reused. I think the best way is by creating jar files and deploying it in ext folder of SoapUI

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

注意:如果要将项目迁移到其他计算机,请确保也要迁移这些库(如果在项目中使用它们的话)

Note: If you are migrating your project to some other machine, make sure to migrate these libraries as well if you are using them in projects

示例详细信息:

步骤1:用类结构创建一个新的groovy脚本文件

Step 1: Create a new groovy script file with a class structure

i.考虑类ScriptLibrary,其中包含一个名为printTestDetails的方法,如下面的代码所示:

i. Considering the class ScriptLibrary that contain a method named printTestDetails as in following code in it:

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

ii.使用类名称ScriptLibrary.groovy保存文件

ii. Save the file with class name, ScriptLibrary.groovy in this case

第2步:编译代码

i.打开命令提示符并执行以下命令(从保存.groovy文件的文件夹中)

i. Open command prompt and fire following command (from the folder where where your .groovy file is kept)

编译代码:

groovyc -d classes SimplePrint.groovy

第3步:创建jar文件

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

i. After compiling the code we can create the jar Create jar file:

jar cvf SimplePrint.jar -C classes .

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

Step 4: Deploy the jar file at SoapUI_Home/bin/ext folder

步骤5:如果已打开SoapUI,请重新启动

Step 5: Restart the SoapUI if was already open

步骤6:创建类的对象并在SoapUI项目中的任何位置使用方法

Step 6: Create the object of class and use the methods anywhere in the SoapUI projects

i.创建对象

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

ii.调用方法

scripts.printTestDetails("This is my argument")

我希望这可以整体解决您的问题,这种方法将使您可以在SoapUI中的任何位置自由使用代码,最重要的是可以解决在外部代码中获取contextlogtestrunner的问题

I hope this solves your problem over all, this approach will allow you to freely use the code any where in the SoapUI and most importantly will solve your problem for getting context, log and testrunner in external code

您还可以使用您选择的任何IDE来创建代码库,并在其上进行编译和创建jar.

You can also use any IDE of your choice to create code library and work on same to compile and create jar as well.

如果您有任何疑问或需要进一步澄清,请告诉我

Let me know if you have any doubts or need more clarification

这篇关于在SoapUI免费版中创建脚本库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆