Groovy III – text file as Data Source for input

Posted: May 23, 2011 in context, groovy, soapUI, testrunner
Tags: , , , , , , , ,
/*
@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.

Advertisements
Comments
  1. Aaron says:

    Hello PB,I have a test case, need excel file as data source. the first, step1 is a request, the response will return and contains 10 the same nodes, each node contains some values. my work is that save these values in excel, for example: I fetchs 5 values from each node, i need to save in 5 cells in excel. if so, in excel will contains 10 lines, each lines contains 5 values. (Ohhhhhh,,,the long description,,,,,,O(∩_∩)O), and then step2, I will treat the 10 lines as data source for subsequent test step. step 3 is a request, and will be populated with one line data from excel.step 4 is data source loop. so if the result is right at the first time, then run the second line data, loops 10 times.
    that’s my thought, i’d like to know how to save data in excel and easily get these values as data source. of course, if you give me some suggestion,I’m looking forward hearing from you….

    thanks a lot,
    Aaron

  2. Aaron says:

    btw, using createFolder.mkdirs() to create new folder would be better, i think. thanks

  3. pranai says:

    I am recieveing the following error using the above code I know it will be a silly mistake burt i still need to work it out please send me the solution to pranainandan08@gmail.com

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script3.groovy: 4: expecting EOF, found ‘:’ @ line 4, column 20. def folderName = “D:/Automate/response” //to store the resonse ^ org.codehaus.groovy.syntax.SyntaxException: expecting EOF, found ‘:’ @ line 4, column 20. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:107) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:163) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:839) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(Compilation

    • Hi,

      How about replacing
      def folderName = “D:/Automate/response” //to store the resonse

      with

      def folderName = “D:\\Automate\\response” //to store the resonse

      Let us know, if it works.

      Best Regards,
      /Pradeep Bishnoi

      • Nathalie says:

        Hi

        I am getting errors either way when trying to create the file

        Wed Jun 10 12:47:27 CDT 2015:ERROR:An error occurred [startup failed:
        Script7.groovy: 11: unexpected char: ‘\’ @ line 11, column 21.
        def folderName = “C:\\Users\\sapuser02\\Documents\\nath” //to store the resonse
        ^
        org.codehaus.groovy.syntax.SyntaxException: unexpected char: ‘\’ @ line 11, column 21.
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:135)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)

        OR

        Wed Jun 10 12:41:36 CDT 2015:ERROR:An error occurred [startup failed:
        Script4.groovy: 11: expecting EOF, found ‘:’ @ line 11, column 20.
        def folderName = “C://Users/sapuser02/Documents/nath.out” //to store the resonse
        ^
        org.codehaus.groovy.syntax.SyntaxException: expecting EOF, found ‘:’ @ line 11, column 20.
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:140)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)

        Here is the script

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

        def folderName = “C:\\Users\\sapuser02\\Documents\\nath” //to store the resonse
        def createFolder = new File(folderName)
        createFolder.mkdir()

        def testSuite = testRunner.testCase.testSuite.project.testSuites[“Testing methods”]
        def testCase = testSuite.getTestCaseAt(0)
        def testStep1 = testCase.getTestStepCount()

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

        File tempFile = new File(“C://Users/sapuser02/Documents/nath.txt”)
        List lines = tempFile.readLines()
        log.info lines
        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(“date”, lines[i++])
        inputValue = context.expand( ‘${#TestCase#date}’ )
        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++
        }

        Thanks for your help

  4. pranai says:

    Hi I am able to save the response in a txt file can tell me the groovy steps to parse the xml and store the values of each tags of response in excel I am really stuck up here it would be a great help.

  5. pranai says:

    Hi Pradeep,

    Thanks a lot for your response, i would request you to please tell me the code to write to an existing excel file in groovy It would be a great help.

  6. Himanshu says:

    Hi Pradeep Iam getting the following error when i run this code

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script17.groovy: 12: unable to resolve class XmlHolder @ line 12, column 15. def xHolder = new XmlHolder(testCase.getTestStepAt(0).getProperty(“Response”).getValue()) ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class XmlHolder @ line 12, column 15. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:148) at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:229) at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:239) at org.codehaus.groovy.control.ResolveVisitor.transformConstructorCallExpression(ResolveVisitor.java:1069) at

    Can u suggest me, what should be the sol..?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s