Set a variable value in a Locator

Hello,
While making generic Keywords, I like to use a variable in the locator string.

ANY_TAB_NAME: ul[role=“menu”] >> a[aria-label=“${tab_name}”]

Then , in the keyword, I use an argument to fill-in the variable value in the locator.
But by default that does not work: the locator string will be used as is and not with the actual argument value .

My workaround is to use the kw “Replace Variables” that replaces the variable in the Locator by their actual value from the argument (matching the variable name).
Is that the best way of using a variable in the locator ?

1 Like

@spicard The code below should work.

*** Variables ***
${ANY_TAB_NAME}  ul[role=“menu”] >> a[aria-label=“tab_name”]
 
*** Keywords ***
Click any tab by name
    [Arguments]  ${tab_name}
    ${locator}=  Replace variables  ${ANY_TAB_NAME}
    Log To Console    ${locator}
 
*** Test Cases ***
Test Case 1
    Click any tab by name    funny_tab_name

Instead of ${ANY_TAB_NAME} ul[role=“menu”] >> a[aria-label=“tab_name”] you can also write ${ANY_TAB_NAME} ul[role=“menu”] >> a[aria-label=“\${tab_name}"]

1 Like