2015-07-01 74 views
5

請求我有一個JSON請求是這樣的:我怎麼能設置JSON對象的值作爲soapUI的

{ 
    "scopeId":"", 
    "scopeType":"", 
    "userId":"", 
    "fieldToBeEncryptedList":[ 
    { 
    "srNo":"", 
    "fieldName":"", 
    "value":"", 
    "echoField":"" 
    } 
] 
} 

現在我想的是設置這是我從響應了每個按鍵動態的價值其他測試步驟。我怎麼能使用groovy腳本來做到這一點。

在此先感謝。

+0

需要更多信息!用例子會很好。它可以在沒有Groovy腳本的情況下完成。如果您想要全面使用Groovy,請查看以下內容:https://siking.wordpress.com/2013/07/05/dynamically-create-elements-in-a-soapui-request-json-version/ – SiKing

回答

5

一些細節丟失你的問題,但我想,你有兩個REST TestSteps,假設第一稱爲REST Test Request,第二個REST Test Request2您可以使用groovy TestStep中的以下groovy腳本來設置響應並獲取您的testSteps的請求值:

import groovy.json.JsonSlurper 
import groovy.json.JsonOutput 

// request 
def request = '''{"scopeId":"", 
    "scopeType":"", 
    "userId":"", 
    "fieldToBeEncryptedList":[{"srNo":"","fieldName":"","value":"","echoField":""}] 
}''' 
// parse the request 
def jsonReq = new JsonSlurper().parseText(request); 

// get the response where you've the values you want to get 
// using the name of your test step 
def response = context.expand('${REST Test Request#Response}') 
// parse response 
def jsonResp = new JsonSlurper().parseText(response) 

// get the values from your first test response 
// and set it in the request of the second test 
jsonReq.scopeId = jsonResp.someField 
jsonReq.scopeType = jsonResp.someObject.someField 
// ... 

// parse json to string in order to save it as a property 
def jsonReqAsString = JsonOutput.toJson(jsonReq) 
// save as request for the next testStep 
def restRequest = testRunner.testCase.getTestStepByName('REST Test Request2'); 
restRequest.setPropertyValue('Request',jsonReqAsString); 

這個腳本得到你的json,並用第一個testStep響應中的值填充它,然後將此json設置爲第二個testStep的請求。

希望這會有所幫助,

+0

非常感謝您的完整答案。幫助很多 –

0

您可能需要做這樣的事情:

​import groovy.json.JsonBuilder 
import groovy.json.JsonSlurper 
def jsonStr = '{"scopeId":" ","scopeType":" ","userId":" ","fieldToBeEncryptedList":[{"srNo":" ","fieldName":" ","value":" ","echoField":" "}]}' 
def slurp = new JsonSlurper().parseText(jsonStr) 
def builder = new JsonBuilder(slurp) 
builder.content.scopeId = 'val1' 
builder.content.fieldToBeEncryptedList[0].srNo = 'val2' 
println builder.toPrettyString()​ 

輸出:

{ 
    "fieldToBeEncryptedList": [ 
     { 
      "echoField": " ", 
      "fieldName": " ", 
      "srNo": "val2", 
      "value": " " 
     } 
    ], 
    "scopeId": "val1", 
    "scopeType": " ", 
    "userId": " " 
} 
+0

Garry ,這對我不起作用。您的解決方案不會更新scopeId和另一個測試步驟中的全部內容。該測試步驟仍然保持其先前從測試用例級別上定義的屬性值取得的值。 – Bugasur

+0

適合我的工作,也增加了輸出。試試吧https://groovyconsole.appspot.com/ – Garry

+0

@Garry是你的代碼完美運行':)',但是OP不希望只是在groovy中改變一個json,它也希望得到來自SOAPUI testStep填充此json,然後在另一個testStep中將結果設置爲請求。 – albciff

相關問題