Register new customer
Add a new simple controller. Right click on it and add new HTTP Request.

Edit the HTTP Request name to Create New Customer

Then set following configuration:
protocol[http]: https
Server Name or IP: ${baseUrl}
Port Number: 443
Method: POST
Path: /customer/api/v1/create
Then click on Body Data and add this script:
{
"id": ${id},
"name": "${name}",
"email": "${email}",
"address": "Dhaka,Bangladesh",
"phone_number":"${phone_number}}"
}

Then ctrl+s to save the configuration
After saving the configuration, HTTP Request name will be changed to your given name

Generate Random Number
Now right click on Simple controller and click on Add > Config Element> Random Variables

Drag and move the Random Variable before the Create Customer HTTP Request
Then add following configuration:

You can set minimum and maximum anything as your wish
We moved the Random variable before the “create new customer” HTTP Request so that we can generate the ID before and use it while creating new customers.
JR223 PreProcessor
JR223 PreProcessor is used to do something before sending a HTTP Request.
The reason for adding pre-processor is to generate some random values and create different customers.
Right click on the simple controller and click on Add > Pre Processors > JR223 PreProcessor

Drag and move it to before “Create New Customer” HTTP Request
Now add the following groovy codes. PreProcessor by default supports groovy code. Also, you can use Java or JavaScript
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
import groovy.json.JsonSlurper
log.info('----- Calling user info generating API -----')
def httpClient = HttpClientBuilder.create().build()
def httpGet = new HttpGet("https://api.namefake.com/english-united-states/male")
def httpResponse = httpClient.execute(httpGet)
def responseData = EntityUtils.toString(httpResponse.getEntity())
log.info('----- Response -----')
log.info(responseData)
slurperresponse = new JsonSlurper().parseText(responseData)
log.info('----- Set to environment variables-----')
vars.put("name",slurperresponse.name)
vars.put("email",slurperresponse.email_u+"@testmail.com")
vars.put("phone_number",slurperresponse.phone_w)

Now ctrl+s to save the script. Then clear the previous history and run the project
You will see that new customer has been created through the API

Now go to the Request tab. You will see every time will generate new info for the customers.

Then move the “view Results Tree” at the last. So that it beautifies our project.

Beanshell post-processor
Beanshell post-processor is used to do something after getting the response. In this segment, we will save our response in a CSV file
Right click on Simple Controller and click on Add > Post Processors > Bean Shell PostProcessor

Add following codes:
import java.io.FileWriter;
import java.io.IOException;
FileWriter writer = null;
String location = "mycustomers.csv";
log.info("=============Started wrting Data to CSV file=======");
try {
writer = new FileWriter(location, true);
String id = vars.get("id");
String name = vars.get("name");
String email = vars.get("email");
String phone_number = vars.get("phone_number");
writer.append(id);
writer.append(',');
writer.append(name);
writer.append(',');
writer.append(email);
writer.append(',');
writer.append(phone_number);
writer.append('\n');
log.info(id+" "+name+" "+email+" "+address+" "+phone_number);
log.info("CSV file is created...");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
writer.close();

Now save and run the project for 3/4 or multiple times

You will see “mycustomers.csv” file has been generated in the bin folder

Open the file. you will see every customer record has been stored in the CSV file

Leave a Reply