Babis Paidokoukis Babis Paidokoukis - 1 month ago 19
Groovy Question

Groovy - SoapUI - Update Headers of Request - Cannot create If statement

I am creating a Test Project in SoapUI and need to do the following:


  • I have a "Token" Test Suite with 1 Test Case that has: 3 steps that generate the Token for 3 different types of User (Viewer, Admin, Editor) and then one Groovy Script Test Step that takes the Token Header and adds it to all the Test Cases of the Suite.

  • For some tests I want to add extra Headers to the Test Steps, but I don't want to delete the already assigned Token Headers.



I had this script created:

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)
}
}
}
}


This script updates the Headers to all the Test Cases that I want, BUT also, adds the Headers to the "Token" Test Suite and breaks it.

I want to add an "IF" Statement that will check: If the Test Suite name does not contain "Token" add the extra headers to it. If it contains "Token" leave it as it is. I have the Token Test Suite first in the Project so when calling it automated (through Bamboo) it generates the Token and thus the headers to the rest of the test cases.

I've tried a couple of things but my Groovy skills are amateur and always result in errors. Can you suggest a solution?

Thank you for your time.

Rao Rao
Answer

Like you pointed, have an if condition when suite name is not Token in your case.

Here is script, little refactored / groovified:

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
                }
            }
        }
    }
}
Comments