1

I am new to Groovy scripting.

Requirement To read the request values from a text file and pass it to the soap request xml and save the output.

Issue facing: I am not able to read the data from step 1 to step 2. However I am setting the values in context variable as well. Kindly help me to fix the issue so that I can able to automate the entire process.

Note: We have only access to SOAPUI not SOAPUI Pro

Step 1:

File file1 = new File("C:\\Users\\Groovy Test\\requests\\orders.txt") 
List textLine = file1.readLines() 
log.info textLine 
context.put('textLine', textLine)  
log.info textLine

Step 2:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<OrderId>${context.get('textLine' )}</OrderId>
</soapenv:Body>
</soapenv:Envelope>

Step 3:

def fileList = context.get('textLine')
def fileName = fileList.pop()
def newname = fileName[0..-5]
def response = context.expand( '${Step2#Response}' )
def f = new File("C:\\Users\\Groovy Test\\responses\\${fileName}_Response.xml")
f.write(response, "UTF-8")
if(fileList.size() >0)
{
testRunner.gotoStepByName("Step2")
}
Rao
  • 20,781
  • 11
  • 57
  • 77
user3212324
  • 163
  • 1
  • 6
  • 23
  • It is a bit unclear. Can you clarify your use case probably with your test case and its steps or screen shot? And how you want transform the data? Also show the contents of your `orders.txt` file to see how the data formatted? Are you trying to do data-driven tests? – Rao Nov 03 '16 at 15:50
  • Hi Rao, Thanks for your time. Basically the webservice will take order id as input and provide the order details as response. I am trying to pass the order id from external source (txt file) to the soap request. In step 1 input file is being read and order id is saved in context variable. In step 2 order id is being read from context variable, In step 3 I am saving the response. – user3212324 Nov 03 '16 at 15:54
  • In Step 1 & 3, I can able to read the values inside orders.txt (for eg: 12345678). But I am not able to pass the context value in Step 2 which is the xml request – user3212324 Nov 03 '16 at 15:59
  • Can the orderId be a random value or must have valid one? How many orderIds in the text file? – Rao Nov 03 '16 at 16:07
  • Order id will be valid one.. It is of length 8 digit. Number of order id it depends per day usage. It may vary for example one day it will be 10 or another day 15. Please let me know whether any other approach is available to read the input from external file and to provide it to the soap request. Note: We have only SOAP UI not SOAPUI pro – user3212324 Nov 03 '16 at 16:22

2 Answers2

3

Here is the approach to achieve what you are looking for.

The test case contains 3 steps as shown below:

  • step1 - Groovy Script test step. This reads data source, executes order steps by looping thru it. Controls test run.
  • step2 - Soap Request test step. Gets orders & saves response.
  • step3 - Groovy Script test step. A way to exit the test execution.

Step1

Groovy Script for step1:

def data = new File('C:/Users/Groovy Test/requests/orders.txt') 
data.eachLine { orderId ->
   context.orderId = orderId
   //Get the step2, index of the step is 1
   def step = context.testCase.getTestStepAt(1)
   //Run the step2
   step.run(testRunner, context)
}
//By now all the orders got executed, now need to exit the step without additionally running step2
//So, jump to step2, index is 2
testRunner.gotoStep(2)

Step2

Change the request to use <OrderId>${orderId}</OrderId>

Add Script Assertion for the request of step2. This checks the response and saves it.

Script Assertion for step2

//Check if there is response
assert context.request, "Request is empty or null"

//Save the contents to a file
def saveToFile(file, content) {
    if (!file.parentFile.exists()) {
         file.parentFile.mkdirs()
         log.info "Directory did not exist, created"
    }
    file.write(content) 
    assert file.exists(), "${file.name} not created"
}

def f = new File("C:/Users/Groovy Test/responses/${context.orderId}_Response.xml")
saveToFile(f, context.response)

Step3

Groovy Script for step3:

