在 SoapUI 中通过 VSTS api 将 groovy 脚本执行到 POST 时出现 400 错误
Getting 400 error on executing groovy script to POST through VSTS api in SoapUI
我正在从 SoapUI 创建到 运行 的 groovy 脚本,以通过 VSTS api 在 VSTS 中创建测试计划,但在执行它时我收到 HTTP400 错误。虽然在 SoapUI 的 REST 步骤中使用相同的 headers 成功地完成了相同的请求 运行。
最初我收到授权错误,但现在已解决..
看来我传递请求的方式 body 不正确
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);
预期结果:应使用提供的名称在 VSTS 中创建新的测试计划。
实际结果:出现错误:1 月 15 日星期二 17:03:14 IST 2019:INFO:HTTP/1.1 400 错误请求 [Cache-Control: no-cache, no-store, must-revalidate, 编译指示: no-cache, Content-Length: 207, Content-Type: application/json;字符集=utf-8,到期:-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-0000000200%0D00;域名=.dev.azure.com; expires=2024 年 1 月 14 日星期日 11:36:12 GMT;路径=/;安全的; 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-4724bb1,47217-b3b1 41=]-用户数据:3486938b-c20d-69ff-9eca-14d4011c8ebf:bmalviya@xxx.com,X-FRAME-OPTIONS:同源,X-AspNet-Version:4.0.30319,X-Powered-By:ASP.NET,X-Content-Type-选项:nosniff,X-MSEdge-Ref:参考 A:BD9BB78BC2814FFD94BEE99ECAE98955 参考 B:BY3EDGE0205 参考 C:2019-01-15T11:36:12Z,日期:星期二,15 2019 年 1 月 11:36:11 格林威治标准时间]
如果您可以让它与常规 REST TestStep 一起工作,我会围绕它构建您的功能。
- 使用该 REST TestStep 创建一个 TestCase。
- 禁用它。 SoapUI 应该不会运行吧。您的 Groovy 脚本应该。
- 添加一个Properties TestStep,并在REST TestStep中添加你想要控制的变量。
- 在您的 REST 请求中添加变量。如果是 GET,那么只需将它们添加到 URI 中即可。
- 然后修改您的 Groovy Script TestStep。它实际上不应该执行 REST 请求,而是 activate/run 您的 REST 请求 TestStep,使用类似:
def result = testRunner.testCase.getTestStepByName("The Name Of Your Rest Request TestStep").run(testRunner, context)
然后您可能想要检查测试步骤是否按预期执行。但应该这样做。您的 Groovy Script TestStep 将是唯一被 TestCase Runner 激活的脚本,但该脚本随后将根据您的代码激活您的 REST 请求 TestStep 0-N 次。确保您的 Groovy 脚本根据需要即时修改 Properties TestStep 的值。
我在套件中创建了 4 个新测试步骤:
1:属性
2: Groovy 测试步骤
3:2 HTTP请求方式测试步骤
在属性测试步骤中,我根据测试结果存储了 2 个标志的值,即 PassFlag 和 FailFlag,其值为 "T"。
在 groovy 测试步骤中,我从属性测试步骤中获取了值,一旦代码发现 PassFlag 的值为 "T",它就会转到测试步骤 "HTTP method",并提供其他通过测试用例的详细信息到 "HTTP method" 并提供测试用例失败的详细信息。下面是 groovy 代码:
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")
}
我正在从 SoapUI 创建到 运行 的 groovy 脚本,以通过 VSTS api 在 VSTS 中创建测试计划,但在执行它时我收到 HTTP400 错误。虽然在 SoapUI 的 REST 步骤中使用相同的 headers 成功地完成了相同的请求 运行。
最初我收到授权错误,但现在已解决.. 看来我传递请求的方式 body 不正确
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);
预期结果:应使用提供的名称在 VSTS 中创建新的测试计划。
实际结果:出现错误:1 月 15 日星期二 17:03:14 IST 2019:INFO:HTTP/1.1 400 错误请求 [Cache-Control: no-cache, no-store, must-revalidate, 编译指示: no-cache, Content-Length: 207, Content-Type: application/json;字符集=utf-8,到期:-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-0000000200%0D00;域名=.dev.azure.com; expires=2024 年 1 月 14 日星期日 11:36:12 GMT;路径=/;安全的; 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-4724bb1,47217-b3b1 41=]-用户数据:3486938b-c20d-69ff-9eca-14d4011c8ebf:bmalviya@xxx.com,X-FRAME-OPTIONS:同源,X-AspNet-Version:4.0.30319,X-Powered-By:ASP.NET,X-Content-Type-选项:nosniff,X-MSEdge-Ref:参考 A:BD9BB78BC2814FFD94BEE99ECAE98955 参考 B:BY3EDGE0205 参考 C:2019-01-15T11:36:12Z,日期:星期二,15 2019 年 1 月 11:36:11 格林威治标准时间]
如果您可以让它与常规 REST TestStep 一起工作,我会围绕它构建您的功能。
- 使用该 REST TestStep 创建一个 TestCase。
- 禁用它。 SoapUI 应该不会运行吧。您的 Groovy 脚本应该。
- 添加一个Properties TestStep,并在REST TestStep中添加你想要控制的变量。
- 在您的 REST 请求中添加变量。如果是 GET,那么只需将它们添加到 URI 中即可。
- 然后修改您的 Groovy Script TestStep。它实际上不应该执行 REST 请求,而是 activate/run 您的 REST 请求 TestStep,使用类似:
def result = testRunner.testCase.getTestStepByName("The Name Of Your Rest Request TestStep").run(testRunner, context)
然后您可能想要检查测试步骤是否按预期执行。但应该这样做。您的 Groovy Script TestStep 将是唯一被 TestCase Runner 激活的脚本,但该脚本随后将根据您的代码激活您的 REST 请求 TestStep 0-N 次。确保您的 Groovy 脚本根据需要即时修改 Properties TestStep 的值。
我在套件中创建了 4 个新测试步骤: 1:属性 2: Groovy 测试步骤 3:2 HTTP请求方式测试步骤
在属性测试步骤中,我根据测试结果存储了 2 个标志的值,即 PassFlag 和 FailFlag,其值为 "T"。 在 groovy 测试步骤中,我从属性测试步骤中获取了值,一旦代码发现 PassFlag 的值为 "T",它就会转到测试步骤 "HTTP method",并提供其他通过测试用例的详细信息到 "HTTP method" 并提供测试用例失败的详细信息。下面是 groovy 代码:
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")
}