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;
}
Can you post your html code here? especially the part where you call this function there? Because you must make his call to generate?
– Paulo Roberto
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.
– Rodrigo Santos
That’s what I asked for, the code snippet where you call this function.
– Paulo Roberto
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.
– Rodrigo Santos
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()); };
– Rodrigo Santos
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?
– Paulo Roberto
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
– Rodrigo Santos
Relax, we’re here to help.
– Paulo Roberto
Paul you would have skype or something ? I think I’d better explain by there
– Rodrigo Santos
Let’s go continue this discussion in chat.
– Rodrigo Santos
Already found the error, the function does not work Rodrigo, Uncaught Referenceerror: randomiza is not defined
– Paulo Roberto
The chat is blocked here in my company, can be by email [email protected]
– Paulo Roberto
aaa and you would know me what is wrong in the function ?
– Rodrigo Santos
I sent you an email
– Rodrigo Santos