【问题标题】:Groovy - SoapUI - Update Headers of Request - Cannot create If statementGroovy - SoapUI - 更新请求标头 - 无法创建 If 语句
【发布时间】:2016-11-03 19:09:18
【问题描述】:

我正在 SoapUI 中创建一个测试项目,需要执行以下操作:

  • 我有一个带有 1 个测试用例的“令牌”测试套件,其中包含:为 3 种不同类型的用户(查看者、管理员、编辑器)生成令牌的 3 个步骤,然后是一个采用令牌标头的 Groovy 脚本测试步骤和将其添加到套件的所有测试用例中。
  • 对于某些测试,我想在测试步骤中添加额外的标头,但我不想删除已分配的令牌标头。

我创建了这个脚本:

import groovy.json.JsonSlurper;
import com.eviware.soapui.support.types.StringToStringMap;

//Get through all the test steps in the project
testRunner.testCase.testSuite.project.testSuites.each {
    suite ->
        suite.getValue().testCases.each {
            q1 ->
                q1.getValue().testSteps.each {
                    it ->
                        if (it.getValue().config.type.equals("restrequest")) {
                            //Get the headers of the current teststep
                            def headers = it.getValue().getHttpRequest().getRequestHeaders()

                            //Append the new header to the existing list
                            headers.put("Header A", "Value A");
                            headers.put("Header B", "Value B");
                            headers.put("Header C", "Value C");
                            //Set the updated header list 
                            it.getValue().getHttpRequest().setRequestHeaders(headers)
                        }
                }
        }
}

此脚本将标头更新为我想要的所有测试用例,但还将标头添加到“令牌”测试套件并破坏它。

我想添加一个“IF”语句来检查:如果测试套件名称不包含“令牌”,则向其添加额外的标头。如果它包含“令牌”,请保持原样。我在项目中首先拥有令牌测试套件,因此当自动调用它时(通过 Bamboo),它会生成令牌,从而生成其余测试用例的标题。

我尝试了一些方法,但我的 Groovy 技能很业余,总是会出错。您能提出解决方案吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: groovy http-headers soapui


    【解决方案1】:

    就像您指出的那样,当套件名称不是 Token 在您的情况下,有一个 if 条件。

    这是脚本,经过少许重构/修改:

    import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
    
    //Define your suite name to ignore
    def suiteToIgnore = 'Token'
    
    
    //Get Project
    def project = testRunner.testCase.testSuite.project
    
    project.testSuiteList.each { suite ->
        if (suiteToIgnore != suite.name) {
            suite.testCaseList.each { kase ->
                kase.testStepList.each { step ->
                    if (step instanceof RestTestRequestStep) {
                        //Get the headers of the current teststep
                        def headers = step.httpRequest.requestHeaders
                        //Append the new header to the existing list
                        headers.put("Header A", "Value A");
                        headers.put("Header B", "Value B");
                        headers.put("Header C", "Value C");
                        //Set the updated header list 
                        step.httpRequest.requestHeaders = headers
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 除了答案中提供的内容之外,您在此脚本中是否还有更多内容 ¿
    • 您好 Rao,感谢您的帮助。我在运行脚本时收到以下错误消息:{Code}org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script4.groovy: 8: unexpected token: project @ line 8, column 5. def project testRunner.testCase。 testSuite.project ^ org.codehaus.groovy.syntax.SyntaxException:意外令牌:项目@第 8 行,第 5 列。在 org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:140)..{Code} PS :我看到您在代码中使用了“kase”。这是正确的,还是你的意思是“案例”?
    • 不,我在此脚本中没有任何其他代码。我应该重写我最初拥有的 2 个导入吗?
    • 抱歉,打错字了。现在固定在答案中。请问可以拿更新的吗?它在def project 之后丢失了=。是的,kase(用作变量)是正确的,因为case 是关键字。
    • 用更新的代码改变了它,现在我得到错误:{Code}org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script8.groovy: 10: expecting '}', found '- ' @ 第 10 行,第 36 列。project.testSuiteList.each { 套件 - > ^ org.codehaus.groovy.syntax.SyntaxException:期待 '}',在 org.codehaus.groovy 第 10 行,第 36 列找到'-' .antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:140) 在{代码}
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多