------Groovy脚本常用方法

1.解析Json数据脚本

//groovy读取json的方式很简单,re.body.businessinfo.c2rate读取c2rate对应的值
import groovy.json.JsonSlurper def xresponse = testRunner.testCase.testSteps["Request"].testRequest.response.contentAsString
def slurper = new JsonSlurper()
def re = slurper.parseText(xresponse)
def num=re.body.businessinfo.size()
log.info(re.body.businessinfo.c2rate)

例:Json格式
{
"body":{
"businessinfo":{
"c1rate":"0.00",
"c2rate":"12.00",
"c4rate":"21.00",
"c5rate":"0.00",
"c6rate":"0.00",
"c8rate":"0.00",
"d1rate":"0.00",
"d2rate":"0.00",
"riskcode":"0355",
"ruleid":"R32000213878_11-0"
}
},
"head":{
"error_code":"0000",
"error_message":"成功",
"request_type":"???",
"response_code":"0"
}
}

2.获取reponse

//Get response
def groovyUtils = new GroovyUtils( context )
def holder = groovyUtils.getXmlHolder('inquire#Response') //Parse return value
def data = holder.getNodeValue("//return")
log.info(data)

3. 导入需要用到的方法和脚本:例如MD5方法、groovy.json.JsonSlurper方法
例如:import groovy.json.JsonSlurper //导入groovy.json包中的JsonSlurper方法

4. 调用 Groovy,java 的jar包的方法

import pub.Text
// 调用 Groovy 的jar包中方法, //此包必须与jar包中的包名一致
def text = new Text()
text.writeFile("D:\\1.log", "test")

操作方法:选中工程->Export-> JAR

这里打jar包时候要注意

1)\WebContent\META-INF\MANIFEST.MF 这个文件打到jar包中去(这个文件描述了该Jar文件的很多信息)

2)要打的是class文件,即要选择Export generated class files and resources

5. 使用Groovy脚本执行接口请求

import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext

CURRENT_TESTCASE = testRunner.testCase
TEST_SUITE = CURRENT_TESTCASE.parent def testStep = TEST_SUITE.getTestCaseByName('TestSuite').getTestStepByName('login') def testStepContext = new WsdlTestRunContext(testStep)
def result = testStep.run(testRunner, testStepContext) log.info result.responseContent

调用另一个TestSuite中的teststep脚本如下:
def testproject = testRunner.testCase.testSuite.project
def testcase = testproject.testSuites['PublicCase'].testCases['TestCase1']
testcase.testSteps["CreateService1"].run(testRunner, context)

调用本TestSuite中的TestStep脚本如下:
testRunner.runTestStepByName("Login")
testRunner.runTestStepByName("CreateAPI1-Scenario2")

6. 步骤跳转脚本
testRunner.gotoStepByName("Step2") 跳转到指定步骤
testRunner.runTestStepByName("Step2") 运行指定步骤或脚本

7. 得到32位的UUID

import java.util.UUID

//得到32位的UUID
//格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12) def uuids = UUID.randomUUID().toString().toLowerCase() //得到去除横线的UUID
def uuids = UUID.randomUUID().toString().replace("-", "").toLowerCase();

8. 获取cookie并添加到对应的请求中

import com.eviware.soapui.support.types.StringToStringMap

def cookiesList = testRunner.testCase.getTestStepByName("login").testRequest.response.responseHeaders["Set-Cookie"]
log.info cookiesList //Get the cookie
String cookieNew = cookiesList.get(0)
log.info "cookie : "+cookieNew //Put cookie to a StringMap
def cookieMap = new StringToStringMap()
cookieMap.put("Cookie",cookieNew) testRunner.testCase.getTestStepByName("filter").testRequest.setRequestHeaders(cookieMap);

其他参考资料:

soapui 自动化教程(三)

soapui 自动化教程(四)

MANIFEST.MF的用途

