SoapUI - Membuat permintaan secara dinamis

Saya perlu membuat permintaan berdasarkan respons permintaan sebelumnya. Responnya memiliki format seperti di bawah ini. Pada dasarnya, respons dapat terdiri dari sejumlah grup yang masing-masing dapat berisi sejumlah pertanyaan Contoh Respons

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

Contoh permintaan Untuk setiap grup dan pertanyaan saya perlu menyertakan beberapa data dalam permintaan

<Header Stuff>
 <UpdateTargets>
   <Group>
     <Question>
       <ID>1234</ID>
       <Row>2</Row>
       <NewValue>my updated value</NewValue>
     </Question>

...Untuk setiap kelompok dan pertanyaan yang muncul dalam tanggapan

Saya tidak yakin bagaimana melakukan ini. Saya berasumsi semacam naskah asyik.


person user3803807    schedule 03.11.2015    source sumber
comment
Tampaknya telah mengacaukan format saya   -  person user3803807    schedule 03.11.2015
comment
Mengacaukan pemformatan saya Permintaan ‹Grup› ‹Pertanyaan› ‹ID›1234‹/› ‹Baris›2‹/Baris› ‹Kode›1-6666‹/Kode› ‹Teks›teks saya‹/Teks› ‹/Pertanyaan› ‹ Pertanyaan› ‹ID›2222‹/› ‹Baris›3‹/Baris› ‹Kode›1-111‹/Kode› ‹Teks›teks saya‹/Teks› ‹/Pertanyaan› ‹/Grup› ‹Grup› ‹Pertanyaan › ‹ID›4244‹/› ‹Baris›0‹/Baris› ‹Kode›1-8888‹/Kode› ‹Teks›teks sayafgdfgd‹/Teks› ‹/Pertanyaan› ‹/Grup›   -  person user3803807    schedule 03.11.2015


Jawaban (1)


Tambahkan skrip testStep yang asyik dan di dalamnya Anda dapat menggunakan XmlSlurper untuk membaca konten respons Anda sebelumnya dan groovy.xml.MarkupBuilder untuk membuat permintaan baru dari data sebelumnya. Lihat kode berikut yang saya harap cukup jelas:

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

Kode ini mencatat permintaan baru Anda sebagai:

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

EDIT BERDASARKAN KOMENTAR OP

Jika Anda memiliki bagian statis dari permintaan Anda, Anda dapat menyertakan bagian dinamyc yang dibuat dengan groovy.xml.MarkupBuilder sebagai berikut:

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

Ini akan menghasilkan:

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

Semoga membantu,

person albciff    schedule 03.11.2015
comment
Terima kasih, hargai bantuan Anda. Permintaan yang saya buat juga akan berisi header dan beberapa tag di awal yang tidak akan terulang. dapatkah metode ini digunakan untuk menambahkan hal-hal dinamis ke permintaan. Misalnya ‹Envelope›‹Header›........‹/Header›‹Body›‹Beberapa Tag›‹/Beberapa tag›‹UpdateTargets›.......tag dinamis ...... ..‹/UpdateTargets›‹/Isi›‹/Amplop› - person user3803807; 03.11.2015
comment
@ user3803807 Saya memperbarui jawaban dengan permintaan baru Anda, lihatlah :). - person albciff; 03.11.2015
comment
Penyematannya berfungsi dengan baik tetapi saya tidak dapat membuat bagian pertama berfungsi. Itu terus muncul kembali hanya dengan ‹UpdateTargets/›Saya menggunakan operasi GetCityForecastByZip di tutorial SoapUI untuk menguji (wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL) dan kode yang saya gunakan ada di komentar berikutnya. Dapatkah Anda melihat sesuatu yang jelas-jelas salah - person user3803807; 03.11.2015
comment
request.UpdateTargets { // untuk setiap ‹Grup› sebagai tanggapan buat grup di berikutnya // minta root.Forecast.each{ perkiraan -› Perkiraan { // untuk setiap ‹Pertanyaan› sebagai tanggapan buat pertanyaan di berikutnya // minta perkiraan .WeatherID.each { ID cuaca -› WeatherID(){ ID('NilaiBaru Anda') Baris('NilaiBaru Anda') Nilai Baru('NilaiBaru Anda') } } } } } - person user3803807; 03.11.2015
comment
Masalahnya adalah <Forecast> bukan merupakan anak langsung dari simpul akar sebagai respons, perbaiki jalur ke root.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.ForecastResult.Forecast.each {... alternatifnya Anda dapat menggunakan findAll sebagai berikut: root.'**'.findAll { it.name() == 'Forecast' }.each{ ... . Kedua ekspresi melakukan hal yang sama dan menyelesaikan masalah Anda, namun agar semuanya lebih jelas, lebih baik Anda menggunakan pendekatan pertama. - person albciff; 03.11.2015
comment
Terima kasih, hargai itu. masih tidak dapat menemukan simpul def xml = konteks.testCase.getTestStepByName('GetCityForecastByZIP').getPropertyValue('response') root = new XmlSlurper().parseText(xml) def sw = new StringWriter() def request = new groovy .xml.MarkupBuilder(sw) // buat ‹UpdateTargets› request.UpdateTargets { Store(Gav) Mine(Gav2) Items{ root.Body.GetCityForecastByZIPResponse.GetCityForecastByZIPResult.ForecastResul‌​t.Forecast.each{forecast-› Forecast{ Store2( Gav333) } } } } log.info sw Item masih muncul sebagai ‹items/› di log - person user3803807; 03.11.2015