Kalnius Absent Member.
Absent Member.
1149 views

Java code to generate a string and use it in the testing

Jump to solution

Hello,

During the test script (WEB - HTTP/HTML), one of the Actions registers a new customer. Since the web application does not allow duplicate IDs, I have a java code sampler which creates a unique customer ID.
Where do I put the java code and how do I store the string that was generated by the java code in a variable and use it during a POST inside an Action?

 

Hope to hear from you guys.

Regards,
Kalnius

0 Likes
1 Solution

Accepted Solutions
michaelgalos Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

If you have client side JavaSCRIPT which generates a unique number before sending a HTTP POST you can re-implement it into your Loadrunner script.

 

Encapsulate your code in a function called 'generateID() { }'

Save the code as 'UniqueID.js' or something similar into your script folder.


Then put this into your script:

web_js_run(
   "Code=generateID()",
   "ResultParam=uniqueid",
   SOURCES,
   "File=UniqueID.js", ENDITEM,
LAST);

 

This code above will load UniquieID.js, execute the generateID() function and save the output to the 'uniqueid' parameter.

Then just use {uniqueid} in your HTTP POST request.

6 Replies
Lancer_ Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

as far as I know, it's hardly to execute java code in c script, so maybe you should choose "Java Vuser" or "Java over Http" protocol instead.

thanks.

0 Likes
Raju_Learner Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

Hi,

 

You can parametrize that ID and set it Unique Number and put max supported length as you can.(i guess %09d)

From range : 1 to 999999999 

And you can concatenate this number with any string and pass it as ID.

 

If you want to use then do ,  Update value on EACH ITERATION and when out of values ABORT VUSER.

 

Thanks 

Raj

 

0 Likes
Kalnius Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

Lancer,

Thank you for the reply.
Since it is a relatively easy script, I would rather just keep it in the WEB - HTTP/HTML and translate it to C.

 

Raju,
Thank you for your contribution.

You see the ID is actually a personal code of lithuanian citizen so I cannot put just random unique numbers, it has to make sense. And the formula for that is:

If a personal code is ABCDEFGHIJK then:

S = A*1 + B*2 + C*3 + D*4 + E*5 + F*6 + G*7 + H*8 + I*9 + J*1

S is then divided by 11 and if the residue is not equal to 10, then that number is a personal control number K. If the residue is equal to 10, then the personal code is calculated using this:

S = A*3 + B*4 + C*5 + D*6 + E*7 + F*8 + G*9 + H*1 + I*2 + J*3

S is divided by 11 again, and if the residue is not 10, that number is a personal control number K then. If the residue is still 10, the control number K is 0.
 
The code is:
String yearF = "02";
String firstD = "4";
Random rand = new Random();
int randMonth = rand.nextInt(12) + 1;
int randDay = rand.nextInt(28) + 1;
int rand3 = 0;
String rMonth = "";
String rDay = "";
String r3 = "";
String id = "";
int last = 0;
done = 0;
while(done == 0)
	{
	rand3 = rand.nextInt(999) + 1;
	if(randMonth < 10)
		rMonth = "0" + Integer.toString(randMonth);
	else
		rMonth = Integer.toString(randMonth);
	if(randDay < 10)
		rDay = "0" + Integer.toString(randDay);
	else
		rDay = Integer.toString(randDay);
	if(rand3 < 10)
		r3 = "00" + Integer.toString(rand3);
	else
		{
		if(rand3 < 100)
			r3 = "0" + Integer.toString(rand3);
		else
			r3 = Integer.toString(rand3);
		}
	id = firstD + yearF + rMonth + rDay + r3;
	last = (Integer.parseInt(id.substring(0,1))*1 + Integer.parseInt(id.substring(1,2))*2 + Integer.parseInt(id.substring(2,3))*3 + Integer.parseInt(		id.substring(3,4))*4 + Integer.parseInt(id.substring(4,5))*5 + Integer.parseInt(id.substring(5,6))*6 + Integer.parseInt(id.substring(6,7))*7 + Integer.parseInt(id.substring(7,8))*8 + Integer.parseInt(id.substring(8,9))*9 + Integer.parseInt(id.substring(9,10))*1) % 11;
	if(last < 10)
		done = 1;
	}
String personalID = id + Integer.toString(last);
String lastN = "L" + personalID;
String firstN = "F" + personalID;
String birthD = "19" + yearF + "." + rMonth + "." + rDay;
vars.put("personalCode",personalID);
vars.put("lastNameN",lastN);
vars.put("firstNameN",firstN);
vars.put("documentNumber",personalID);
vars.put("birthDate",birthD);

String shN = "S" + r3;
String itN = "I" + r3;
String regNr = "0" + personalID.substring(1,11);
vars.put("registrationCodeL",regNr);
vars.put("registrationDocumentNumberL",regNr);
vars.put("fullNameL",firstN);
vars.put("shortNameL",shN);
vars.put("internationalNameL",itN);

 


Thanks,
Kalnius

0 Likes
michaelgalos Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

If you have client side JavaSCRIPT which generates a unique number before sending a HTTP POST you can re-implement it into your Loadrunner script.

 

Encapsulate your code in a function called 'generateID() { }'

Save the code as 'UniqueID.js' or something similar into your script folder.


Then put this into your script:

