Javascript function to generate valid CNPJ

1

I need to generate valid CNPJ for the tests I'm performing, but I do not know how to create a function in Javascript and adapt it in Selenium IDE .

I even found the script on the internet, but I played it in Selenium user-extension file and it did not work:

The Script is this:

//função para gerar CNPJ

function gerarCnpj (field) {

    var comPontos = false;

    var n = 9;
    var n1 = randomiza(n);
    var n2 = randomiza(n);
    var n3 = randomiza(n);
    var n4 = randomiza(n);
    var n5 = randomiza(n);
    var n6 = randomiza(n);
    var n7 = randomiza(n);
    var n8 = randomiza(n);
    var n9 = 0; //randomiza(n);
    var n10 = 0; //randomiza(n);
    var n11 = 0; //randomiza(n);
    var n12 = 1; //randomiza(n);
    var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
    d1 = 11 - ( mod(d1,11) );
    if (d1>=10) d1 = 0;
    var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
    d2 = 11 - ( mod(d2,11) );
    if (d2>=10) d2 = 0;
    retorno = '';
    if (comPontos) cnpj = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
    else cnpj = ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12+d1+d2;

   field.value = cnpj;

}
    
asked by anonymous 02.10.2015 / 20:44

2 answers

1

Every function in Selenium has to start with Selenium.prototype.doNomeDialName. I adapted your code and it worked here. I did it based on one I got from CPF.

Selenium.prototype.doDigitaCNPJ = function(localizador, comPontuacao){

var elemento = this.page().findElement(localizador);

function gera_random(n){
var ranNum = Math.round(Math.random()*n);
return ranNum;
}

function mod(dividendo,divisor){
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}

 var n = 9;
 var n1 = gera_random(n);
 var n2 = gera_random(n);
 var n3 = gera_random(n);
 var n4 = gera_random(n);
 var n5 = gera_random(n);
 var n6 = gera_random(n);
 var n7 = gera_random(n);
 var n8 = gera_random(n);
 var n9 = 0;//gera_random(n);
 var n10 = 0;//gera_random(n);
 var n11 = 0;//gera_random(n);
 var n12 = 1;//gera_random(n);
 var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
 d1 = 11 - ( mod(d1,11) );
 if (d1>=10) d1 = 0;
 var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
 d2 = 11 - ( mod(d2,11) );
 if (d2>=10) d2 = 0;

if (comPontuacao=='true'){
    resultado = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
}
else {
    resultado = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12+d1+d2;    
} 

this.page().replaceText(elemento, resultado);

}

At the time of pointing to the CNPJ field use the "digitaCNPJ" command that was created in the code I pasted here. If you want a CNPJ with punctuation, put "true" in value (without the quotation marks). If it is to fill only with numbers, without points and slash, leave the value field empty.

By the time, I imagine that you have already resolved, but I hope to help others who will come to this topic.

    
17.03.2017 / 15:36
0

Rodrigo, here is the correct function.

Put it in your file and call it by clicking on the element with the onclick event, if you do not know anything about JavaScript events, read this here: COMPLETE LIST OF JAVASCRIPT EVENTS

Its function only generated the random numbers, that is, it only did a part of the work, did not concatenate the values later.

Full example, it will generate a valid number as soon as you click on the input:

<!DOCTYPE HTML>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">

    <script type="text/javascript">
function gera_random(n){
var ranNum = Math.round(Math.random()*n);
return ranNum;
}

function mod(dividendo,divisor){
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}

function cnpj(){
 var n = 9;
 var n1 = gera_random(n);
 var n2 = gera_random(n);
 var n3 = gera_random(n);
 var n4 = gera_random(n);
 var n5 = gera_random(n);
 var n6 = gera_random(n);
 var n7 = gera_random(n);
 var n8 = gera_random(n);
 var n9 = 0;//gera_random(n);
 var n10 = 0;//gera_random(n);
 var n11 = 0;//gera_random(n);
 var n12 = 1;//gera_random(n);
 var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
 d1 = 11 - ( mod(d1,11) );
 if (d1>=10) d1 = 0;
 var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
 d2 = 11 - ( mod(d2,11) );
 if (d2>=10) d2 = 0;
resultado = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
document.getElementById('cnpj').value=resultado;
}

    </script>

    <title></title>
</head>
<body>
<input type="text" id="cnpj" onclick="cnpj();">
</body>
</html>

Here's just the function:

function gera_random(n){
var ranNum = Math.round(Math.random()*n);
return ranNum;
}

function mod(dividendo,divisor){
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}

function cnpj(){
 var n = 9;
 var n1 = gera_random(n);
 var n2 = gera_random(n);
 var n3 = gera_random(n);
 var n4 = gera_random(n);
 var n5 = gera_random(n);
 var n6 = gera_random(n);
 var n7 = gera_random(n);
 var n8 = gera_random(n);
 var n9 = 0;//gera_random(n);
 var n10 = 0;//gera_random(n);
 var n11 = 0;//gera_random(n);
 var n12 = 1;//gera_random(n);
 var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
 d1 = 11 - ( mod(d1,11) );
 if (d1>=10) d1 = 0;
 var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
 d2 = 11 - ( mod(d2,11) );
 if (d2>=10) d2 = 0;
resultado = ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
document.getElementById('cnpj').value=resultado;
}
    
02.10.2015 / 21:38