Monday, May 23, 2011

Groovy III - text file as Data Source for input

/*
@Title : Gr-ooo-vy Code III
@Description : using text file as Data Source using Groovy code lines
@Author : Pradeep Bishnoi
*/

This is another blog post unleashing the power of Groovy script to perform certain set of task. SoapUI users, who have used the 15 days trail license of SoapUI Pro version, must be missing various important features, after license expiration. Well, in this blog i will try to replicate one of the important feature of Data Source [provided by Pro version] using the Groovy. A lot can be achieved using the Groovy...

Recommendation : I would encourage users to go for soapUI Pro license to avail many more useful features + the support from soapUI support team. Go soapUI Pro!!

Copy and paste the below provided code lines in the TearDown script for the selected TestCase.


import com.eviware.soapui.support.XmlHolder

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def folderName = "D:/Automate/response"  //to store the resonse
def createFolder = new File(folderName)
createFolder.mkdir()

def testSuite = testRunner.testCase.testSuite.project.testSuites["My_TestSuite"]
def testCase = testSuite.getTestCaseAt(0)
def testStep1 = testCase.getTestStepCount()

def xHolder = new XmlHolder(testCase.getTestStepAt(0).getProperty("Response").getValue())

File tempFile = new File("D:/data_source_file.txt")
List lines = tempFile.readLines()
def i=0
def temp1 = 0
def inputValue

def myTestStep
def myTestRunner
def myTestStepContext
//Loop through all the names of the test steps.
( 0..<testStep1 ).each
{
    i=0

   tempFile.eachLine
    {
        testCase.setPropertyValue("inputValue", lines[i++])
        inputValue = context.expand( '${#TestCase#inputValue}' )
        myTestStep = testCase.getTestStepAt(temp1)
        myTestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(myTestCase, null)
        myTestStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext( myTestStep )
        myTestStep.run( myTestRunner, myTestStepContext )
       

xHolder = new XmlHolder(testCase.getTestStepAt(temp1).getProperty("Response").getValue())
        f = new File( folderName + '/' + inputValue + '_' +  testCase.getTestStepAt(temp1).getName().toString() + '.txt')
        f.append("\r\n\r\n\r\n\r\n\r\n")
        f.append("#################################################\r\n")
        f.append("\t\t\t Response \r\n")
        f.append("#################################################\r\n\r\n\r\n\r\n\r\n")
        f.write(xHolder.prettyXml)
        xHolder.clear()
   
    }
    temp1++
}


Now all the response will be stored in the filename with specific name [inputvalue_teststepname.txt] in the folder name defined in the above part of the code.Also, you have to create a user defined property under the TestCase (test properties) and name it as "inputValue" [Case Sensitive]. And use this user defined property in all the teststep as the parameterized input. 
Updated : Code lines mentioned above are updated after recieving the feedback from the users. There was some problem with HTML tags, so the actual code was not visible. Now it should work.

8 comments:

  1. Hi Pradeep, you have a great deal of hold on the SoapUI APIs. I am trying to get some simple thing from long time. It is as below:
    In my testcases, I add a test request step and below it are say 3-4 assertions. After my test request step, I add a groovy script step which validates my request and writes some details to the external excel sheet.
    Now, when the test request is run, we see that some of the assertions are passing and some are failing.
    All I want is, i need to pass the details of all the failed assertions to my excel sheet.
    How can i do this in my groovy script?
    Please reply soon. You can send the reply to my mail mallikarjun.melagiri@gmail.com

    Thanks in advance!

    ReplyDelete
  2. Hi Mallikarjun,

    I haven't worked on such a scenario yet, however will try to implement the same asap.
    We can grab the name & status of the each assertion using groovy. So, I am thinking about getting the "failed assertion detail" ...i guess we can fetch it through 'AssertionError' class.

    i will give it a try. hope it works ;-)

    cheers!
    ~~ pradeep bishnoi ~~

    ReplyDelete
  3. Hi Pradeep,

    Thanks for your quick reply. I tried the following and was successful halfway:

    WsdlTestRequestStep wtrs = testRunner.testCase.getTestStepByName("");
    log.info wtrs.getAssertionStatus();

    But I think it gives the overall status of the assertions.
    But I need status for individual assertion.

    Please look into the methods of WsdlTestRequestStep and provide me the solution. Meanwhile, i too will give a try today.

    Thanks a lot!

    ReplyDelete
  4. Hi Mallikarjun,

    Lucky you, i shared the solution via new blog post. Do refer the same.

    http://pradeepbishnoi.blogspot.com/2011/06/groovy-7-getstatus-geterror-message-of.html

    Happy reading & keep sharing!
    Regards,
    ~~ Pradeep Bishnoi ~~

    ReplyDelete
  5. I have an XML, I need to run it with diff set of data 234 times and capture the responses how can i do i, i have the input values for the field in a text file.

    ReplyDelete
  6. can u provide code for multiple input values in request and should store response the same way in above code

    ReplyDelete
  7. hi,we provide online training & video tutorial for soapui
    for free videos refer
    http://soapui-tutorial.com/soapui-tutorial/introduction-to-webservices/

    ReplyDelete