poniedziałek, 4 marca 2013

Better version of RANDOM PESEL in Selenium IDE & RANDOM PESEL, NIP & REGON in Jmeter



Mówią , że postęp nas bogaci co wiek;
- Bardzo mi to jest miło i przyjemnie -
Niestety! co dnia mniej cieszę się ze mnie,
Śmiertelny człowiek!

Surprisingly good translation from Google:

They say that the progress of our rich what age;
- I'm it's nice and pleasant -
Alas! Every day I am less of me,
Mortal man!



Well, being aware that there are more important things in the world than improving scripts we’re going to do this.

I’m going to show better version of javascript code which generates random PESEL for Selenium IDE. Better than shown in post from March ’12. Now it should work for PESELs from a range of years from 1800 to 2299, it will generate random birth date using days from a  range from 0 to 30 (instead of 28) days for months other than February and, most importantly, I get rid off many useless, rubbish pieces of code so now it’s more concise:


Selenium.prototype.doTypeRandomPesel = function(locator){

var initialDate = new Date();
var curr_year = initialDate.getFullYear();

var curr_yearMinus100 = curr_year - 100;

var curr_yearMinus19 = curr_year - 18;
var curr_yearMinus19_2 = curr_yearMinus19 - (curr_year - 100);

var rok = Math.floor(curr_yearMinus19_2*Math.random()+curr_yearMinus100);
var miesiac = Math.floor(12*Math.random()+1);

if(miesiac == 2){
var dzienNr = Math.floor(28*Math.random()+1);
}else{
var dzienNr = Math.floor(30*Math.random()+1);
}

var dzien = dzienNr.toString();

var MyPesel = new Array();
MyPesel[0] = rok.toString().substring(2,3);
MyPesel[1] = rok.toString().substring(3,4);
    
if(rok.toString().substring(0,2) == "19") var dodajMiesiac = 0;
if(rok.toString().substring(0,2) == "18") var dodajMiesiac = 80;
if(rok.toString().substring(0,2) == "20") var dodajMiesiac = 20;
if(rok.toString().substring(0,2) == "21") var dodajMiesiac = 40;
if(rok.toString().substring(0,2) == "22") var dodajMiesiac = 60;

var miesiacNr = parseInt(miesiac + dodajMiesiac);
var newMiesiac = miesiacNr.toString();

if(newMiesiac.length == 1){
     MyPesel[2] = 0;
     MyPesel[3] = newMiesiac;
     }else{
     MyPesel[2] = newMiesiac.substring(0,1);
     MyPesel[3] = newMiesiac.substring(1,2);
     }

if(dzien.length == 1){
     MyPesel[4] = 0;
     MyPesel[5] = dzien;
     }else{
     MyPesel[4] = dzien.substring(0,1);
     MyPesel[5] = dzien.substring(1,2);
     }

MyPesel[6] = Math.round(Math.random() * (9 - 0));
MyPesel[7] = Math.round(Math.random() * (9 - 0));
MyPesel[8] = Math.round(Math.random() * (9 - 0));

var myPlec = Math.round(Math.random() * (9 - 0));
MyPesel[9] = myPlec;

var MySuma = parseInt(MyPesel[0]*1+MyPesel[1]*3+MyPesel[2]*7+MyPesel[3]*9+MyPesel[4]*1+MyPesel[5]*3+MyPesel[6]*7+MyPesel[7]*9+MyPesel[8]*1+MyPesel[9]*3);     
var MyControl = 10 - MySuma%10;
var MyControlNr = (MyControl == 10)? 0 : MyControl;
MyPesel[10] = MyControlNr;

pesel_gen = MyPesel.join('');

this.doType(locator, pesel_gen);
}


Copy/ paste above to your user-extensions.js file (see post from March ‘12) and you’ll have new command typeRandomPesel.


