Forum Discussion

Nagravision_SA__1's avatar
12 years ago

How to search for a value in .txt file using groovy script.

Hello,

Actually i have a *.txt Data source file which contains many rows and in each row there is a data something like ID,StartAvailability,EndAvailability. Now in this file i want to search for a particular ID. Once this found then i want to update other values of the same row something StartAvailability or EndAvailability etc. After that i want to set the data source current row to 1. Then again in the loop go through for another ID. I don't know if we can do this without groovy script in Soapui Pro. If yes please tell me how i can do in the SoapUi Pro. if not then please tell me also how i can do via groovy script. I am new in groovy script.

Thanks !!

Regards

4 Replies

  • Cizo89's avatar
    Cizo89
    Frequent Contributor
    Hi,

    this is usually done by Groovy scripts or with a combination of Groovy scripts and DataSource, DataSource Loop and DataSink TestSteps.
    But since you have the .txt file and after a quick look at your requirements I think this couldn't be done, because if I'm not wrong you can only overwrite all data in that file or just append some data to the end of the file, not amend some values in the file.
    All the other things in your requirements could be done with a few steps and scripts.

    Try to store your data to an Excel file and with this configuration you can easily amend existing data...or maybe the SmartBear Support guys will come up with some pretty good solution.

    Regards,
    Marek
  • Hi Marek,

    Thanks for your reply !

    Actaully i want to store the data in excel. But there is a following issue in the soapUi Pro. That is why i choose txt file.

    viewtopic.php?f=2&t=22085

    Can you please give me an idea how i can do using excel ?

    Thanks in Advance Marek.


    Regards
  • Cizo89's avatar
    Cizo89
    Frequent Contributor
    Hi,

    I've tried this in my SoapUI Pro and I found out that there is a problem which will block you - http://forum.soapui.org/viewtopic.php?f=4&t=22213&sid=ba7f6da2d983c11c9c61513dfb5fb928.

    So, as soon as the SoapUI devs fix that potentional bug, you could try this:
    1. Configure the DataSource for your excel file
    2. Put this into Groovy script TestStep:

    def dataSource = testRunner.testCase.testSteps["DataSource"]
    def dataSink = testRunner.testCase.testSteps["DataSink"]

    String Id = context.expand( '${DataSource#Id}' )
    String StartAvailability = context.expand( '${DataSource#StartAvailability}' )
    String EndAvailability = context.expand( '${DataSource#EndAvailability}' )

    if (Id == "2"){
    dataSink.setPropertyValue("Id", "edited_Id")
    dataSink.setPropertyValue("StartAvailability", "edited_sa2")
    dataSink.setPropertyValue("EndAvailability", "edited_ea2")

    //I'm using the TestCase property, because the DataSink class doesn't have a setStartAtCell method
    //because I'm using A1 row as the header
    testRunner.testCase.setPropertyValue("rowToUpdate", "A" + (dataSource.getCurrentRow() + 1).toString())
    testRunner.runTestStepByName("DataSink")

    dataSource.restart(testRunner, context)
    }

    3. Create DataSource Loop TestStep (target step will be the Groovy script)
    4. Create, configure (both "File" and "Out File" fields and put Property Expansion to the Start at Cell field) and disable DataSink TestStep

    I hope it will help you

    Regards,
    Marek
  • I believe this is a bug (unless we want to limit writes by design, but I don't think so).

    Until we fix it, you can use Groovy scripts to change values in the file. Unless the file is being used concurrently by another process or task (which you should probably avoid anyway), rewriting the file from scratch is not such a big problem, and is probably quite easy to do in Groovy. For example. Here's how you'd replace all occurrences of the String "Enzo" with "Mario" in the file "data.txt":

    def dataFile = new File("data.txt")
    dataFile.text = dataFile.text.replaceAll("Enzo", "Mario")

    Ahhh, it's so much nicer to do these things in Groovy than in Java! :-)

    Of course, you need to be careful so you don't modify too much, so in the end you may want to read and parse lines individually. In that case you may want to use code like this.

    def dataFile = new File("data.txt")
    def buffer = new StringBuilder()
    dataFile.eachLine { line ->
    // inside this loop, the line will be available in the variable line, so you can process them individually, e.g.
    def processedLine = "*** " + line + "\r\n"
    buffer.append(processedLine)
    }
    dataFile.text = buffer.toString()

    Kind regards,
    Manne