Navigation: JunoRestClient > Advanced >

CSV file

 

Send comments on this topic.

 

 

Using CSV File

You can use CSV file and groovy script to run one or more service with various test scenarios or use cases.

 

 

 

Step 1: Create SampleData.csv file in a folder C:/temp.

 

The first row describes column headers and the rest of rows indicate the row data.

 

C:/temp/SampleData.csv

UseCase,AUTHORIZATION,SERVER_NAME

UseCase-1,I6IKPXVCJ9ISINEIJIUZI1NIOR5CCYJHBGCI,http://jsonplaceholder.typicode.com

UseCase-2,IJIUZI1NIOR5CCI6IKPXVCJ9ISINEYJHBGCI,http://jsonplaceholder.typicode.com

 

Step: 2 Add a Groovy Script with the following.

 

import org.apache.commons.csv.CSVParser

import java.nio.file.Paths;

import static org.apache.commons.csv.CSVFormat.*

 

def csvFileName = "C:/temp/SampleData.csv" ;

def listing = []

Paths.get(csvFileName).withReader { reader ->

    CSVParser csv = new CSVParser(reader, DEFAULT.withHeader() )

 

    for (record in csv.iterator()) {

        def row = record.toMap();

        binding.setVariable( "SERVER_NAME", row.SERVER_NAME);

        binding.setVariable( "AUTHORIZATION", row.AUTHORIZATION);

        app.run("Service-1");

   }

}          

 

 

 

"Service-1" is the name of a REST service in JunoRestClient project. You can replace with the service name of your choice.

 

 

Step 3: Add a REST service "Service-1" and change the following.

 

Insert the following in URL text field.

 ${SERVER_NAME}/posts

 

 

 

Insert the following Request Headers Multi-line Text Field.

 Authorization: Bearer ${AUTHORIZATION}

 

 

 

Insert the following in Request Payload field.

 {

 

          "token" : "${AUTHORIZATION}"

 }

 

 

 

 

Step 4: Now Select both groovy script and service. Click on [Run Services and Scripts] button.

 

 

Step 5: At runtime, the groovy script reads each row from .csv file and replace SERVER_NAME, AUTHORIZATION variable in URL, headers and payload.

 

 

Step 6: Click on [Generate Report] from Run Manager dialog.

 

 

Compilation Errors

 

12/25/2016 12:46:26 : org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script1.groovy: 1: unable to resolve class org.apache.commons.csv.CSVParser

 @ line 1, column 1.

   import org.apache.commons.csv.CSVParser

 

 

If you come across compilation errors similar to the above, it is probably due to missing groovy extension library(s).

The above groovy script uses CSVParser from apache commons-csv-1.4.jar file.

 

Using 3rd Party Groovy Script Library(s)

 

In order to use the 3rd party groovy library within a groovy script, please follow the steps outlined below.

 

Include the 3rd party library via [Preferences | Library Settings] page.

Click [Update Class Path] button in [Preferences | Library Settings] page.

 

 

 

See How to use 3rd Party Groovy Library

 

 

 

 

 

Copyright © 2016 junorestclient.com

www.JunoRestClient.com