Calling Local Variables to be used in Objects

Good afternoon,

I have the question if it possible to use Local Variables inside Objects, the same way that I do with Global Variables, which is by using the following syntax inside the respective Object Attributes parameters:

${GlobalVariables.MyVariable}

I am using Katalon v7.2.1

Leandro.

@leandro.rodeia

You can pass variables to Test Object. Assuming your TestObject has an attribute myAttribute with value ${myValue}, you can do this in your test case.

findTestObject('your Test Object ID', ['myValue': 'This is the actual value']);

Now the “This is the actual value” will be used in place of whatever operations that use myAttribute attribute.

Example.

findTestObject('your Test Object ID', ['myValue': 'This is the actual value']
.getProperties().each{prop -> println prop.getValue()}

will print This is the actual value among values of other attributes.

Hello @ThanhTo,

Thanks for the quick reply. Although, I don’t think I fully understand it.

I am attempting the following way for a local variable which is very similar to the way of Global Variables, but it doesn’t work:

What am I doing wrong here in terms of syntax of the local variable within the object?

@leandro.rodeia

Assuming your Test Object with a local variable is like this:

If you go to the Test Case’s manual view and add an action (Click for example)

You would need to specify a Test Object, click on null and choose your Test Object (the one with local variables), and add the name of the local variable, as well as the value.

And click OK. If you switch to Script view, you should see

WebUI.click(findTestObject('New Test Object', [('local_variable_name') : 'actual value']))

Now your Test Object will replace the variable with the actual value when the executions needs it. Alternatively, you can write the above line directly in the Script mode of a Test Case, which was what I originally suggested.

Hello @ThanhTo,

Thank you for your swift reply, unfortunately I was a bit busy and I only had time today to carefully review this situation, in which I could conclude it is still not working.

Here’s what I did in my test script:

Created my variable in Script mode and successfully insert it towards the target input field…

String LogisticsRandomVAT = CustomKeywords.‘framework.random.randomNumber’(200000000, 700000000)

WebUI.setText(findTestObject(‘Company Creation and Verification/input_VATNumber’), LogisticsRandomVAT)

Now for the verification part I adapted my originally detected Katalon Object path (which works 100% with other fields of the same exact style, but with static raw inputs) from:

id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number 403179643 ’ or . = 'VAT Number 403179643 ')]) = count(//[(text() = 'VAT Number 403179643 ’ or . = 'VAT Number 403179643 ')])]

With local variable, to:

id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(//[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')])]

But at this very last step, an error is returned saying something about the Web Elemend ID not being found…

What am I doing wrong?

This should be:

WebUI.setText(findTestObject(‘Company Creation and Verification/input_VATNumber’, ['LogisticsRandomVAT': LogisticsRandomVAT]));

Note that the map of variable(s) is an argument to the findTestObject function, rather than the setText keyword.

If you still encounter error, please provide the full console log. Devils are in the details.

Hello @ThanhTo,

Thanks for the swift reply.

I don’t have any problem with the setText operation, just with the very last operation which is the adapted verification Object with the Local Variable within its attribute which is where I am stuck at, by not detecting my local variable:

id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | // [(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(// [(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')])]

based on (with raw value):

id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | // [(text() = 'VAT Number 403179643 ’ or . = 'VAT Number 403179643 ')]) = count(// [(text() = 'VAT Number 403179643 ’ or . = 'VAT Number 403179643 ')])]

Which returned the following error:

ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Web element with id: ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ located by 'By.xpath: id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(//[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ‘)])]’ not found (Root cause: com.kms.katalon.core.exception.StepFailedException: Web element with id: ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ located by 'By.xpath: id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(//[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ‘)])]’ not found
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.doCall(VerifyElementVisibleKeyword.groovy:89)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.call(VerifyElementVisibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.verifyElementVisible(VerifyElementVisibleKeyword.groovy:97)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.execute(VerifyElementVisibleKeyword.groovy:67)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementVisible(WebUiBuiltInKeywords.groovy:372)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementVisible$5.call(Unknown Source)
at Company Creation and Verification.run(Company Creation and Verification:76)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1589882382869.run(TempTestCase1589882382869.groovy:23)
)
2020-05-19 11:01:04.850 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to verify object ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ is visible (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to verify object ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ is visible
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.verifyElementVisible(VerifyElementVisibleKeyword.groovy:97)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.execute(VerifyElementVisibleKeyword.groovy:67)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementVisible(WebUiBuiltInKeywords.groovy:372)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementVisible$5.call(Unknown Source)
at Company Creation and Verification.run(Company Creation and Verification:76)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1589882382869.run(TempTestCase1589882382869.groovy:23)
Caused by: com.kms.katalon.core.exception.StepFailedException: Web element with id: ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ located by 'By.xpath: id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(//[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ‘)])]’ not found
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.doCall(VerifyElementVisibleKeyword.groovy:89)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.call(VerifyElementVisibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more
)
2020-05-19 11:01:04.852 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Logistics-side/Company Creation and Verification FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify object ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ is visible
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.verifyElementVisible(VerifyElementVisibleKeyword.groovy:97)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.execute(VerifyElementVisibleKeyword.groovy:67)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementVisible(WebUiBuiltInKeywords.groovy:372)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementVisible$5.call(Unknown Source)
at Company Creation and Verification.run(Company Creation and Verification:76)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1589882382869.run(TempTestCase1589882382869.groovy:23)
Caused by: com.kms.katalon.core.exception.StepFailedException: Web element with id: ‘Object Repository/Company Creation and Verification/div_VAT_Number_Randomized’ located by 'By.xpath: id(“app”)/div[1]/section[@class=“section is-main-section”]/div[@class=“columns”]/div[@class=“column”]/div[@class=“card tile is-child”]/div[@class=“card-content”]/form[1]/div[@class=“field”][count(. | //[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ')]) = count(//[(text() = 'VAT Number ${LogisticsRandomVAT} ’ or . = 'VAT Number ${LogisticsRandomVAT} ‘)])]’ not found
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.doCall(VerifyElementVisibleKeyword.groovy:89)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.call(VerifyElementVisibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
… 17 more

Anyhow, I decided to apply your answer and now my setText stopped working, the error being returned on that aspect is:

WebUI.setText(findTestObject(‘Company Creation and Verification/input_VATNumber’, [‘LogisticsRandomVAT’: LogisticsRandomVAT]));

ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Logistics-side/Company Creation and Verification FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.setText() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject) values: [TestObject - ‘Object Repository/Company Creation and Verification/input_VATNumber’]
Possible solutions: getText(com.kms.katalon.core.testobject.TestObject), setText(com.kms.katalon.core.testobject.TestObject, java.lang.String), getText(com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling), setText(com.kms.katalon.core.testobject.TestObject, java.lang.String, com.kms.katalon.core.model.FailureHandling), clearText(com.kms.katalon.core.testobject.TestObject), clearText(com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling)
at Company Creation and Verification.run(Company Creation and Verification:46)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1589882819024.run(TempTestCase1589882819024.groovy:23)

I believe this is all about a miss-interpretation by Katalon on the Local Variable within the Object attributes, despite being theoretically well-placed?

Additionally, it’s important to note that this works by replacing the Local Variable with the respective generated raw value, but since this is dynamic (random raw value) I need the Local Variable to work.

Let me spread this out for you:

WebUI.setText(  // call the setText method
  findTestObject( // call findTestObject to find the target element
    // ID of the test object in the OR
    'Company Creation and Verification/input_VATNumber',
    // variables map to be passed to findTestObject
    ['LogisticsRandomVAT': LogisticsRandomVAT]
  ), 
  // The value to pass to setText
  'A new value'
)

How about trying this:

WebUI.verifyElementPresent(
    findTestObject(
        ‘Company Creation and Verification/input_VATNumber’, 
        [ ‘LogisticsRandomVAT’ : LogisticsRandomVAT ]
    ),
    10
);

Here I assume you set up the TestObject in the way @ThanTo suggested.

If this WebUI.verifyElementPresent call passes, then it proves that the TestObject created by findTestObject() is fine. If it fails, then the TestObject is still wrong.


I suppose, this WebUI.verifyElementPresent would pass if you follow the ThanTo’s way.

Then, why you got ERROR of “groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.setText() is applicable for …”? Well, let’s talk about it later. Pleast try the above-mentioned test first.