poniedziałek, 5 marca 2012

Generating random PESEL

This post will be useful mostly for Polish users. It’s about generating random PESEL number with a single command in Selenium IDE. The goal is to provide you with ready to use code so that you don’t have to invent a wheel again.
I go to sleep. Don’t sit up late.


So don’t sit up late just start using it. Two remarks:

  1. Javascript code used to create Selenium command generating random PESEL was found by me on this website: http://itraport.pl/tools/generator-numerow-pesel/. I modified it to run it on Selenium IDE but most credits goes to guy who developed it who is, I believe, Tomasz Wesołowski.
  2. To use it:
    1. Create (if you don’t have already) new .js file in text editor, name it e.g. “user-extensions.js”
    2. Place the code in this file.
    3. Add path to the file in Selenium > Options > Options, e.g.
  

 
    1. Click “OK” and re-open Selenium
    2. New command “TypeRandomPesel” will be available
    3. Use it like this:
 






























Here’s the code:


Selenium.prototype.doTypeRandomPesel = function(locator){

var initialDate = new Date();
var curr_year = initialDate.getFullYear();
//below string - describes lower boundary of years used to generate PESEL; in this case it's minus 100 years from present year
var curr_yearMinus100 = curr_year - 100;
//below two strings - describes upper boundary of years used to generate PESEL; in this case it's minus 19 years from present year
//in first string it's "curr_year - 18" because string generating random number will add +1 so "curr_year - 18" in fact means "curr_year - 19"
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);

var dzien = Math.floor(28*Math.random()+1);

/*
var rok = document.getElementById('rok');

var miesiac = document.getElementById('miesiac');

var dzien = document.getElementById('dzien');

var plec_k = document.getElementById('plec_k');
var plec_m = document.getElementById('plec_m');
var plec_l = document.getElementById('plec_l');
var pesel_gen = document.getElementById('pesel_gen');
pesel_gen.innerHTML = "";
var data_urodzenia_gen = document.getElementById('data_urodzenia_gen');
data_urodzenia_gen.innerHTML = "";
var plec_gen = document.getElementById('plec_gen');
plec_gen.innerHTML = "";
*/

    if(rok!=''){
        if( rok<1800 || rok >2299 || rok == null){
            pesel_gen.innerHTML = "Podano nieprawidłowy rok";
            return false;
            }else{
            var MyRok = rok;
            }
    }else{
    var MyRok = Math.round(Math.random() * (2299 - 1899)) + 1899;
    }

    if(miesiac!=""){
        if( miesiac<1 || miesiac >12 || miesiac == null){
            pesel_gen.innerHTML = "Podano nieprawidłowy miesiąc";
            return false;
            }else{
            var MyMiesiac = miesiac;
            }
    }else{
    var MyMiesiac = Math.round(Math.random() * (12 - 1)) + 1;
    }

    if(dzien!=''){
        if(dzien<1 || dzien >31 || dzien == null){
            pesel_gen.innerHTML = "Podano nieprawidłowy dzień?";
            return false;
            }else{
            var MyDzien = dzien;
            }
    }else{
    var MyDzien = Math.round(Math.random() * (28 - 1)) + 1;
    }
   
    var d = new Date();
    d.setFullYear(MyRok);
    d.setMonth(MyMiesiac-1);
    d.setDate(MyDzien);
   
   
    var MyPesel = new Array();
    MyPesel[0] = d.getFullYear().toString().substring(2,3);
    MyPesel[1] = d.getFullYear().toString().substring(3,4);
   
    if(d.getFullYear().toString().substring(0,2) == "19") var dodajMiesiac = 0;
    if(d.getFullYear().toString().substring(0,2) == "18") var dodajMiesiac = 80;
    if(d.getFullYear().toString().substring(0,2) == "20") var dodajMiesiac = 20;
    if(d.getFullYear().toString().substring(0,2) == "21") var dodajMiesiac = 40;
    if(d.getFullYear().toString().substring(0,2) == "22") var dodajMiesiac = 60;
   
   
    var newMiesiac = d.getMonth();
    newMiesiac++;
    newMiesiac = parseInt(newMiesiac+dodajMiesiac);
    newMiesiac = newMiesiac.toString();
   
    if(newMiesiac.length == 1){
            MyPesel[2] = 0;
            MyPesel[3] = newMiesiac;
            }else{
            MyPesel[2] = newMiesiac.substring(0,1);
            MyPesel[3] = newMiesiac.substring(1,2);
            }
    var newDzien = d.getDate().toString();
       
    if(newDzien .length == 1){
            MyPesel[4] = 0;
            MyPesel[5] = newDzien;
            }else{
            MyPesel[4] = newDzien.substring(0,1);
            MyPesel[5] = newDzien.substring(1,2);
            }
   
    Math.round(Math.random() * (999 - 100)) + 100;
   
    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)) + 0;
    if(plec_k = 1){
   
        if(myPlec==0) MyPesel[9]=myPlec;
        else MyPesel[9] = parseInt((myPlec+1)%10)
   
    }else if(plec_m = 1){
        if(myPlec==1) MyPesel[9]=myPlec;
        else MyPesel[9] = parseInt((myPlec+1)%10)
   
    }else MyPesel[9]=myPlec;
   
