SOAPUI 使用groovy发送http请求 自动化测试

思路是使用一个配置文件,决定发送哪些请求及顺序。对结果与期望值进行匹配,断言某个请求的成功或失败。

摘要:groovy 动态添加request参数 发送http请求 结果期望匹配 自动化

soapui api地址:http://www.soapui.org/apidocs/index.html

国内资料较少,搜到的就那几篇。
我是这样了解soapui类的,打印类名,然后查API,了解该类的方法。
SOAPUI 使用groovy发送http请求 自动化测试_第1张图片

详细教程

源码:

import groovy.json.JsonSlurper
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext

def CURRENT_TESTCASE = testRunner.testCase
def TEST_SUITE = CURRENT_TESTCASE.parent

def JSON = new JsonSlurper()

//加载任务
def TASKS = {
    def taskFile = new File(CURRENT_TESTCASE.properties.task_file.value)
    //log.info taskFile.getText()

    JSON.parseText(taskFile.getText())
}()

//获取登录token
def LOGIN_STATUS = {
    def loginStep = CURRENT_TESTCASE.getTestStepByName("login")
    //发送请求
    def result = loginStep.run(testRunner, new WsdlTestRunContext(loginStep))

    def rsJson = JSON.parseText(result.responseContent)

    if (rsJson.flag == "1" && rsJson.data != null) {
        log.info "【login】${rsJson.data.toString()}"
        return rsJson.data
    } else {
        log.info "登录失败!"
        assert false
    }
}()

//所有请求添加登录状态 动态添加参数到requet中 添加的参数必须在资源的请求中定义 如果是动态可以默认为空
def addLoginStatus = {testStep ->
    testStep.setPropertyValue("token", LOGIN_STATUS.token)
    testStep.setPropertyValue("Id", LOGIN_STATUS.Id)
    testStep.setPropertyValue("account", LOGIN_STATUS.account)
    testStep.setPropertyValue("accountId", LOGIN_STATUS.accountId)
    testStep.setPropertyValue("os", "A")
}

//递归    深度比较返回值与期望是否匹配
def verifyExcept(actual, expect) {
    def rs = true

    //类型判断  java Collection 与 Map 不同的遍历方式
    if (expect instanceof Collection) {
        expect.eachWithIndex {it, i ->
            //log.info "i:" + i + "  it:" + it
            if (it instanceof Map || it instanceof Collection) {
                return rs = verifyExcept(actual[i], it)
            } else {
                if (actual[i] != it) {
                    log.error "1actual:${actual[i]}  expect: $index:${i}=${it}"
                    return rs = false
                }
            }
        }
    } else if (expect instanceof Map) {
        expect.each {k, v ->
            //log.info "k:" + k + "  v:" + v
            if (v instanceof Map || v instanceof Collection) {
                return rs = verifyExcept(actual[k], v)
            } else {
                if (actual[k] != v) {
                    log.error "2actual:${actual[k]}  expect: ${k} = ${v}"
                    return rs = false
                }
            }
        }
    }

    return rs
}


//执行任务
TASKS.each {
    def caseName = it.reqName.split("\\.")[0]
    def stepName = it.reqName.split("\\.")[1]

    def testCase = TEST_SUITE.getTestCaseByName(caseName)
    def testStep = testCase.getTestStepByName(stepName)
    addLoginStatus(testStep)
    //log.info "token:" + testStep.getPropertyValue("token")

    def testStepContext = new WsdlTestRunContext(testStep)
    def result = testStep.run(testRunner, testStepContext)

    log.info "【${it.reqName}:response】${JSON.parseText(result.responseContent)}"
    log.info "【${it.reqName}:expect】${it.expect}"
    def rsJson = JSON.parseText(result.responseContent)

    if (verifyExcept(rsJson, it.expect)) {
        log.info "【${it.reqName}】success!"
        assert true
    } else {
        log.error "【${it.reqName}】fail!"
        assert false
    }
}

return 

执行结果
SOAPUI 使用groovy发送http请求 自动化测试_第2张图片
其中第十行CURRENT_TESTCASE.properties.task_file文件内容如下:

[{
    "reqName": "GetChannelInfo.getChannelInfo",  //对应 测试用例名·请求名
    "expect": {
        "flag": "1",
        "data": {
            "saleList": [{
                "value": "02"
            }]
        }
    }
}, {
    "reqName": "isBindCard.isBindCard",
    "expect": {
        "flag": "1",
        "data": {
            "isNotCheck": "0"
        }
    }
}]

任务文件路径配置
SOAPUI 使用groovy发送http请求 自动化测试_第3张图片

参数预留给登录状态需要的动态添加的参数
SOAPUI 使用groovy发送http请求 自动化测试_第4张图片

添加完请求后自动生成测试用例
SOAPUI 使用groovy发送http请求 自动化测试_第5张图片

你可能感兴趣的:(自动化测试,soapui,groovy)