And now version for Jmeter. In Jmeter we’re going to use Java. Exactly not Java but BeanShell – kind of scripting version of Java. Anyway syntax is the same. To get random PESEL (as well as NIP and REGON) add BeanShell sampler to your test plan:


Then paste below code to it:

Calendar cal = Calendar.getInstance();
int curr_year = cal.get(Calendar.YEAR);

int curr_yearMinus100 = curr_year - 100;

int curr_yearMinus19 = curr_year - 18;   
                    
Random r = new Random(); 
               
int yearRand_0 = r.nextInt((curr_yearMinus19 + 1) - curr_yearMinus100) + curr_yearMinus100;
String yearRand_1 = String.valueOf(yearRand_0);
int[] year = new int[2];
if(yearRand_0 < 10){
     year[0] = 0;
     year[1] = yearRand_0;
     }else{
     String yearRand_2 = yearRand_1.substring(2,3);
     String yearRand_3 = yearRand_1.substring(3,4);
     year[0] = Integer.parseInt(yearRand_2);
     year[1] = Integer.parseInt(yearRand_3);
     }
         
int monthRand_0 = r.nextInt((12 + 1) - 1) + 1;
String monthRand_1 = String.valueOf(monthRand_0);
int[] month = new int[2];
if(monthRand_0 < 10){
     month[0] = 0;
     month[1] = monthRand_0;
     }else{
     String monthRand_2 = monthRand_1.substring(0,1);
     String monthRand_3 = monthRand_1.substring(1,2);
     month[0] = Integer.parseInt(monthRand_2);
     month[1] = Integer.parseInt(monthRand_3);
     }
         
int dayRand_0 = r.nextInt((28 + 1) - 1) + 1;  
String dayRand_1 = String.valueOf(dayRand_0);
int[] day = new int[2];
if(dayRand_0 < 10){
     day[0] = 0;
     day[1] = dayRand_0;
     }else{
     String dayRand_2 = dayRand_1.substring(0,1);
     String dayRand_3 = dayRand_1.substring(1,2);
     day[0] = Integer.parseInt(dayRand_2);
     day[1] = Integer.parseInt(dayRand_3);
     }   
               
int pes7 = r.nextInt((9 + 1) - 0) + 0;
int pes8 = r.nextInt((9 + 1) - 0) + 0;
int pes9 = r.nextInt((9 + 1) - 0) + 0;   
int pes10 = r.nextInt((9 + 1) - 0) + 0;  
               
int sum = year[0]*1+year[1]*3+month[0]*7+month[1]*9+day[0]*1+day[1]*3+pes7*7+pes8*9+pes9*1+pes10*3;
int control = 10 - sum%10;
int controlNr = (control == 10)? 0 : control;
int pes11 = controlNr;
         
String p1 = String.valueOf(year[0]);
String p2 = String.valueOf(year[1]);
String p3 = String.valueOf(month[0]);
String p4 = String.valueOf(month[1]);
String p5 = String.valueOf(day[0]);
String p6 = String.valueOf(day[1]);
String p7 = String.valueOf(pes7);
String p8 = String.valueOf(pes8);
String p9 = String.valueOf(pes9);
String p10 = String.valueOf(pes10);
String p11 = String.valueOf(pes11);
         
String pesel = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11;

vars.put("peselSearchVar", pesel);
 

Look at the last line. The variable you can use in your script is peselSearchVar, e.g.:

Do the same with code for random NIP…:


Random r = new Random();

int n1 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n2 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n3 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n4 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n5 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n6 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n7 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n8 = Math.round(r.nextInt((9 + 1) - 0) + 0);
int n9 = Math.round(r.nextInt((9 + 1) - 0) + 0);

int sum = n1*6 + n2*5 + n3*7 + n4*2 + n5*3 + n6*4 + n7*5 + n8*6 + n9*7;

int control = sum%11;
int n10 = (control == 10)? 0 : control;

