# Using variables in Test Objects

**URL:** <https://forum.katalon.com/t/using-variables-in-test-objects/6557>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 11, 2016, 8:57pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557 "2016-11-11T20:57:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dean\_Hicks](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@Dean\_Hicks](https://forum.katalon.com/u/Dean_Hicks)\
**Post date:** [November 11, 2016, 8:57pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/1 "2016-11-11T20:57:25Z")

</div>

Is it possible to use a variable or test data within a Test Object?

For instance, I want to create an event in our events management system that pulls the title “Event 1” from test data (or a variable). Then, in second test case, I want to find the Event to edit it by locating and clicking the link element on that page whose label matches that same variable/test data.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Roxanne\_Frankland](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Roxanne\_Frankland](https://forum.katalon.com/u/Roxanne_Frankland)\
**Post date:** [May 16, 2017, 9:51am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/2 "2017-05-16T09:51:22Z")

</div>

Hi Dean

I’m also interested to find out if there is a simple way to reference a variable. Did you ever find a way?

Thanks

---

<div class="post-metadata">

**Author:** ![Dean\_Hicks](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@Dean\_Hicks](https://forum.katalon.com/u/Dean_Hicks)\
**Post date:** [November 15, 2016, 8:29pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/3 "2016-11-15T20:29:14Z")

</div>

There’s no way to simply reference a variable as a property on the Test Object so I can “find object by”?

For example:

text=[variable]

---

<div class="post-metadata">

**Author:** ![Sandip\_Toradmal](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@Sandip\_Toradmal](https://forum.katalon.com/u/Sandip_Toradmal)\
**Post date:** [September 7, 2017, 8:31am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/4 "2017-09-07T08:31:28Z")

</div>

Suppose below is your xpath for specific row in the table

html/body/div[1]/section[2]/div[1]/section/aside/div[2]/aside/aside[2]/div[2]/div/div/div[1]/table/tbody/tr[1]/td[3]/a

in this case you want to change the row dynamically i.e. tr[1] to be changed to tr[2], tr[3] …and so on

You can declare int i=1 and use the same if want to increase/ decrease its value

xpath for the same will change

html/body/div[1]/section[2]/div[1]/section/aside/div[2]/aside/aside[2]/div[2]/div/div/div[1]/table/tbody/tr["+i+"]/td[3]/a

---

<div class="post-metadata">

**Author:** ![Sandip\_Toradmal](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@Sandip\_Toradmal](https://forum.katalon.com/u/Sandip_Toradmal)\
**Post date:** [September 7, 2017, 9:21am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/5 "2017-09-07T09:21:38Z")

</div>

> @Dean Hicks said:  
> Is it possible to use a variable or test data within a Test Object?
> 
> For instance, I want to create an event in our events management system that pulls the title “Event 1” from test data (or a variable). Then, in second test case, I want to find the Event to edit it by locating and clicking the link element on that page whose label matches that same variable/test data.
> 
> Thanks in advance!

You can use object property wherein your “title” contains value “Event 1”

---

<div class="post-metadata">

**Author:** ![Alex\_Brohin](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/alex_brohin/32/23869_2.png) [@Alex\_Brohin](https://forum.katalon.com/u/Alex_Brohin)\
**Post date:** [May 26, 2017, 9:08pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/6 "2017-05-26T21:08:08Z")

</div>

It is a good question, as we also would be interested in knowing how to solve it. In our case we have unique transactions that need to be verified, to be more precise their content is to be verified.

Create a new transaction  
Store the transaction ‘Date/Time’ as a variable  
Open the list of transactions  
Find the last transaction by using the unique ‘Date/Time’ variable stored earlier  
Open the exact transaction  
Verify the content

Please help us implement this scenario!

Thanks!

---

<div class="post-metadata">

**Author:** ![Zarashima](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/zarashima/32/928_2.png) [@Zarashima](https://forum.katalon.com/u/Zarashima)\
**Post date:** [November 14, 2016, 3:49pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/7 "2016-11-14T15:49:44Z")

</div>

This case is quite complicated, and you can use groovy scripts to make this happen.

Assign variable’s value to the test object , which is link element in your example. This test object should have one unique property such as “text()” to demonstrate event’s label

After an event is created, change “text()” property’s value to be new value and use it in your test step.

Example:

`import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase import static com.kms.katalon.core.testdata.TestDataFactory.findTestData import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords import com.kms.katalon.core.model.FailureHandling as FailureHandling import com.kms.katalon.core.testcase.TestCase as TestCase import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory import com.kms.katalon.core.testdata.TestData as TestData import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository import com.kms.katalon.core.testobject.TestObject as TestObject import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords import internal.GlobalVariable as GlobalVariable import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS`

// Create test object’s variable  
lnk\_EditEvent = findTestObject(‘Page\_EventManagement/btn\_SearchEvent’)

// Change test object’s property and set its active to use this property for finding object  
lnk\_EditEvent.findProperty(‘text()’).setValue(createdEventName)  
lnk\_EditEvent.findProperty(‘text()’).setActive(true)

// Click on the changed link element  
WebUI.click(lnk\_EditEvent)

---

<div class="post-metadata">

**Author:** ![B\_L](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/b_l/32/976_2.png) [@B\_L](https://forum.katalon.com/u/B_L)\
**Post date:** [October 24, 2017, 4:20pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/8 "2017-10-24T16:20:06Z")

</div>

> @Dean Hicks said:  
> Is it possible to use a variable or test data within a Test Object?
> 
> For instance, I want to create an event in our events management system that pulls the title “Event 1” from test data (or a variable). Then, in second test case, I want to find the Event to edit it by locating and clicking the link element on that page whose label matches that same variable/test data.
> 
> Thanks in advance!

I am still new to Katalon, but from what I have seen this would not be possible, once a test is completed and a second test is run it would have to have fresh values or pre-stored variables. I do not know of a way to edit test data from running a test…

With that being said if you mean in a single test case to grab it and then use it for a dynamic value. yes you could do that.

I would do something like this:  
eventOne = WebUI.getText(findTestObject(‘objectEvent’))

linkElement = WebUI.modifyObjectProperty(findTestObject(‘objectLink’), ‘xpath’, ‘equals’, ‘//\*[@type="button"][@text()="’ + eventOne + ‘"]’, false)

WebUI.click(linkElement)

obviously you would want to modify the property modification based upon what you need. What this will do is create a new variable, based upon an object you predefined and add in the data pulled from event.

notice I have a " with a follow ’ this is closing off the first part and adding in the value from eventOne then follows with "] to close it off.

Is that what you where looking for?

---

<div class="post-metadata">

**Author:** ![Balraj\_Alladi](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Balraj\_Alladi](https://forum.katalon.com/u/Balraj_Alladi)\
**Post date:** [October 31, 2017, 4:58pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/9 "2017-10-31T16:58:50Z")

</div>

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

> **[Read data from excel sheet using Katalon Studio](http://mundrisoft.com/tech-bytes/read-data-from-excel-sheet-using-katalon-studio/)**
>
> It it very easy to get data from excel using Katalon Studio. For Data driven test cases we need excel sheet. In this article will see how to use excel sheet in KATALON studio by using findTestData method. Select Menu File -\> New -\> Test Data (Katalon...

[image.png](https://s3.amazonaws.com/katalon-forum/editor/cs/ljr64dsywd4a.png)

---

<div class="post-metadata">

**Author:** ![Shamji\_Jinjala](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@Shamji\_Jinjala](https://forum.katalon.com/u/Shamji_Jinjala)\
**Post date:** [March 6, 2018, 10:51am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/10 "2018-03-06T10:51:12Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 10:36am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/11 "2018-05-02T10:36:15Z")

</div>

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

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

**Error message below…**  
 ![](https://s3.amazonaws.com/katalon-forum/editor/sj/n4r5z990l7ty.jpg "Image: https://s3.amazonaws.com/katalon-forum/editor/sj/n4r5z990l7ty.jpg")//_[@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)  
 ![](https://s3.amazonaws.com/katalon-forum/editor/xy/r5cajqbxr3x6.jpg "Image: https://s3.amazonaws.com/katalon-forum/editor/xy/r5cajqbxr3x6.jpg")

[script.jpg](https://s3.amazonaws.com/katalon-forum/editor/xy/r5cajqbxr3x6.jpg)

[error.jpg](https://s3.amazonaws.com/katalon-forum/editor/sj/n4r5z990l7ty.jpg)

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [May 2, 2018, 11:14am UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/12 "2018-05-02T11:14:44Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 12:02pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/13 "2018-05-02T12:02:46Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [May 2, 2018, 12:10pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/14 "2018-05-02T12:10:30Z")

</div>

I think it should be something like:  
changecountry = WebUI.modifyObjectProperty(findTestObject(‘GetCountryies/country\_name’), ‘xpath’, ‘equals’, ‘//\*[[@city=’+findTestData(‘countriesListing’)](http://forum.katalon.com/profile//city%3D%27findTestData%28%27countriesListing%27%29).getValue(1, 2)+’,true)

Or maybe you should play around with double and single quotes, like this:  
“//\*[[@city="+findTestData(‘countriesListing’)](http://forum.katalon.com/profile//city%3D%27findTestData%28%27countriesListing%27%29).getValue(1, 2)+”

Try it and see what happens. 🙂

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 12:27pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/15 "2018-05-02T12:27:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [May 2, 2018, 12:36pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/16 "2018-05-02T12:36:09Z")

</div>

Try adding double quotes:

…‘xpath’, ‘equals’, ‘//\*[[@city=’"+findTestData(‘countriesListing’)](http://forum.katalon.com/profile//city%3D%27%2BfindTestData%28%27countriesListing%27%29 "Link: http://forum.katalon.com/profile//city%3D%27%2BfindTestData%28%27countriesListing%27%29").getValue(1, 4)+"’]’,true)

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 1:04pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/17 "2018-05-02T13:04:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 1:13pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/18 "2018-05-02T13:13:45Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [May 2, 2018, 1:22pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/19 "2018-05-02T13:22:35Z")

</div>

You are welcome!

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 2, 2018, 1:30pm UTC](https://forum.katalon.com/t/using-variables-in-test-objects/6557/20 "2018-05-02T13:30:05Z")

</div>

You Made my day!!!

[Next page](https://forum.katalon.com/t/using-variables-in-test-objects/6557.md?page=2)
