0
votes

I am creating a groovy script to run from SoapUI to create test plans in VSTS through VSTS api, But on executing it I am getting HTTP400 error. While the same request runs successfully with same headers in REST step of SoapUI.

Initialy I was getting error for authorization but now that got resolved.. It seems the way I am passing the Request body is incorrect

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
import org.apache.commons.codec.binary.Base64;
import com.eviware.soapui.support.types.StringToStringMap
import org.apache.http.client.methods.HttpPost;
import java.io.File;
import java.io.IOException; 
import groovy.json.JsonBuilder
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.entity.mime.content.StringBody; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import static groovyx.net.http.Method.*;
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HTTP;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.entity.StringEntity;
import static groovy.json.JsonOutput.toJson;

HttpClient httpclient = new DefaultHttpClient();
def post = new HttpPost("https://dev.azure.com/xxx?api-version=1.0");

def jsonBody = [:]
// Test title
jsonBody.put("title", "Test title")

def http = new HTTPBuilder( 'https://dev.azure.com/xxx?api-version=1.0' )
post.addHeader("Authorization","Basic 
Onvdfdsfgsdgdfhgfhgfhgfhjgfhtrhtrhtrhtrbvdfb=");
post.addHeader("Accept","application/json");
post.setEntity(jsonBody)

HttpResponse response = httpclient.execute(post);

Expected Result: New test plan should be created in VSTS with the Provided name.

Actual Result: Getting Error: Tue Jan 15 17:03:14 IST 2019:INFO:HTTP/1.1 400 Bad Request [Cache-Control: no-cache, no-store, must-revalidate, Pragma: no-cache, Content-Length: 207, Content-Type: application/json; charset=utf-8, Expires: -1, P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT", Set-Cookie: VstsSession=%7B%22PersistentSessionId%22%3A%22b53ba5ae-5940-4bc4-b464-24b72350fd09%22%2C%22PendingAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%2C%22CurrentAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%7D; domain=.dev.azure.com; expires=Sun, 14-Jan-2024 11:36:12 GMT; path=/; secure; HttpOnly, X-TFS-ProcessId: ffde6ea4-0faa-4f90-b5d8-07ed687398f7, Strict-Transport-Security: max-age=31536000; includeSubDomains, ActivityId: 3b96e21f-0539-4724-b31f-178b14072bb1, X-TFS-Session: 3b96e21f-0539-4724-b31f-178b14072bb1, X-VSS-E2EID: 3b96e21f-0539-4724-b31f-178b14072bb1, X-VSS-UserData: 3486938b-c20d-69ff-9eca-14d4011c8ebf:[email protected], X-FRAME-OPTIONS: SAMEORIGIN, X-AspNet-Version: 4.0.30319, X-Powered-By: ASP.NET, X-Content-Type-Options: nosniff, X-MSEdge-Ref: Ref A: BD9BB78BC2814FFD94BEE99ECAE98955 Ref B: BY3EDGE0205 Ref C: 2019-01-15T11:36:12Z, Date: Tue, 15 Jan 2019 11:36:11 GMT]

2
You can use REST Request step in SoapUI which is meant for testing the same and not sure why groovy?Rao
Yes REST tescase can work but the purpose won't be solved... I want to update the testcase status as Passed or failed as per the result of the test run so that would be possible only from the groovy script if else conditions.Bharat

2 Answers

0
votes

If you can get it working with a regular REST TestStep, I would build your functionality around that.

  • Create a TestCase with that REST TestStep.
  • Disable it. SoapUI should not run it. Your Groovy Script should.
  • Add a Properties TestStep, and add the variables you want to control in the REST TestStep.
  • Add the variables inside your REST Request. If it's a GET, then just add them in the URI.
  • Then modify your Groovy Script TestStep. It shouldn't actually DO the REST Request, but rather activate/run your REST Request TestStep, using something like:

def result = testRunner.testCase.getTestStepByName("The Name Of Your Rest Request TestStep").run(testRunner, context)

You may then want to check if the teststep executed as expected. But that should do it. Your Groovy Script TestStep will be the only one activated by the TestCase Runner, but the script will then activate your REST Request TestStep 0-N times depending on your code. Make sure your Groovy Script modifies the values of your Properties TestStep as needed on the fly.

0
votes

I created 4 new test steps in my suite: 1: Properties 2: Groovy test step 3: 2 HTTP request method test steps

In the properties test step I stored the value of 2 flags viz PassFlag and FailFlag with value as "T" depending upon the result from the test result. In the groovy test step I fetched the value from properties test step and once the code finds the value of PassFlag as "T" it moves to test step "HTTP method" with details to pass the test case else to the "HTTP method" with details to fail the test case. below is the groovy code:

import com.eviware.soapui.support.*; 
import com.jcraft.jsch.*

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def myTestCase = testRunner.getTestCase()//myTestCase contains the test case 
propTestStep = myTestCase.getTestStepByName("PassFailStatus") // get the Property 
TestStep object
def testCase = testRunner.getTestCase() 
def propTestStep = testCase.getTestStepByName("PassFailStatus")
def Passvalue = propTestStep.getPropertyValue("PassFlag").toString()
def Failvalue = propTestStep.getPropertyValue("FailFlag").toString()

if(Passvalue == "T"){

Thread.sleep(30000)
testRunner.gotoStep(testRunner.testCase.getTestStepIndexByName('PassTestCase')) 

log.info("success")
}else{

testRunner.gotoStep(testRunner.testCase.getTestStepIndexByName('FailTestCase')) 

log.info("Failure") 
}