Road to reading data from text file in SoapUI using groovy script

In this post, I am going to show you how to read data from text file in SoapUI. SoapUI Pro has some advance feature which is not in SaopUI as data fetching from external sources so in SoapUI we use Groovy script for that. Following are the peace of groovy script code for reading data from text file.

1. Reading all data from text file.
//reading all txt file at once

File file = new File("D://user.txt")
fileContent = file.getText()                 
log.info fileContent

2. Reading data line by line from text file.
//reading text line by line

File file1 = new File("D://user.txt")
List textLine = file1.readLines()
log.info textLine

3. Reading data randomly of any line from text file.
//reading text randon line number

File file2 = new File("D://user.txt")
List textLine2 = file2.readLines()
rowIndex  =  Math.abs(new Random().nextInt() % 4 + 1)
log.info textLine2[rowIndex]

3 comments:

  1. can you elaborate the meaning of each and every keyword in the above code? Actually I want to understand the above code

    ReplyDelete
  2. These work! They are the only scripts I've come across that have worked the first time as is.

    I wonder if the contributor is still reading this blog? It would be great to see it read from Excel and write to Requests to form a data driven framework for SoapUI using the free version.

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution