Using variables in Test Objects

Check this site: http://mundrisoft.com/tech-bytes/read-data-from-excel-sheet-using-katalon-studio/

image.png

Hi,
Can any one please provide me the piece of code for the below scenario.
- There are multiple Roles available e.g. Admin, Manger, Engineer etc, I want to identify this roles as soon as user gets logged in and based on Rights module appears for the respective logged in user. Now I want to check all the modules available and clickable after logged in with any user based on the role.
e.g. Admin User ( can access Sys Admin and Sys admin contains module A,B and C) and Rest of the Modules like Setup, Configuration etc.
Engineer User (Can’t see the Sys Admin Module, hence will not be able to click or open this) and Rest of the modules like Setup, Configuration etc. available

Manager User (Can’t access the Sys Admin and Setup Module, hence will not be able to click or open this) and Rest of the modules like Report, Configuration etc available.

Can any one please help me with the piece of code for the same. I am struggling with this have tried many things but still not got the proper solution. JFI I have created the Keyword for this and making user login and then trying to fetch and save the Role in variable so that I can use it for the module role access…

Tried to change the xpath using this modify object, am unable to change the value

previous xpath: //*[@data-city=‘Singapore’]

Error message below…
//[@data-city=‘Singapore’][count(. | //[@data-city = ‘New Delhi’]) = count(//*[@data-city = ‘New Delhi’])]’

changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘data-city’, ‘equals’, ‘New Delhi’, true)

WebUI.click(changecountry)

script.jpg

error.jpg

I’m not sure ‘data-city’ is recognized as a object property. You could try using a more complete xpath like this:

changecountry = WebUI.modifyObjectProperty(findTestObject('GetCountryies/country_name'), 'xpath', 'equals', '//*[@city='New Delhi']', true)
1 Like

Thank you so much. This is working… Great!!!
Can we able to pass the test data in xpath like
changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘xpath’, ‘equals’, '//*[@data-city=‘findTestData(‘countriesListing’).getValue(1, 2)’,true)

I think it should be something like:
changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘xpath’, ‘equals’, ‘//*[@city=’+findTestData(‘countriesListing’).getValue(1, 2)+’,true)

Or maybe you should play around with double and single quotes, like this:
“//*[@city="+findTestData(‘countriesListing’).getValue(1, 2)+”

Try it and see what happens. :slight_smile:

1 Like

we are almost there.
changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘xpath’, ‘equals’, ‘//*[@data-city=’+findTestData(‘countriesListing’).getValue(1, 4)+’]’,true)

Xpath shows
Finding web element with id: ‘Object Repository/GetCountryies/country_name’ located by ‘By.xpath: //*[@data-city=Hong Kong]’ in ‘30’ second(s)

Single quote between Hong kong is not appearing

Try adding double quotes:

…‘xpath’, ‘equals’, ‘//*[@city=’"+findTestData(‘countriesListing’).getValue(1, 4)+"’]’,true)

1 Like

Sorry this doesn’t seems to work
changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘xpath’, ‘equals’, ‘//*[@data-city=’"+findTestData(‘countriesListing’).getValue(1, 2)+"’],true)

Hi Mate,
Thanks for your support, below code is working

changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country_name’), ‘xpath’, ‘equals’, ‘//*[@data-city="’+findTestData(‘countriesListing’).getValue(1, 3)+’"]’,true)

You are welcome!

1 Like

You Made my day!!!

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!!



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

1 Like

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…??