String nip = String.valueOf(n1) + String.valueOf(n2) + String.valueOf(n3) + String.valueOf(n4) + String.valueOf(n5) + String.valueOf(n6) + String.valueOf(n7) + String.valueOf(n8) + String.valueOf(n9) + String.valueOf(n10);

vars.put("nipVar", nip);
 

…and REGON:



Random r = new Random();

boolean generate = true;
int reg1; int reg2; int reg3; int reg4; int reg5; int reg6; int reg7; int reg8; int reg9; int sum; int control; int woj;

while (generate){
reg1 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg2 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg3 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg4 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg5 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg6 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg7 = Math.round(r.nextInt((9 + 1) - 0) + 0);
reg8 = Math.round(r.nextInt((9 + 1) - 0) + 0);

sum = reg1*8 + reg2*9 + reg3*2 + reg4*3 + reg5*4 + reg6*5 + reg7*6 + reg8*7;

control = sum%11;
reg9 = (control == 10)? 0 : control;

woj = reg1*10 + reg2;

if (((woj != 0) && (woj%2 == 0) && (woj > 34)) || ((woj != 0) && (woj%2 == 1) && (woj > 97))){
generate = true;
}else{
generate = false;
}
}

String regon = String.valueOf(reg1) + String.valueOf(reg2) + String.valueOf(reg3) + String.valueOf(reg4) + String.valueOf(reg5) + String.valueOf(reg6) + String.valueOf(reg7) + String.valueOf(reg8) + String.valueOf(reg9);

vars.put("regonVar", regon);


Next time I’ll post some code for random NIP and REGON in Selenium IDE + I’ll show how to get random PESEL in Loadrunner. Stay tuned.

czwartek, 7 lutego 2013

Comparing some dates

Task is to compare two dates in format DD/MM/YYYY, e.g. 16/04/2010 and 17/04/2010 and say which is earlier, using Selenium IDE.

We’ll use several javascript snippets. First of all we must remember that for javascript all dates are in format MM/DD/YYYY. So we have to switch places for DD and MM:



Result for above will be 04/16/2010.


Same actions for second date:



Now we’ve got two dates:
04/16/2010
04/17/2010

Time to transform them into more comparable form.



In above example parsing date means converting it into number of milliseconds which passed from 01/01/1970 until that date. We end up with two numbers:
dateMilliA = "1271368800000"
dateMilliB = "1271455200000"

Compare them and print the result.


Syntax for comparison:
store | javascript{(condition)?returnThisWhileItsTrue:returnThisWhileItsNotTrue} | variableHoldingResult


Whole script:


And version to copy:

 //additional variables
store | 0 | counter0
store | 1 | counter1
store | 2 | counter2
//store 1st date to compare
store | 16/04/2010 | dateA
//switch places for DD and MM
store | javascript{storedVars['dateA'].split('/')[storedVars['counter0']]} | dateDAYA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter1']]} | dateMONTHA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter2']]} | dateYEARA
store | javascript{storedVars['dateMONTHA'] + "/" + storedVars['dateDAYA'] + "/" + storedVars['dateYEARA']} | dateToParseA
//store 2nd date to compare
store | 17/04/2010 | dateB
//and switch places for 2nd date
store | javascript{storedVars['dateB'].split('/')[storedVars['counter0']]} | dateDAYB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter1']]} | dateMONTHB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter2']]} | dateYEARB
store | javascript{storedVars['dateMONTHB'] + "/" + storedVars['dateDAYB'] + "/" + storedVars['dateYEARB']} | dateToParseB
//transform dates into milliseconds
store | javascript{Date.parse(storedVars['dateToParseA']);} | dateMilliA
store | javascript{Date.parse(storedVars['dateToParseB']);} | dateMilliB
//compare parsed dates
store | javascript{(storedVars['dateMilliA'] < storedVars['dateMilliB'])?storedVars['dateA']:storedVars['dateB']} | result
//show result
runScript | javascript{alert("Earlier date is " + storedVars['result'] + ".")}
 |