web_js_run(
   "Code=generateID()",
   "ResultParam=uniqueid",
   SOURCES,
   "File=UniqueID.js", ENDITEM,
LAST);

 

This code above will load UniquieID.js, execute the generateID() function and save the output to the 'uniqueid' parameter.

Then just use {uniqueid} in your HTTP POST request.

Kalnius Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

Thanks, Michael,

That did work for me, just had to make it into JS.

In case you ever need a Lithuanian personal code (for passport) generator, here it is:

function generatePersonalCode() {
	var MorF = Math.floor((Math.random() * 2) + 3);
	var year = Math.floor((Math.random() * 99) + 1);
	if(year<10){year="0"+year}
	var month = Math.floor((Math.random() * 12) + 1);
	if(month<10){month="0"+month}
	var day = Math.floor((Math.random() * 28) + 1);
	if(day<10){day="0"+day}
	var rand3nums = Math.floor((Math.random() * 999) + 1);
	if(rand3nums<10){rand3nums="00"+rand3nums}
	else if(rand3nums<100){rand3nums="0"+rand3nums}
	var personalCode = MorF+""+year+""+month+""+day+""+rand3nums;
	var done = 0;
	while(done == 0){
		last = (parseInt(personalCode.substring(0,1))*1 + parseInt(personalCode.substring(1,2))*2 + parseInt(personalCode.substring(2,3))*3 + parseInt(personalCode.substring(3,4))*4 + parseInt(personalCode.substring(4,5))*5 + parseInt(personalCode.substring(5,6))*6 + parseInt(personalCode.substring(6,7))*7 + parseInt(personalCode.substring(7,8))*8 + parseInt(personalCode.substring(8,9))*9 + parseInt(personalCode.substring(9,10))*1) % 11;
		if(last != 10){
			done = 1;
		}else if(last = 10){
		last = (parseInt(personalCode.substring(0,1))*3 + parseInt(personalCode.substring(1,2))*4 + parseInt(personalCode.substring(2,3))*5 + parseInt(personalCode.substring(3,4))*6 + parseInt(personalCode.substring(4,5))*7 + parseInt(personalCode.substring(5,6))*8 + parseInt(personalCode.substring(6,7))*9 + parseInt(personalCode.substring(7,8))*1 + parseInt(personalCode.substring(8,9))*2 + parseInt(personalCode.substring(9,10))*3) % 11;
		if(last != 10){done=1}
		}else if(last = 10){
		last = 0;
		done = 1;
		}
	}
	personalCode = personalCode+""+last;
	return personalCode;
}

 Thanks to all of you for the input!

Kalnius Absent Member.
Absent Member.

Re: Java code to generate a string and use it in the testing

Jump to solution

Updated version:

function generatePersonalInfo(){
	var MorF = Math.floor((Math.random() * 2) + 3);
	var year = Math.floor((Math.random() * 99) + 1);
	if(year<10){year="0"+year}
	var month = Math.floor((Math.random() * 12) + 1);
	if(month<10){month="0"+month}
	var day = Math.floor((Math.random() * 28) + 1);
	if(day<10){day="0"+day}
	var rand3nums = Math.floor((Math.random() * 999) + 1);
	if(rand3nums<10){rand3nums="00"+rand3nums}
	else if(rand3nums<100){rand3nums="0"+rand3nums}
	
	var DoB = 19+""+year+"."+month+"."+day;
	var personalCode = MorF+""+year+""+month+""+day+""+rand3nums;
	last = (parseInt(personalCode.substring(0,1))*1 + parseInt(personalCode.substring(1,2))*2 + parseInt(personalCode.substring(2,3))*3 + parseInt(personalCode.substring(3,4))*4 + parseInt(personalCode.substring(4,5))*5 + parseInt(personalCode.substring(5,6))*6 + parseInt(personalCode.substring(6,7))*7 + parseInt(personalCode.substring(7,8))*8 + parseInt(personalCode.substring(8,9))*9 + parseInt(personalCode.substring(9,10))*1) % 11;
	if(last == 10){
		last = (parseInt(personalCode.substring(0,1))*3 + parseInt(personalCode.substring(1,2))*4 + parseInt(personalCode.substring(2,3))*5 + parseInt(personalCode.substring(3,4))*6 + parseInt(personalCode.substring(4,5))*7 + parseInt(personalCode.substring(5,6))*8 + parseInt(personalCode.substring(6,7))*9 + parseInt(personalCode.substring(7,8))*1 + parseInt(personalCode.substring(8,9))*2 + parseInt(personalCode.substring(9,10))*3) % 11;
		if(last == 10){
			last = 0;
		}
	}
personalCode = personalCode+""+last;
var regNo = "0"+personalCode.substring(1, 11);
var MorFstr = "";
if(MorF==3){
	MorFstr = "M";
}else if(MorF==4){
	MorFstr = "F";
}
var array = [personalCode, DoB, regNo, rand3nums, MorFstr];
return array;
}

 personalCode - 11 digit lithuanian citizen personal code
DoB - date of birth
regNo - just a mock number (no value)

rand3nums - *******XXX* in the personalCode

MorFstr - string that represents Male or Female (returns 'M' or 'F'). 3 - male born in XXth century, 4 - female born in XXth century.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.