Forum Discussion

luizmass's avatar
luizmass
Regular Visitor
9 years ago

SoapUI Groovy: Brazilian Gerador de CPF CNPJ

Hi, 

 

To Brazilians that use ID, CPF and CNPJ, for the Test Cases, i got this in javascript and convert to Groovy, is it OK:

 

Here, I'd just used log.info but you can use any code to try them.

 

 

log.info gerarCPF(true)
log.info gerarCPF(false)
log.info gerarCnpj(true)
log.info gerarCnpj(false)

 

 

// codigo para gerar CPF
// codigo copiado de http://gerardocumentos.com.br/?pg=funcao-javascript-para-gerar-cpf e transformado para groovy
int randomiza(n) {
ranNum = Math.round(Math.random()*n);
return ranNum;
}

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

String gerarCPF(boolean comPontos) {

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

return cpf;
}

String gerarCnpj (boolean comPontos) {

n = 9;
n1 = randomiza(n);
n2 = randomiza(n);
n3 = randomiza(n);
n4 = randomiza(n);
n5 = randomiza(n);
n6 = randomiza(n);
n7 = randomiza(n);
n8 = randomiza(n);
n9 = 0; //randomiza(n);
n10 = 0; //randomiza(n);
n11 = 0; //randomiza(n);
n12 = 1; //randomiza(n);
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;
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;

return cnpj
}

1 Reply

  • Harry12's avatar
    Harry12
    New Contributor

    Thanks for sharing this Groovy version. The CPF and CNPJ validation logic looks correct and it's a useful example for anyone creating test data in SoapUI. In our QA process, we sometimes need valid documents quickly for manual testing without modifying the script. In those cases, we simply gerar CPF online and use the generated values in our requests before automating them. It saves time when testing different scenarios.