SoapUI教程

SoapUI Groovy

SoapUI Groovy Script

Apache Groovy 是一种基于 Java 平台的面向对象、Java 语法兼容的编程语言.它具有许多类似于 python、Ruby、Pero 和 Small talk 语言的静态和动态特性。它可以用作一种编程和脚本语言,使 Java 平台能够使用编译成 Java 虚拟机( JVM) 字节码。此外,它在内部使用 Java 库,并直接在 Groovy 脚本中使用与 Java 相关的关键字和函数。它是一种包含所有 Java 库的脚本语言。如果我们想直接在 Groovy 脚本中使用 Java 关键字和函数,我们可以很容易地使用它。
在 SoapUI 工具中使用 Groovy 脚本进行 API 或 Web 服务测试。
groovy 脚本用于生成数据并将其提供给 groovy 测试请求。 用于验证常规响应。 可用于设置不同级别的脚本属性值,例如请求测试步骤、测试用例和测试套件级别。 使用 groovy 脚本,我们可以在请求的 groovy 脚本期间启用或禁用测试套件和测试用例。

在 SoapUI 工具中创建和测试 Groovy 脚本

用于在 SoapUI,我们必须遵循给定的步骤,如图所示。
步骤 1: 选择 CalculatorSoap TestSuite 和然后选择测试用例,我们将在其中创建Groovy 脚本。右键单击测试步骤,然后转到添加步骤,从可用服务中选择Groovy 脚本,如下所示。
SoapUI Groovy Script
第2步: 点击Groovy Script,它显示一个弹出对话框来指定新步骤的 groovy 脚本名称,或者我们可以离开Groovy 脚本名称为默认值,然后单击 OK 按钮。
SoapUI Groovy Script
第 3 步: 当点击 OK 按钮时,它会显示一个 Groovy Script 编辑器,我们可以在其中编写脚本并通过点击 >run按钮,如下图。
SoapUI Groovy Script
第 4 步: 现在,我们写一些 tex t 在 Groovy 脚本编辑器中,然后运行它。
让我们在 Groovy 脚本编辑器中编写一条消息,在日志输出中显示该消息。
log.info "Welcome to lidihuo"         // log is a variable and info used to print statement.
log.info ("Welcome to lidihuo")
    
第 5 步: 要在 SoapUI 工具中执行 Groovy 脚本,请单击在日志中显示脚本输出的绿色运行按钮。
SoapUI Groovy Script
示例: 编写程序返回SoapUI中使用的方法数Groovy 脚本编辑器。
通过右键单击旧的 groovy 脚本创建一个新的 Groovy 脚本,然后选择新的 groovy 脚本,如下所示。
SoapUI Groovy Script
选择Groovy Script服务后,显示groovy Script窗口,如下图
SoapUI Groovy Script
现在进入Groovy Script语句,如下图。
// It is the statement used to return the number of methods used in a SoapUI Groovy Script.
log.info testRunner.metaClass.methods*.name.unique().sort()
    
SoapUI Groovy Script
点击运行按钮后,显示所有SoapUI Groovy Script 语言中使用的方法。
Wed Sep 23 19:43:07 IST 2020:INFO:[cancel, equals, fail, getClass, getLog, getReason, getResults, getRunContext, getStartTime, getStatus, getTestCase, getTestRunnable, getTimeTaken, gotoStep, gotoStepByName, hashCode, isRunning, notify, notifyAll, runTestStep, runTestStepByName, setMockRunContext, setRunContext, start, toString, wait, waitUntilFinished]
    
示例: 编写程序以在 groovy 中创建类。
要在 SoapUI 中创建类,请右键单击 TestStep 并选择 Groovy Script 以打开编辑器.将类保存为"MyClass",然后编写如下代码,如下所示。
MyClass
import java.io.*;
class MyClass
{
def log;
def Myclass (log)
{
    log.info("Welcome to lidihuo");
}
}
context.setProperty ("MyClass", new MyClass(log)) // Specify the class name with consttructor parameter
    

SoapUI Groovy Script
点击运行按钮执行groovy脚本。
输出:
SoapUI Groovy Script

从另一个 Groovy 类调用一个 Groovy 类

要在 SoapUI 中创建一个类,请右键单击 TestStep 并选择 Groovy Script 以打开编辑器。将类保存为MyClass,然后编写如下代码,如下所示。
MyClass
import java.io.*;
class MyClass
{
def log;
def context;
def testRunner;
def Myclass (log, context, testRunner)
{
    this.log = log
    this.context = context
    this.testRunner = testRunner
}
def MyPro(String name)
{
    log.info (" Welcome to " + name);
}
}
context.setProperty ("MyClass", new MyClass(log, context, testRunner))  /* It is the necessary sentence while creating a class in SoapUI groovy */
    

