我需要创建一个基于前一个请求的响应的请求。所述响应具有如下的格式。基本上,响应可以具有任意数量的组,其每一个可以包含任意数量的问题,样品响应
<Group>
<Question>
<ID>1234</ID>
<Row>2</Row>
<Code>1-6666</Code>
<Text>my text</Text>
</Question>
<Question>
<ID>2222</ID>
<Row>3</Row>
<Code>1-111</Code>
<Text>my text</Text>
</Question>
</Group>
<Group>
<Question>
<ID>4244</ID>
<Row>0</Row>
<Code>1-8888</Code>
<Text>my textfgdfgd</Text>
</Question>
</Group>
样品要求针对每一组的问题,我需要在请求中包含的一些数据
<Header Stuff>
<UpdateTargets>
<Group>
<Question>
<ID>1234</ID>
<Row>2</Row>
<NewValue>my updated value</NewValue>
</Question>
...各组问题的回应出现
我不知道如何做到这一点。我假设某种常规的脚本。
添加Groovy脚本一步步测试,并在它里面,你可以使用XmlSlurper
看你以前的响应的内容和groovy.xml.MarkupBuilder
创建从以前的数据了新的要求。请参见后续的代码,我希望这是自我解释:
// this is your xml hardcoded for the sample, but you can
// get it using the follow line
// def xml = context.testCase.getTestStepByName('SOAP Request').getPropertyValue('response')
def xml = '''<root><Group>
<Question>
<ID>1234</ID>
<Row>2</Row>
<Code>1-6666</Code>
<Text>my text</Text>
</Question>
<Question>
<ID>2222</ID>
<Row>3</Row>
<Code>1-111</Code>
<Text>my text</Text>
</Question>
</Group>
<Group>
<Question>
<ID>4244</ID>
<Row>0</Row>
<Code>1-8888</Code>
<Text>my textfgdfgd</Text>
</Question>
</Group></root>
'''
// parse the response from the previous testStep
def root = new XmlSlurper().parseText(xml)
// markup to create the request
def sw = new StringWriter()
def request = new groovy.xml.MarkupBuilder(sw)
// create <UpdateTargets>
request.UpdateTargets {
// for each <Group> in response create a group in next
// request
root.Group.each{ group ->
Group {
// for each <Question> in response create a question in next
// request
group.Question.each { question ->
Question(){
ID(question.ID)
Row(question.Row)
NewValue('yourNewValue')
}
}
}
}
}
log.info sw
此代码记录你的新的要求为:
<UpdateTargets>
<Group>
<Question>
<ID>1234</ID>
<Row>2</Row>
<NewValue>yourNewValue</NewValue>
</Question>
<Question>
<ID>2222</ID>
<Row>3</Row>
<NewValue>yourNewValue</NewValue>
</Question>
</Group>
<Group>
<Question>
<ID>4244</ID>
<Row>0</Row>
<NewValue>yourNewValue</NewValue>
</Question>
</Group>
</UpdateTargets>
编辑 - 基于OP COMMENT
如果您的请求的静态部分,可以包括与qazxsw POI建造动态部分如下:
groovy.xml.MarkupBuilder
这将输出:
... from previous script
// markup to create the request
def sw = new StringWriter()
def request = new groovy.xml.MarkupBuilder(sw)
// create <UpdateTargets>
...
// static data sample
def staticData =
'''<Envelope>
<Header>someData</Header>
<Body>
<SomeTags>moreData</SomeTags>
</Body>
</Envelope>'''
def newRequest = new XmlSlurper().parseText(staticData)
// dinamic part builded with markupBuilder
def dinamicPart = new XmlSlurper().parseText(sw.toString())
// append the <UpdateTargets> to your static part
newRequest.Body.appendNode(dinamicPart)
// serialize the full xml
log.info groovy.xml.XmlUtil.serialize( newRequest )
希望能帮助到你,