Javascript function to generate valid CNPJ

Asked

Viewed 2,416 times

1

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

I even found on the Internet the script, but I played in the file of user-Extension of Selenium and it didn’t work:

The Script that’s the one:

//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;

}
  • Can you post your html code here? especially the part where you call this function there? Because you must make his call to generate?

  • Hi Paulo, then.. i have no HTML file to call, I use the Selenium-ide and pass the name of the function q I create within a file containing several other Java functions. In this file there are only distinct functions.

  • That’s what I asked for, the code snippet where you call this function.

  • I don’t understand this bit of code I call function.. because all the other functions that I have I only create the function and in Selenium I pass its name and it works.

  • an example of a function that I use in this file: Selenium.prototype.assertPaging = Function(Locator, paging) { var element = this.page(). findElement(Locator); var actualText = gettext(element); var val = actualText.match(/ d+| d+ b| d+(?= w)/g); var splitedPaging = paging.split('='); var isValid = val[splitedPaging[0]] == splitedPaging[1]; Assert.Matches("true", isValid.toString()); };

  • So come on, you go to the page, with Lenium open and it records the steps and then you run them again, right? At some point in this process you invoke this Cpf generating function to generate a number and fill in the page input with the data... You can post your full code there instead of just posting the javascript function?

  • Sorry Paul is because I’m new to the automation part. I do not control the html code of the page I am automating...I simply mount the commands in Selenium-ide and use some new commands that Selenium does not have ( that of cnpj ) for example. then wanted to use as use in other commands for example : Command = gerarCnpJ | Target = ( screen input field ) | value = variableany

  • Relax, we’re here to help.

  • Paul you would have skype or something ? I think I’d better explain by there

  • Already found the error, the function does not work Rodrigo, Uncaught Referenceerror: randomiza is not defined

  • The chat is blocked here in my company, can be by email [email protected]

  • aaa and you would know me what is wrong in the function ?

  • I sent you an email

Show 9 more comments

2 answers

0

Rodrigo, here is the correct function.

Put it on your file and call it She by clicking on the element with the onclick event, if you know nothing about Javascript events read this here: COMPLETE LIST OF JAVASCRIPT EVENTS

His function only generated the random numbers, that is, he only did a part of the work, did not concatenate the values after.

Complete 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 is only 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;
}
  • 4

    Any particular reason to create a new function to do what is already native in JS?

0

Every function in Selenium has to start with Selenium.prototype.doNomeDaNovaFuncao. 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);

}

When pointing to the CNPJ field use the "digitaCNPJ" command that was created in the code that I pasted here. If you want a CNPJ with punctuation, put "true" in value (without quotation marks). If filling only with numbers, no dots and bar, leave the field empty.

For the time being, I imagine you already have, but I hope to help others who will reach this topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.