Forum Discussion

giffyd's avatar
giffyd
New Contributor
5 years ago
Solved

Add headers to a teststep in rest testcase using the java api

I am using SOAP-UI 5.5 for automating some rest services and modifing the rest soap-ui project using java.  I can add a teststep using the following code snippet.    WsdlTestCase testCaseAt = p...
  • giffyd's avatar
    giffyd
    5 years ago

    I found the correct way to add the headers to a new teststep.Here is the modified codesnippet:

    WsdlTestCase testCaseAt = project.getTestSuiteAt(0).getTestCaseAt(0);
    RestResource restResourceAt = (RestResource) project.getInterfaceAt(0).getOperationAt(0);
    RestMethod restMethodAt = restResourceAt.getRestMethodAt(0);
    
    RestRequestStepFactory restRequestFactory = (RestRequestStepFactory) WsdlTestStepRegistry.getInstance().getFactory("restrequest");
    TestStepConfig testStepConfig = restRequestFactory.createNewTestStep(restMethodAt, "teststep_added");
    WsdlTestStep buildTestStep = restRequestFactory.buildTestStep(testCaseAt, testStepConfig, false);
    
    if (buildTestStep instanceof RestTestRequestStep) {
    	RestTestRequestStep step = (RestTestRequestStep)buildTestStep;
    	RestRequestStepConfig requestStepConfig = step.getRequestStepConfig();
    	if (requestStepConfig != null) {
    		RestRequestConfig restRequest = requestStepConfig.getRestRequest();
    		CompressedStringConfig addNewRequest = restRequest.addNewRequest();
    		restRequest.setRequest(addNewRequest);
    		// write headers
    		StringToStringMap map = new StringToStringMap();
    		map.put("header", "header_value");
    		step.getHttpRequest().setRequestHeaders(map);
    	}
    }
    testCaseAt.addTestStep(testStepConfig);

    The solution is similiar to the groovy script but you need a class cast before you can find the needed api.