------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. 【传智播客】Libevent学习笔记(三):事件循环

    目录 00. 目录 01. event_base_loop函数 02. event_base_dispatch函数 03. event_base_loopexit函数 04. event_base_l ...

  2. Node.js 创建server服务器

    var http=require('http'); //引入http模块 var server=http.createServer(function(req,res){  //创建一个server r ...

  3. vue 父子组件的加载顺序

    一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...

  4. poj 1664放苹果(转载,不详细,勿点)(递归)

    题目和别人的解析传送门 我的代码 #include<bits/stdc++.h> using namespace std; int f(int m,int n) { ) ; ||m==) ...

  5. vs编译应用程序不依赖运行vs环境

    控制台应用程序不依赖VS运行环境就可以运行的设置: 1,使用release模式编译 2,项目属性中,“C/C++”,“代码生成”中的运行库改为“多线程(/MT)” 3,项目属性中,“链接器”,“系统” ...

  6. ICPC-Beijing 2006 狼抓兔子

    题目描述 题解: 裸的最小割. 但是最大流跑不过去怎么办? 转变一下,既然最大流是一条左下<->右上的通路,我们可以把图划分为若干区域, 最后找左下到右上的最短路就行了. 代码: #inc ...

  7. docker的网络(基础)

    Docker的网络子系统是可插拔的,使用驱动程序.默认情况下存在多个驱动程序,并提供核心网络功能: bridge:docker默认的网络驱动.如果未指定驱动程序,则这是需要创建的网络类型.当应用程序在 ...

  8. (7) openssl dgst(生成和验证数字签名)

    该伪命令是单向加密工具,用于生成文件的摘要信息                  也可以进行数字签名,及验证数字签名. 首先要明白的是,数字签名的过程是计算出摘要信息,然后使用私钥对摘要信息进行加密得 ...

  9. css搞定所有垂直居中问题

    单行文本 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  10. 洛谷 1196 [NOI2002]银河英雄传说【模板】带权并查集

    [题解] 经典的带权并查集题目. 设cnt[i]表示i前面的点的数量,siz[i]表示第i个点(这个点是代表元)所处的联通块的大小:合并的时候更新siz.旧的代表元的cnt,路径压缩的时候维护cnt即 ...