Katalon Studio v5.8 - It's time for the all-new API testing experience
Dean Hicks
Katalon Apprentice
11/11/2016

Using variables in Test Objects

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!

Upvote
Quote

Comments

  • Vinh Nguyen
    Katalon Moderator
    11/14/2016
    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)
    Upvote
    Quote
  • Dean Hicks
    Katalon Apprentice
    11/15/2016
    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]
    Upvote
    Quote
  • Roxanne Frankland
    Katalon Apprentice
    05/16/2017
    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
    Upvote
    Quote
  • Alex Brohin
    Katalon Apprentice
    05/26/2017
    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!
    Upvote
    Quote
  • Sandip Toradmal
    Katalon Apprentice
    09/07/2017

    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

    Upvote
    Quote
  • Sandip Toradmal
    Katalon Apprentice
    09/07/2017

    @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"

    Upvote
    Quote
  • B L
    Katalon Ambassador
    10/24/2017

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

    Upvote
    Quote
  • Shamji Jinjala
    Katalon Apprentice
    03/06/2018
    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..


    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    edited May 2
    Tried to change the xpath using this modify object, am unable to change the value

    previous xpath: //*[@city='Singapore']

    Error message below....
    //*[@city='Singapore'][count(. | //*[@city = 'New Delhi']) = count(//*[@city = 'New Delhi'])]'

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

    WebUI.click(changecountry)

    Upvote
    Quote
  • Mate Mrse
    Katalon Expert
    05/02/2018
    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)


    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    edited May 2
    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', '//*[@city='findTestData('countriesListing').getValue(1, 2)',true)
    Upvote
    Quote
  • Mate Mrse
    Katalon Expert
    05/02/2018
    edited May 2
    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.  :)
    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    we are almost there.
    changecountry = WebUI.modifyObjectProperty(findTestObject('GetCountryies/country_name'), 'xpath', 'equals', '//*[@city='+findTestData('countriesListing').getValue(1, 4)+']',true)

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

    Single quote between Hong kong is not appearing
    Upvote
    Quote
  • Mate Mrse
    Katalon Expert
    05/02/2018
    Try adding double quotes:

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


    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    Sorry this doesn't seems to work
    changecountry = WebUI.modifyObjectProperty(findTestObject('GetCountryies/country_name'), 'xpath', 'equals', '//*[@city='"+findTestData('countriesListing').getValue(1, 2)+"'],true)
    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    Hi Mate,
    Thanks for your support, below code is working 

    changecountry = WebUI.modifyObjectProperty(findTestObject('GetCountryies/country_name'), 'xpath', 'equals', '//*[@city="'+findTestData('countriesListing').getValue(1, 3)+'"]',true)
    Upvote
    Quote
  • Mate Mrse
    Katalon Expert
    05/02/2018
    You are welcome!
    Upvote
    Quote
  • Jaikumar Kesav
    Katalon Apprentice
    05/02/2018
    You Made my day!!!
    Upvote
    Quote
  • Gustavo Queiroz
    Katalon Apprentice
    05/29/2018
    edited May 29
    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!!



    Upvote
    Quote
  • kazurayam
    Katalon Evangelist
    05/30/2018
    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?

    Upvote
    Quote
  • Gustavo Queiroz
    Katalon Apprentice
    05/30/2018
    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)

    Upvote
    Quote
  • Gustavo Queiroz
    Katalon Apprentice
    05/30/2018
    I solved the problem reading the comments in  https://forum.katalon.com/discussion/6774/parameterize-a-web-service-object#latest and inserting a variable in my body! 
    Thanks!!
    Upvote
    Quote
  • Shadhi Morrow
    Katalon Apprentice
    06/01/2018
    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
    Upvote
    Quote
  • himal
    Katalon Apprentice
    06/07/2018
    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"
    Upvote
    Quote
Sign In or Register to comment.