log.info "Test completed."
Rao
  • 20,781
  • 11
  • 57
  • 77
  • Great. Thanks @Rao, it is working fine. I have modified few lines in assertion since the responses are not saved initially. Below is te one I have did it and it works fine. – user3212324 Nov 04 '16 at 08:19
  • def response = context.expand( '${Step2#Response}' ) def f = new File("C:\\Users\\Documents\\Groovy Scripts\\response\\${context.orderId}_Response.xml") f.write(response, "UTF-8") – user3212324 Nov 04 '16 at 08:20
  • @user3212324, happy to know that it is helpful. Suggest, not use spaces in directory / file names.If possible put the actual paths into test case or suite or project properties, so that it is easy for other members of your team uses it by just changing the value of the property without editing scripts. – Rao Nov 04 '16 at 08:25
2

I think that the problem is the notation in the Xml of the step 2:

Use:

<OrderId>${=context.get('textLine')}</OrderId>

Instead of:

<OrderId>${context.get('textLine')}</OrderId>

Note the = character.

albciff
  • 18,112
  • 4
  • 64
  • 89
  • 1
    Super sonic fast :), @albciff. another way, `${textLine}` – Rao Nov 03 '16 at 16:26
  • Great...Both the answers are working. But [] is appended. Can you please help me how to remove it. [99997785]. This is how I am getting which is providing no response due to square bracket. – user3212324 Nov 03 '16 at 16:30
  • Do you want set all the orders in one go? that was a list, one order id. – Rao Nov 03 '16 at 16:32
  • Also If I put 3 order id's for each request the order id is appending. Please find the below. How to run all 3 steps for one order id then go for the second one.[99997712, 99997713, 99997714] – user3212324 Nov 03 '16 at 16:34
  • No Rao, I need to run and save the response for each order id. But the loop should continue till last line of the txt file – user3212324 Nov 03 '16 at 16:35
  • Hi Rao, Kindly help me to modify to iterate the txt file. I am entirely new to groovy script and currently blank over this issue – user3212324 Nov 03 '16 at 16:42
  • 1
    @user3212324 Can you provide a file example with some ids? Is the txt a csv? – albciff Nov 03 '16 at 16:46
  • 1
    Thanks @Rao sorry for the fast approach... I'm not ever read correctly the whole question and I'm only guessing which error is the OP facing. BTW thanks for the upvote :) – albciff Nov 03 '16 at 16:48
  • 1
    @albciff, always thumbs up :). I know the question did not have adequate details. This is not relevant, got a [docker image](https://hub.docker.com/r/nmrao/soapui521/), please check if intereseted. – Rao Nov 03 '16 at 16:50
  • 1
    @albciff, @ Rao. It is an .txt file with below values as follows. 99997712 99997713 99997714. The file is not a csv file, it is a simple text file. – user3212324 Nov 03 '16 at 16:57
  • @albciff. I am sorry If I miss some information here. This is the maximum information I can provide from my end. Sorry for it. – user3212324 Nov 03 '16 at 16:59
  • @user3212324 but the numbers are separated with a blank space and all are in the first line? – albciff Nov 03 '16 at 16:59
  • 1
    The numbers are separated with new line character – user3212324 Nov 03 '16 at 16:59
  • @user3212324 ok all is clear now :), I'll answer later now I've to leave sorry. – albciff Nov 03 '16 at 17:33
  • @albciff.. Thanks for your time. We will discuss by tomorrow. Thanks again – user3212324 Nov 03 '16 at 17:34
  • @user3212324, please check the answer. Sorry albciff to hop in. – Rao Nov 03 '16 at 19:16
  • 1
    @user3212324 I'm late, Rao already give you an answer which IMO meets perfectly your requirements so check his answer. Don't hesitate to unaccept my answer to accept his answer. :) – albciff Nov 03 '16 at 21:02
  • 1
    @Rao no worries :), you give a more detailed and better answer! – albciff Nov 03 '16 at 21:03
  • @albciff, you are amazingly supportive and courageous. I am flatted with your kind words. – Rao Nov 04 '16 at 02:31
  • @Rao & @ albciff, Its been pleasure to work with you guys... You people are so supportive... I will try the approach and get back to you.. Thanks very much :) – user3212324 Nov 04 '16 at 03:25