SoapUI - สร้างคำขอแบบไดนามิก

ฉันต้องสร้างคำขอตามการตอบกลับของคำขอก่อนหน้า คำตอบมีรูปแบบดังนี้ โดยพื้นฐานแล้ว คำตอบสามารถมีจำนวนกลุ่มเท่าใดก็ได้ ซึ่งแต่ละกลุ่มสามารถมีคำถามจำนวนเท่าใดก็ได้ ตัวอย่างคำตอบ

<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>

...สำหรับแต่ละกลุ่มและคำถามที่ปรากฏในคำตอบ

ฉันไม่แน่ใจว่าต้องทำอย่างไร ฉันสมมติว่ามีสคริปต์ที่น่าสนใจบางอย่าง


person user3803807    schedule 03.11.2015    source แหล่งที่มา
comment
ดูเหมือนว่าจะทำให้การจัดรูปแบบของฉันเลอะเทอะ   -  person user3803807    schedule 03.11.2015
comment
ทำให้คำขอการจัดรูปแบบของฉันยุ่งเหยิง ‹กลุ่ม› ‹คำถาม› ‹ID›1234‹/› ‹Row›2‹/Row› ‹Code›1-6666‹/Code› ‹ข้อความ›ข้อความของฉัน‹/ข้อความ› ‹/คำถาม› ‹ คำถาม› ‹ID›2222‹/› ‹Row›3‹/แถว› ‹Code›1-111‹/Code› ‹Text›ข้อความของฉัน‹/ข้อความ› ‹/คำถาม› ‹/กลุ่ม› ‹กลุ่ม› ‹คำถาม› ‹ID›4244‹/› ‹Row›0‹/Row› ‹Code›1-8888‹/Code› ‹Text›ข้อความของฉันfgdfgd‹/Text› ‹/คำถาม› ‹/Group›   -  person user3803807    schedule 03.11.2015


คำตอบ (1)


เพิ่ม testStep สคริปต์ 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

หากคุณมีส่วนที่คงที่ในคำขอของคุณ คุณสามารถรวมส่วน dinamyc ที่สร้างด้วย 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 )

สิ่งนี้จะส่งออก:

<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>

หวังว่ามันจะช่วยได้

person albciff    schedule 03.11.2015
comment
ขอบคุณ ขอขอบคุณสำหรับความช่วยเหลือของคุณ คำขอที่ฉันกำลังสร้างจะมีส่วนหัวและแท็กบางส่วนที่จุดเริ่มต้นซึ่งจะไม่ทำซ้ำ สามารถใช้วิธีนี้เพื่อผนวกสิ่งไดนามิกเข้ากับคำขอได้หรือไม่ ตัวอย่างเช่น ‹Envelope›‹Header›........‹/Header›‹Body›‹Some Tags›‹/Some tags›‹UpdateTargets›.......dynamic tags ...... ..‹/UpdateTargets›‹/Body›‹/Envelope› - person user3803807; 03.11.2015
comment
@ user3803807 ฉันอัปเดตคำตอบด้วยข้อกำหนดใหม่ของคุณ ลองดูสิ :) - person albciff; 03.11.2015
comment
การฝังทำงานได้ดี แต่ฉันไม่สามารถทำให้ส่วนแรกทำงานได้ มันกลับมาเรื่อยๆ ด้วย ‹UpdateTargets/›ฉันใช้การดำเนินการ GetCityForecastByZip ในบทช่วยสอน SoapUI เพื่อทดสอบ (wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL) และโค้ดที่ฉันใช้อยู่ในความคิดเห็นถัดไป คุณเห็นสิ่งผิดปกติอย่างชัดเจนหรือไม่ - person user3803807; 03.11.2015
comment
request.UpdateTargets { // สำหรับแต่ละ ‹Group› ในการตอบสนอง ให้สร้างกลุ่มในถัดไป // ขอ root.Forecast.each{ การคาดการณ์ -› การคาดการณ์ { // สำหรับแต่ละ ‹คำถาม› ในการตอบกลับ ให้สร้างคำถามในถัดไป // คำขอการคาดการณ์ .WeatherID.each { weatherID -› WeatherID(){ ID('yourNewValue') แถว('yourNewValue') NewValue('yourNewValue') } } } } } - person user3803807; 03.11.2015
comment
ปัญหาคือ <Forecast> ไม่ใช่ลูกโดยตรงของโหนดรูทในการตอบสนอง แก้ไขพา ธ เป็น root.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.ForecastResult.Forecast.each {... หรือคุณสามารถใช้ findAll ดังนี้: root.'**'.findAll { it.name() == 'Forecast' }.each{ ... ทั้งสองสำนวนทำเช่นเดียวกันและช่วยแก้ปัญหาของคุณ อย่างไรก็ตาม เพื่อให้สิ่งต่าง ๆ ชัดเจนยิ่งขึ้นสำหรับคุณควรใช้แนวทางแรก - person albciff; 03.11.2015
comment
ขอบคุณ ขอบคุณ ยังคงไม่พบโหนด def xml = context.testCase.getTestStepByName('GetCityForecastByZIP').getPropertyValue('response') root = new XmlSlurper().parseText(xml) def sw = new StringWriter() def request = new groovy .xml.MarkupBuilder(sw) // create ‹UpdateTargets› request.UpdateTargets { Store(Gav) Mine(Gav2) Items{ root.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.ForecastResul‌​t.Forecast.each{forecast-> พยากรณ์{ Store2( Gav333) } } } } log.info sw รายการยังคงปรากฏเป็น ‹items/› ในบันทึก - person user3803807; 03.11.2015