SoapUI Groovy Script
点击运行按钮执行Groovy 脚本。如果脚本运行过程中出现任何错误,Groovy 图标的颜色会变为红色,如果没有错误,则图标的颜色会变为绿色 .
同样,右键单击测试步骤,然后通过添加步骤从可用资源创建 groovy 脚本。保存名为Call Groovy Script MyClass的groovy文件,然后编写如下代码调用MyClass,如下图
调用Groovy 脚本 MyClass
// initialize the testStep variable
def testStep = testRunner.testCase.getTestStepByName("MyClass")
testStep.run(testRunner, context)  
context.MyClass.MyPro( "lidihuo" ) // pass the string name to MyPro Method
    

SoapUI Groovy Script
点击运行按钮执行groovy脚本。
输出:
SoapUI Groovy Script

使用 Groovy 脚本的 SoapUI 操作符

在 SoapUI 中,我们可以使用 Groovy 脚本 编辑器来执行算术运算。算术运算符用于在 SoapUI Groovy Script 中执行简单的运算,如加法、减法、乘法、除法等。
// Addition of two number 
int a;
int b;
int c;
// assign the value to the respected variable and b
a = 5;
b = 20;
// perform the sum of two number and assign their results to another variable c.
c = a + b;
// print the sum of two number using the groovy script
log.info ( "Sum of two number is :" + c) );  /* log is a global variable, and info function is used to display the number. */
// Or we can use varibale in groovy Script follows as
a = 10  // assign varible a
b = 20  // assign varible b
c = a + b   // Store the sum of a + b into c
log.info ( "Sum of two number is :" + c)  //  print the sum
    
以下是SoapUI中groovy脚本支持的操作。

算术运算符

加法运算符/字符串连接(+) 减法运算符(-) 乘法运算符(*) 除法运算符(/) 模数或余数运算符(%)
要在 SoapUI 中创建 groovy 文件,请右键单击 TestStep 并选择 Groovy Script 以打开编辑器。将groovy文件保存为Groovy Script Arithmetic,然后编写如下代码,如下图
Groovy Script Arithmetic
/* Arithmetic operator example */
// Addition Operator
int sum = 20 + 30
log.info ( "Addition of two number is :" + sum)
sum1 = 25 + 25
log.info ( "Sum of two number is :" + sum1)
// Concatenation of two string using (+) operator
String name = "Harry" + " Potter";
log.info ( "String Concatenation of two number is :" + name);
// Subtraction Operator
int sub = 30-10
log.info ( "Subtraction of two number is :" + sub)
// Multiplication Operator
int mul = 30 * 10
log.info ( "Multiplication of two number is :" + mul)
// Division Operator
int div = 30 / 10
log.info ( "Division of two number is :" + div)
// Modulus Operator
int mod = 20 % 3
log.info ( "Modulus of two number is :" + mod)
    

SoapUI Operator Using Groovy Script
执行上面的groovy 脚本 在 SoapUI 工具中,单击显示以下输出的运行按钮。
SoapUI Operator Using Groovy Script

一元运算符

一元运算符是一个仅与一个操作数一起使用以产生新值的单个运算符。
右键单击 TestStep 并选择 Groovy Script 以打开编辑器。将groovy文件保存为Groovy Script Unary,然后编写如下代码,如下所示。
Groovy Script Unary
/* Unary operator example */
// Increment Operator
int a = 5
a++   // a = a + 1, post-increment 
log.info ("Result :" + a);
int x = 5;
++x   // pre increment
log.info ("Result :" + x);
// decrement Operator
int b = 5
b--  // a = a-1, post decrement 
log.info ("Result :" + b);
int c = 5
--c   // a = a + 1 pre decrement
log.info ("Result :" + c);
    

SoapUI Operator Using Groovy Script
在SoapUI中执行上述groovy脚本工具,单击显示以下输出的绿色运行按钮,如下所示。
SoapUI使用 Groovy 脚本的运算符

赋值运算符

赋值运算符在 groovy 脚本中用于在其右侧或左侧分配值。通常,赋值运算符使用等号(=)、+=、-=、*=、/=、%=。
右键单击 TestStep 并选择 Groovy Script 打开编辑器。将groovy文件保存为Groovy Script Assignment,然后编写如下代码,如下图。
/* Assignment operator */
int a = 10
a += 20  // a = a + 20
log.info("result is " + a);
int b = 20
b-= 10  // b = b-10
log.info("result is " + b);
int c = 10
c *= 20  // c = c * 20
log.info("result is " + c);
int d = 20
d /= 10   // d = d / 10
log.info("result is " + d);
int e = 22
e %= 10  // e = e % 10
log.info("result is " + e);
    

SoapUI Operator Using Groovy Script
要执行上述 Groovy 脚本,请单击显示以下输出的绿色运行按钮。
SoapUI Operator Using Groovy Script
同样,我们可以使用 Groovy 脚本支持的其他运算符,例如逻辑、关系、比较、条件等。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4