我需要根据先前请求的响应创建请求。响应的格式如下。基本上,响应可以包含任意数量的组,每组可以包含任意数量的问题 示例回复
<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>
...对于响应中出现的每个小组和问题
我不知道该怎么做。我假设某种时髦的剧本。
答案 0 :(得分:0)
添加一个groovy脚本testStep,在其中你可以使用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评论的编辑
如果您的请求中有静态部分,则可以包含使用groovy.xml.MarkupBuilder
构建的dinamyc部分,如下所示:
... 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 )
这将输出:
<Envelope>
<Header>someData</Header>
<Body>
<SomeTags>moreData</SomeTags>
<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>
</Body>
</Envelope>
希望它有所帮助,