Get customKeywords variables

Hello, guys!

I’ve created a customKeyword method for enter in a website and get a “cpf”:

public class buscaNovoCpf {
	@Keyword
	def coletarCpf(){		
		WebUI.openBrowser('');
		WebUI.maximizeWindow();
		WebUI.navigateToUrl('https://www.geradordecpf.org/');		
		WebUI.delay(2);		
		def driver = DriverFactory.getWebDriver();		
		WebElement btnGerarCpf = driver.findElement(By.cssSelector('#btn-gerar-cpf'));
		btnGerarCpf.click();	
		def cpfValue = WebUI.executeJavaScript('return $(".col-sm-8 #numero").val()', []);
		println(cpfValue);			
	}
}

CPF is a personal document here in Brazil and this website provides a fake for testing purposes.

If i want to use the variable “cpfValue” in a test case, how can i access this?

Thanks!

If I’ve understood correctly…

	String coletarCpf(){		
		WebUI.openBrowser('');
		WebUI.maximizeWindow();
		WebUI.navigateToUrl('https://www.geradordecpf.org/');		
		WebUI.delay(2);		
		def driver = DriverFactory.getWebDriver();		
		WebElement btnGerarCpf = driver.findElement(By.cssSelector('#btn-gerar-cpf'));
		btnGerarCpf.click();	
		def cpfValue = WebUI.executeJavaScript('return $(".col-sm-8 #numero").val()', []);
		return cpfValue
	}

In your test case…

println(/* The call to your coletarCpf Keyword*/)

Hello, Russ

My idea is something like this:

CustomKeywords.'metodosGenericos.buscaNovoCpf.coletarCpf'();
WebUI.setText(findTestObject('Page_ (4)/input_defaultDocumentValue'), 'cpfValue');

Call the method that will return the cpfValue and “paste” him at the right field on the form.

Try this, Mateus :

String cpf = CustomKeywords.'metodosGenericos.buscaNovoCpf.coletarCpf'()
WebUI.setText(findTestObject('Page_ (4)/input_defaultDocumentValue'), cpf)

But make sure you change coletarCpf to type String (see my last post).

Thank you so much, Russ, it worked fine for me!!

Best regards, bro!

You’re welcome. And thanks for the BA B)

Returning the value from the keyword is definitely the cleanest solution. It keeps the keyword reusable and avoids relying on global variables.

For test automation, I also prefer generating fresh test data on demand rather than storing static values. For example, when I need to gerar cpf for validation or form testing, returning the generated value directly from the keyword makes the test much easier to maintain.

Yes, Keywords or any reusable test artifacts are anytime the best choice in the script