Hello everyone
Please help me
I am new in Gatling
These parameter values must be static
school_reg, school_name, school_url and student_id
This parameter value must be dynamic, but both requests req_1 and req_2 are the same.
reg_date_par
These parameter values must be dynamic
reg_time, student_info, token_param
First, I must run the req_1 request
and i must get std_req_ids, and std_req_par parameter values
Second, I must run the req_2 request and I must use std_req_ids, std_req_par parameter values for this request
When I run a load test the parameter must generate dynamic
My code is there
package TestDemo;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.sun.tools.javac.code.Attribute;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
import javax.crypto.spec.SecretKeySpec;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import static com.google.common.base.Charsets.UTF_8;
public class SchoolTestCases extends Simulation {
{
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String school_reg = "Test Registration";
String school_name = "Test Name";
String school_url = "http://testscool.com";
String student_id = "000001";
String reg_date_par = format.format(Calendar.getInstance().getTime()) + new Random().nextInt(999999);
String reg_time = format.format(Calendar.getInstance().getTime());
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
md.update(String.valueOf(new Random().nextInt(999999)).getBytes());
byte[] digest = md.digest();
String student_info = DatatypeConverter
.printHexBinary(digest).toUpperCase().substring(0, 16).toLowerCase();
String text_all = school_reg.length() + school_reg + school_name.length() + school_name + school_url.length() + school_url + student_id.length() + student_id + reg_date_par.length() + reg_date_par + reg_time.length() + reg_time + student_info.length() + student_info;
String pass = "4554gdf5gdf5g4d5g4dgsd4gf5gfg1gfg";
byte[] bytes = BaseEncoding.base16().decode(pass.toUpperCase());
final SecretKey SHA1_KEY = new SecretKeySpec(bytes, "HmacSHA1");
String token_param = Hashing.hmacSha1(SHA1_KEY)
.newHasher()
.putString(text_all, UTF_8)
.hash()
.toString()
.toLowerCase();
HttpProtocolBuilder httpProtocol = http
.baseUrl("http://testscool.com")
.inferHtmlResources(AllowList(), DenyList(".*\\.js", ".*\\.css", ".*\\.gif", ".*\\.jpeg", ".*\\.jpg", ".*\\.ico", ".*\\.woff", ".*\\.woff2", ".*\\.(t|o)tf", ".*\\.png", ".*detectportal\\.firefox\\.com.*"))
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en-US,en;q=0.9,az;q=0.8,tr;q=0.7")
.upgradeInsecureRequestsHeader("1")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
Map<CharSequence, String> headers = new HashMap<>();
headers.put("sec-ch-ua", "Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105");
headers.put("sec-ch-ua-mobile", "?0");
headers.put("sec-ch-ua-platform", "macOS");
headers.put("sec-fetch-dest", "document");
headers.put("sec-fetch-mode", "navigate");
headers.put("sec-fetch-site", "none");
headers.put("sec-fetch-user", "?1");
ScenarioBuilder req_1 = scenario("MyComputerRecordedSimulation")
.exec(
http("request_1")
.post("/tst_reg")
.headers(headers)
.formParam("school_reg", school_reg)
.formParam("school_name", school_name)
.formParam("school_url", school_url)
.formParam("student_id", student_id)
.formParam("reg_date_par", reg_date_par)
.formParam("reg_time", reg_time)
.formParam("student_info", student_info)
.formParam("tokenisation", token_param)
.check(bodyString().saveAs("resp"))
.check(xpath("//std_req_ids").saveAs("std_req_ids"))
.check(xpath("//std_req_par").saveAs("std_req_par"))
.check(status().is(200))
.check(xpath("//desc").is("Success"))
.check(xpath("//action").is("OK"))
.check(xpath("//req").is("OK"))
)
.pause(2);
String reg_date_par_2 = format.format(Calendar.getInstance().getTime()) + new Random().nextInt(999999);
String reg_time_2 = format.format(Calendar.getInstance().getTime());
MessageDigest md_2 = null;
try {
md_2 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
md_2.update(String.valueOf(new Random().nextInt(999999)).getBytes());
byte[] digest_2 = md_2.digest();
String student_info_2 = DatatypeConverter
.printHexBinary(digest_2).toUpperCase().substring(0, 16).toLowerCase();
String text_all_2 = school_reg.length() + school_reg + school_name.length() + school_name + school_url.length() + school_url + student_id.length() + student_id + reg_date_par_2.length() + reg_date_par_2 + reg_time_2.length() + reg_timereg_time_2 + student_inforeg_time_2.length() + student_inforeg_time_2
byte[] bytes_2 = BaseEncoding.base16().decode(pass.toUpperCase());
final SecretKey SHA1_KEY_2 = new SecretKeySpec(bytes_2, "HmacSHA1");
String token_param_2 = Hashing.hmacSha1(SHA1_KEY_2)
.newHasher()
.putString(text_all_2, UTF_8)
.hash()
.toString()
.toLowerCase();
ScenarioBuilder req_2 = scenario("MyComputerRecordedSimulation_2")
.exec(
http("request_1")
.post("/tst_reg")
.headers(headers)
.formParam("school_reg", school_reg)
.formParam("school_name", school_name)
.formParam("school_url", school_url)
.formParam("student_id", student_id)
.formParam("reg_date_par", reg_date_par)
.formParam("reg_time", reg_time_2)
.formParam("student_info", student_info_2)
.formParam("tokenisation", token_param_2)
.formParam("std_req_ids", "#{std_req_ids}")
.formParam("std_req_par", "#{std_req_par}")
.check(bodyString().saveAs("resp"))
.check(status().is(200))
.check(xpath("//desc").is("Approved"))
.check(xpath("//action").is("OK"))
.check(xpath("//req").is("OK"))
)
.pause(2);
{
String admins = scenario("Admins").exec(req_1, req_2);
{
setUp(
admins.inject(rampUsers(2).during(10))
).protocols(httpProtocol);
}
}
}
}