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 = 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"); restRequestFactory.buildTestStep(testCaseAt, testStepConfig, false); testCaseAt.addTestStep(testStepConfig);
Now I am looking for a way to add the headers to that teststep. I knwo how to add headers to a request in the interface of the restservice using a code snippet (will not work, just for showing the approache) similar like this :
StringToStringMap map = new StringToStringMap(); map.put("headername", "headervalue"); request = RestMethod.addNewRequest("name"); request.setRequestHeaders(map);
But there is not a similar api for teststeps in a testcase. Can someone show me a way how to do this?
I searched around the community and tried different ways, but up to now I am clueless...
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.