如何使用groovy将参数传递给SOAP UI测试用例中的HTTP请求(teststep)并运行它
我正在编写一个groovy脚本来执行/自动化我的测试套件。在一个测试用例中,我有一个HTTPRequest,其中我有一个请求URL、参数(用户名和密码)和方法(GET)来获取令牌id,然后我将把该令牌id传递给下一步(SOAP请求)来获取数据 我陷入了需要使用groovy传递参数(用户名和密码)、请求URL和方法(GET)的困境。 我在测试用例下手动创建了一个测试步骤,我只需要通过参数 当我在线搜索时,我知道了如何将标题、url传递到SOAP请求,如下所示如何使用groovy将参数传递给SOAP UI测试用例中的HTTP请求(teststep)并运行它,groovy,parameters,httprequest,soapui,Groovy,Parameters,Httprequest,Soapui,我正在编写一个groovy脚本来执行/自动化我的测试套件。在一个测试用例中,我有一个HTTPRequest,其中我有一个请求URL、参数(用户名和密码)和方法(GET)来获取令牌id,然后我将把该令牌id传递给下一步(SOAP请求)来获取数据 我陷入了需要使用groovy传递参数(用户名和密码)、请求URL和方法(GET)的困境。 我在测试用例下手动创建了一个测试步骤,我只需要通过参数 当我在线搜索时,我知道了如何将标题、url传递到SOAP请求,如下所示 def headers = new S
def headers = new StringToStringMap()
testRunner = new com.eviware............WsdlTestCaseRunner(myTestCase,null);
testStepContext = new com.eviware.soapui........WsdlTestRunContext(testsetp);
headers.put("apikey", "abcd")
teststep.getTestRequest().setRequestHeaders(headers)
teststep.getHttpRequest().setEndpoint(encpointurl);
testsetp.run(testRunner ,testStepContext )
但我想知道如何将参数传递给http请求(测试步骤)并运行它。- 将属性测试步骤添加到您的测试用例中。只是让它保留默认的“属性”名称
- 将需要传输的属性添加到属性测试步骤
- 在groovy teststep中,您可以使用以下方法设置属性: def properties=testRunner.testCase.getTestStepByName(“属性”); setPropertyValue(“名称”、“值”)
- 使用${Properties#name}格式的变量直接在请求中添加参数,并用实际参数名替换“name”。如果您希望这样做,可以在请求正文和URL中完成
- 将属性测试步骤添加到您的测试用例中。只是让它保留默认的“属性”名称
- 将需要传输的属性添加到属性测试步骤
- 在groovy teststep中,您可以使用以下方法设置属性: def properties=testRunner.testCase.getTestStepByName(“属性”); setPropertyValue(“名称”、“值”)
- 使用${Properties#name}格式的变量直接在请求中添加参数,并用实际参数名替换“name”。如果您希望这样做,可以在请求正文和URL中完成
def body = new StringToStringMap()
def jsonbildr = new JsonBuilder()
body.put("username","Hackme")
body.put("password","LockUout")
def root = jsonbildr body
jsonbildr = jsonbildr.toPrettyString()
log.info(jsonbildr)
testStep.setPropertyValue("Request", jsonbildr)
输出:
{
"password": "LockUout",
"username": "Hackme"
}
通过使用groovy.json.JsonBuilder类,可以完全在groovy中完成
def body = new StringToStringMap()
def jsonbildr = new JsonBuilder()
body.put("username","Hackme")
body.put("password","LockUout")
def root = jsonbildr body
jsonbildr = jsonbildr.toPrettyString()
log.info(jsonbildr)
testStep.setPropertyValue("Request", jsonbildr)
输出:
{
"password": "LockUout",
"username": "Hackme"
}
太好了,行得通<代码>testRunner.testCase.testSteps[“TokenStep”].setPropertyValue(“用户名”、“myusername”)完美,这很有效<代码>testRunner.testCase.testSteps[“TokenStep”].setPropertyValue(“用户名”、“myusername”)