Using variables in Test Objects

Hi Guys, I’m facing a similar problem, but with a WS Request object
I need to replace an object property value that is in the body of my request with the variable that I had generate in my script, before send the request. (Because for the criation of users, the property cannot be repeated)

Until here, i’ve tried two ways to deal with it:

object = (findTestObject(‘Users/ValidaCriacaoUsuario/CriaUsuarioComSucesso’))

object.findProperty(‘CPF’).setValue(cpf)

object.findProperty(‘CPF’).setActive(true)

when i run, i got this error:
05-29-2018 04:55:56 PM - [ERROR] - Test Cases/Users/ValidaCriacaoUsuario FAILED because (of) java.lang.NullPointerException: Cannot invoke method setValue() on null object

And i don’t know why i got this error, because i have a object with this propertys in the body.
The second way i tried unsuccessfully, was using a WebUI keyword, and i don’t got any error, but it is not working too (the value in the property keep the same):

WebUI.modifyObjectProperty(object, ‘CPF’, ‘equals’, cpf, true, FailureHandling.STOP_ON_FAILURE)

//WebUI.modifyObjectProperty(object, ‘Email’, ‘equals’, strng, true, FailureHandling.STOP_ON_FAILURE)

Can anyone help me ? I’m new in katalon and don’t know if there is any way to do this or it can be a limitation of the tool once the body gets ‘fixed’ on the object

Thanks!!



1 Like

The message “java.lang.NullPointerException: Cannot invoke method setValue() on null object” is saying that the referece to the variable ‘cpf’ is null in the following line.

object.findProperty('CPF').setValue(cpf)

Where and how do you define the variable cpf?

The variable cpf is the return of a method that i call before the request

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 cpf = gerarCPF(false)

I solved the problem reading the comments in http://forum.katalon.com/discussion/6774/parameterize-a-web-service-object#latest and inserting a variable in my body!
Thanks!!

Another way to accomplish this is to parameterize the Test Object itself. You can set up your TO with an xpath like: id(“rbContainer_pCustSegToDisplay”)/li[text()=${Metric}]

Then you use it in your Test Case like this:

String Metric = 'Sales'
TestObject to = findTestObject('SelectMetric', [('Metric') : Metric])
WebUI.click(to)

The Metric variable can be assigned in your Test Case as above or through a Data File.

Pros:

No need to modify Test Object properties within your Test Cases

It is clear that you are passing a variable to the Test Object

Cons:

Test Objects become a little more complex

I think this works only with the Basic selection method, not the XPath or CSS selection methods

2 Likes

you can do it, bro,

steps----

  1. First, add profiles which you want like "staging production "likewise then create variable there and put it empty.

2.in the script like this use like this Name is ur variable and you assigned it to your profile variable
like this
“GlobalVariable.Username = Name”

Hi ,

I have a model page and in that model page i have one option that is Api Url so to ensure that if any change made to model specification the api url should be same .

So what i want to do is

API URL - www.apiurl.com (This value) i can store to one string variable and print to console then do some change and then again extract url value from that api url box and saved to another variable and print to console now after that i compare both var 1 and var2 whether they are equal if they are equal then print test “Equal” otherwise print “Not equal”

Please give me idea how i can get value of url from web element and save it to a string var then after print same to console and again how i can compare both

Thanks!
Rahul

This approach isn’t working in a Loop once the First index it Finds it always uses that value only irrespective of the Looping variable value getting incremented …Could you please suggest a Solution on this…??