soapUI系列之—-03 Groovy脚本常用方法2的更多相关文章

  1. soapUI系列之—-02 Groovy脚本常用方法

    ------Groovy脚本常用方法 1. 设置参数值:setPropertyValuea. 设置 project level property//set to project level prope ...

  2. [SoapUI] Post请求Body里面限制特殊字符(&、%),Groovy脚本里特殊字符需要添加“\”转义($)。

    SoapUI的Post请求,在body里面不能包含(&.%),如果含有这些特殊字符,请求会报错:在添加的Groovy脚本里有些特殊字符需要使用“\”转义($),不然也会报错.

  3. soapUI参数中文乱码问题解决方法 (groovy脚本中文乱码)

    soapUI参数中文乱码问题解决方法 可能方案1: 字体不支持中文,将字体修改即可: file-preferences-editor settings-select font 修改字体,改成能显示中文 ...

  4. excel文件的groovy脚本在SoapUI中进行数据驱动测试

    SoapUI Pro具有从外部文件读取数据的功能,例如:excel,csv等.但SoapUI不提供从excel文件读取数据的功能.因此,为了从SoapUI中的excel文件中读取数据,我们需要在gro ...

  5. SSAS系列——【03】多维数据(多维数据集对象)

    原文:SSAS系列--[03]多维数据(多维数据集对象) 1.什么是Cube? 简单 Cube 对象由基本信息.维度和度量值组组成. 基本信息包括多维数据集的名称.多维数据集的默认度量值.数据源和存储 ...

  6. 180807-Quick-Task 动态脚本支持框架之Groovy脚本加载执行

    Quick-Task 动态脚本支持框架之Groovy脚本加载执行 上一篇简答说了如何判断有任务动态添加.删除或更新,归于一点就是监听文件的变化,判断目录下的Groovy文件是否有新增删除和改变,从而判 ...

  7. Groovy脚本-通用SQL开关

    备注:使用Groovy语言进行编写,看不懂的同学请先了解Groovy脚本. Groovy学习地址:https://www.cnblogs.com/tiechui2015/p/10828457.html ...

  8. [Tomcat 源码分析系列] (二) : Tomcat 启动脚本-catalina.bat

    概述 Tomcat 的三个最重要的启动脚本: startup.bat catalina.bat setclasspath.bat 上一篇咱们分析了 startup.bat 脚本 这一篇咱们来分析 ca ...

  9. jenkins2 groovy脚本参考

    使用plugin生成groovy脚本,或者参考已有的groovy脚本. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.co ...

随机推荐

  1. 全志T8智能汽车方案芯片参数介绍

    T8处理器代表了Allwinner在智能汽车市场上的最新成就.T8适用于需要三维图形.高级视频处理.精密相机.多种连接选项和高水平系统集成的应用程序.它将把先进的消费电子体验带入未来的汽车,实现高性能 ...

  2. MySQL(C#的链接姿势)

    介绍 这篇随笔主要介绍MySQL的基础API的使用姿势 基本使用姿势: 第一步:登陆数据库 string connStr = "Database=start;datasource=127.0 ...

  3. 5. 在Datadir目录外创建单独表空间

    5. 在Datadir目录外创建单独表空间 要在MySQL dadadir之外的位置创建单独表空间,请使用该子句: DATA DIRECTORY = '/path' 在目标目录中,MySQL会创建一个 ...

  4. 如何使用GoEasy实现PHP与Websocket实时通信

    最近搞了搞websocket 做了个简答的聊天demo 1.      从GoEasy获取appkey appkey是验证用户的有效性的唯一标识. Ø  注册账号. GoEasy官网:https:// ...

  5. Django框架基础知识03-模板变量及模板过滤器

    模板变量及模板过滤器. 1.模板路径的查找 -查找顺序 1.尝试,在app目录下存放模板. -两种方案 1.app项目文件夹下存放. 2.templates文件夹下分类存放. 首先查找项目settin ...

  6. 【转】Unable to load native-hadoop library for your platform(已解决)

    1.增加调试信息寻找问题 2.两种方式解决unable to load native-hadoop library for you platform 附:libc/glibc/glib简介 参考: 1 ...

  7. Food Delivery (区间DP)

    When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...

  8. google的三篇论文

    文章:MapReduce/GFS/BigTable三大技术资料 文章:Google File System(中文翻译) 文章:MapReduce:超大机群上的简单数据处理 文章:Google's Bi ...

  9. BZOJ1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草

    数轴上n<=1000个点,从p出发以任意顺序走到所有的点,求到达每个点的时间之和的最小值. 好题!看起来水水的实际易错! 显然的结论是经过一个区间点之后肯定落在左端点或右端点上,谁没事最后还往中 ...

  10. linux下reboot和shutdown关机命令详解

    我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用 容易导致面板卡死.所以最好的方 ...