I don’t know how the Selenium
works, but apparently Ricardo has already answered the question of how to catch the CPF
of the site.
Now, to generate in your own application, in Java
, you should follow some rules, which are available on some websites, such as this.
I passed the website for code in an extremely basic way:
class GeradorDeCPF {
public static void main(String[] args) {
int[][] CPFArray = new int[11][3];
// PREENCHA O CPF ABAIXO, OU NA EXECUCAO DO PROGRAMA
// String CPFString = "111444777";
String CPFString = args[0];
/*
* LACO PARA TRANSFORMAR O CPF NUM ARRAY DE INTEIROS
* INCLUIR OS NUMEROS MULTIPLICADORES E FAZER A CONTA
*/
for (int i = 0; i < CPFString.length(); i++) {
// CONVERTE CHAR PARA INT E ATRIBUI AO ARRAY
CPFArray[i][0] = Character.getNumericValue(CPFString.charAt(i));
// ATRIBUI MULTIPLICADOR AO ARRAY
CPFArray[i][1] = 10-i;
// EXECUTA A MULTIPLICACAO
CPFArray[i][2] = CPFArray[i][0] * CPFArray[i][1];
}
// SOMA TODOS OS RESULTADOS
int somaResultados = 0;
for (int i = 0; i < 9; i++) {
somaResultados += CPFArray[i][2];
}
// CALCULA MODULO
int moduloDigito1 = somaResultados % 11;
int digito1 = 0;
/*
* SE O MODULO FOR MENOR QUE DOIS, TRANSFORMAR EM ZERO
* CASO NAO SEJA, SUBTRAIR DE 11 PARA OBTER O PRIMEIRO DIGITO
*/
if (moduloDigito1 < 2) {
digito1 = 0;
} else {
digito1 = 11 - moduloDigito1;
}
// INCLUI O PRIMEIRO DIGITO NO FINAL DO CPF
CPFArray[9][0] = digito1;
// ALTERA OS MULTIPLICADORES
for (int i = 0; i < 10; i++) {
// ATRIBUI MULTIPLICADOR AO ARRAY
CPFArray[i][1] = 11-i;
// EXECUTA A MULTIPLICACAO
CPFArray[i][2] = CPFArray[i][0] * CPFArray[i][1];
}
// SOMA TODOS OS RESULTADOS
int somaResultados2 = 0;
for (int i = 0; i < 10; i++) {
somaResultados2 += CPFArray[i][2];
}
// CALCULA MODULO
int moduloDigito2 = somaResultados2 % 11;
int digito2 = 0;
/*
* SE O MODULO FOR MENOR QUE DOIS, TRANSFORMAR EM ZERO
* CASO NAO SEJA, SUBTRAIR DE 11 PARA OBTER O PRIMEIRO DIGITO
* (E IMPRIME)
*/
if (moduloDigito2 < 2) {
digito2 = 0;
} else {
digito2 = 11 - moduloDigito2;
}
// INCLUI O PRIMEIRO DIGITO NO FINAL DO CPF
CPFArray[10][0] = digito2;
// IMPRIME CPF COM DIGITOS
System.out.print("Digitos verificadores: ");
System.out.print(CPFArray[9][0] + "" + CPFArray[10][0] + "\n");
}
}
Follow the execution:
Now, if you want one CPF
random, you have to make some changes. Import these classes at the beginning:
import java.util.concurrent.ThreadLocalRandom;
import java.text.DecimalFormat;
And change the CPFString
for:
int CPFInt = ThreadLocalRandom.current().nextInt(0,999999999 + 1);
DecimalFormat formatador = new DecimalFormat("000000000");
String CPFString = formatador.format(CPFInt);
Upshot:
Remember that this code is just a start (you can improve ENOUGH), and you still need to make it run on Selenium
!
I managed to get from the site quietly through
$(".btn-default").click(); $("#numero").val();
, but wouldn’t it be better to generate it in your application? This way, your testing is not conditioned to another site.– Ricardo Rodrigues de Faria
@Ricardorodriguesdefaria and how would I do that? I’m new to both java and automation. I was suggested to do that too, but I have no idea how and how complex it is
– Luís Gustavo Vieira
Hi Luis@Welcome to [en.so]! I marked the question as pending because there wasn’t enough information to determine why Selenium didn’t report it. If you need help with that, post the code you used so we can analyze it. Besides, the question ended up changing the focus a little to "how to generate a CPF"? If you wish you can [Dit] your question to make it consistent with the answer.
– utluiz