Generate a valid South African ID number with javascript and add it as an extension to Selenium

The South African ID number contains of 13 digits:
- the first six digits are a date of birth in the yymmdd format,
- the seventh digits represents the gender – female if the digit if the digit is less than 5 and male if equals 5 or if it is greater than 5,
- the next three digits can be generated randomly,
- the 11th digit can be either 0 or 1
- the 12th digit can be random
- the 13th digit is a parity check that can be generated with the Luhn algorithm

For a given date of birth and gender we can write a piece of javascript as follows:

      var dob=800101;  //date of birth
      var gen=6; //gender
      var randoms = Math.floor(Math.random()*900)+1000; //
      var nin = ''+dob + gen +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*2) +
Math.floor(Math.random()*10);    
      var sumEven = 0;
      var sumOdd = 0;
      var even = false;
      for(i=0; i<12; i++) {
        if(!even) {
          sumOdd += parseInt(nin.charAt(i));
        } else {
          var doubleEven = 2*parseInt(nin.charAt(i));
          sumEven += Math.floor(doubleEven/10)+(doubleEven % 10);
        }       
        even = !even
      }
      var total = sumOdd + sumEven;
      var check = (10 - (total % 10)) % 10;
      var fulnin = nin+check;
     document.write(fulnin);

Say that for testing purposes we want to generate a different number anytime we run a Selenium test. The above can be added as a Selenium extension after minor modifications:

Selenium.prototype.doGenZAIDnumber = function(locator, text) {
      var element = this.page().findElement(locator);
      var dob=800101;
      var gen=6;
      var nin = ''+dob + gen +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*10) +
Math.floor(Math.random()*2) +
Math.floor(Math.random()*10);    
      var sumEven = 0;
      var sumOdd = 0;
      var even = false;
      for(i=0; i<12; i++) {
        if(!even) {
          sumOdd += parseInt(nin.charAt(i));
        } else {
          var doubleEven = 2*parseInt(nin.charAt(i));
          sumEven += Math.floor(doubleEven/10)+(doubleEven % 10);
        }
        even = !even
      }
      var total = sumOdd + sumEven;
      var check = (10 - (total % 10)) % 10;
      fullnin = nin+check;
      this.page().replaceText(element, fullnin);
}

To add the above as a command to Selenium, save it on your drive as user-extentions.js. Then open Selenium and go to Options->Options->General, click ‘Browse; next to the ‘Selenium, Core extentions’ field and point to your saved file. Restart Selenium.

Now, two new commands should appear in the command drop down in Selenium: ‘GenZAIDnumber’ and ‘GenZAIDnumberAndWait’. They can be used to fill any field with a random South African ID numbers.

Comments

Thanks for wtirnig such an

Thanks for wtirnig such an easy-to-understand article on this topic.

:)

:)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.