Groovy 在SoapUI免费版本中创建脚本库
我是SoapUI和groovy脚本的新手 我想创建一个groovy脚本存储库,可以在各种测试步骤中重用 我使用的是SoapUI免费版本,下面是我的SoapUI项目的结构Groovy 在SoapUI免费版本中创建脚本库,groovy,soapui,Groovy,Soapui,我是SoapUI和groovy脚本的新手 我想创建一个groovy脚本存储库,可以在各种测试步骤中重用 我使用的是SoapUI免费版本,下面是我的SoapUI项目的结构 Project |-TestSuite | |-TestCase | |-TestSteps | |-LocalScript (Groovy TestStep to reuse library scripts) | |-OtherTestStep (Run TestCase TestStep) |-Scrip
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步骤)
问题是:
现在,我想创建一个新类,并向其中添加一个函数,该函数需要运行该类的信息,并简单地打印它。这里的区别在于,一些值是在测试运行中生成的,应该传递给库脚本,以便处理/打印等
为了让我的问题更清楚,下面是代码片段
这里我将使用一个简单的场景
示例目标:希望能够打印所有断言和状态(因为这将在我想要创建库的所有测试用例中使用)
不使用库时的相同代码如下(这可以作为groovy脚本步骤)
在库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)
这只是一个例子,我想创建一些更常见的脚本库,这些脚本在本地使用时使用上下文变量
请帮助我,如果需要更多信息/澄清,请告诉我
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文件夹中
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
}
}
二,。在本例中,使用类名ScriptLibrary.groovy
保存文件
步骤2:编译代码
一,。打开命令提示符并激发以下命令(从保存.groovy
文件的文件夹中)
编译代码:
groovyc -d classes SimplePrint.groovy
步骤3:创建jar文件
一,。在编译代码之后,我们可以创建jar
创建jar文件:
jar cvf SimplePrint.jar -C classes .
步骤4:在SoapUI\u Home/bin/ext
文件夹中部署jar文件
步骤5:如果SoapUI已经打开,请重新启动
步骤6:创建类的对象,并在SoapUI项目中的任何位置使用方法
一,。创建对象
def scripts = new ScriptLibrary(context:context, log:log, testRunner:testRunner)
二,。调用方法
scripts.printTestDetails(“This is my argument”)
总的来说,我希望这能解决您的问题,这种方法将允许您在SoapUI中的任何位置自由使用代码,最重要的是将解决您在外部代码中获取上下文
、日志
和testrunner
的问题
您还可以使用您选择的任何IDE来创建代码库,并在代码库上编译和创建jar
如果您对项目级有任何疑问或需要更多澄清,请告知我,您可以编写“加载脚本”,并将实用程序类实例保存在项目级上下文中:
context.setProperty("ScriptLibrary", new ScriptLibrary())
在您的测试套装中(如“设置脚本”),您可以获得:
testSuite.project.context.getProperty('ScriptLibrary')
或者在您的测试用例中:
testCase.testSuite.project.context.getProperty('ScriptLibrary')
或者在Groovy测试步骤中:
testRunner.testCase.testSuite.project.context.getProperty('ScriptLibrary')
是否有效?@jsheeran是groovy的新手,我不知道如何传递参数和调用在本例中使用的类中的特定函数。不过,我应该传递什么参数,以便断言列表可以在外部类中运行并打印它。我认为这是解决大多数问题的方法,并且将更加模块化。
testCase.testSuite.project.context.getProperty('ScriptLibrary')
testRunner.testCase.testSuite.project.context.getProperty('ScriptLibrary')