Change value of a variable used in a variable

Hi, as I want to eliminate code duplication I am trying to create a variable with element xpath which has another variable in it. I created a keyword with argument and then set the value of the variable in the xpath as you can see in the example below.

*** Variables ***
${locator} //*[text() = β€˜${helpVar}’]
${helpVar}
*** Keywords ***
Is it visible?
[Arguments] ${text}
${helpVar} Set Variable ${text}
Get Element States ${locator} contains visible

${text} this value can be form1 for example
I tried:
Log to console ${helpVar} - this logs form1 - so it changed
Log To Console ${locator} - but this variable is not affected as it still has no value in it: //*[text() = β€˜β€™]

Please could anyone point me the right direction?

Thank you in advance.

I dont think thats possible even within python unless you use templates of some sort. Value of the variable is evaluted at when its defined or value is stored.

On python side something like this would be done with str.format() or with string.Template class. You could try to minic either one.

Hi Pavel,

As you discovered the contents of ${helpVar} was evaluated at the time ${locator} was declared, so you can’t really do this as a global variable.

You could do it like this though if that helps:

*** Keywords ***
Is it visible?
    [Arguments]    ${text}
    ${helpVar}    Set Variable    ${text}
    ${locator}    Set Variable    //*[text() = β€˜${helpVar}’]
    Get Element States    ${locator}    contains visible

Dave.

1 Like

Thank you for clarification, I think I will try to create some keyword for the xpath and reuse the keyword instead of the variable.

Thanks a lot
Pavel

1 Like