//    if((MyPesel[9]%2) == 0) plec_gen.innerHTML="Kobieta";
//    if((MyPesel[9]%2) == 1) plec_gen.innerHTML="Mężczyzna";

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.innerHTML = MyPesel.join('');
    pesel_gen = MyPesel.join('');
//    data_urodzenia_gen.innerHTML = d.getFullYear() + '-' + parseInt(d.getMonth()+1) + '-' + MyPesel[4] + MyPesel[5];

//var "pesel_gen" contains generated PESEL
this.doType(locator, pesel_gen);

}
 

środa, 1 lutego 2012

Xpath – count options in dropdown

Often we want our script to flexibly react on what’s appearing on a web page. The way to achieve that is to count how many results, rows in a table, pages etc. is present at exact moment, because numbers don’t lie…


(400 goldfishes = 1200 fulfilled wishes,
Numbers don’t lie”)


Our example includes random choice of brand and model of car on http://www.motors.ebay.com/.

We’ll use command “storeXpathCount” which counts number of xml nodes. Web page is not an xml document but xpath (XML Path Language) allows to search (make queries) through web page as it were an xml document. Here you can find more about xpaths: http://www.w3schools.com/xpath/xpath_intro.asp.



And these are our dropdowns:
























The trick is that when you choose brand then number of options in the next dropdown changes. After you record selecting brand from first dropdown the next step is to change locator:



Now change command from “select” into “storeXpathCount” and in third column (Value) type name of variable which will store the result:


If you run script at this moment, value of “brandCount” would equal 1 because it counts number of dropdowns with id “_mak_new” (and there’s only one). You should modify target like this:

//select[@id='_mak_new']/option

Now it counts number of options in this dropdown.

Below is whole script which firstly counts number of options in first dropdown, randomly chooses one of them, then counts number of options in second dropdown and again randomly chooses one of them.



easy to copy version:

open | http://www.motors.ebay.com/
storeXpathCount | //select[@id='_mak_new']/option | brandCount
store | javascript{Math.floor(storedVars['brandCount']*Math.random()+0)} | brand
select | id=_mak_new | index=${brand}
storeXpathCount | //select[@id='_mod_new']/option | modelCount
store | javascript{Math.floor(storedVars['modelCount']*Math.random()+0)} | model
select | id=_mod_new | index=${model}



As you can see in strings generating random numbers the lower boundary is “0” because in these dropdowns first elements have value “0” (not 1). Selecting e.g. 2nd element will look like:
select | id=_mod_new | index=1