Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this code snippet

*** Keywords ***
My Keyword
  [Arguments]  ${arg}
  ${xpath}  Set Variable  really long dynamic xpath using ${arg}
  Do Something With  ${xpath}
  etc.

My Other Keyword
  [Arguments]  ${arg}
  ${xpath}  Set Variable  really long dynamic xpath using ${arg}
  Do Something Totally Different With  ${xpath}
  etc.

To follow programming best practices, I want to have the xpath (the same in both keywords) defined at single place only. I tried to modify it like this

*** Variables *** 
${xpath_template}  really long dynamic xpath using ${arg}

*** Keywords ***
My Keyword
  [Arguments]  ${arg}
  ${xpath}  Evaluate  ${xpath_template}
  Do Something With  ${xpath}
  etc.

My Other Keyword
  [Arguments]  ${arg}
  ${xpath}  Evaluate  ${xpath_template}
  Do Something Totally Different With  ${xpath}
  etc.

In other words, I need the substitution of ${arg} in ${xpath_template} be defered until the moment ${arg} is defined. However, the code above does not perform it, nor my similar experiments when I tried to force Set Variable or ${{...}} to do it for me... Can you help me, please, to have the xpath be only once in my code?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
805 views
Welcome To Ask or Share your Answers For Others

1 Answer

The solution is to use the Replace Variables keyword.

*** Variables ***
${xpath_template}  blah blah ${arg}

*** Keywords ***
My Keyword
    [Arguments]  ${arg}
    ${xpath}  Replace variables  ${xpath_template}
    Return from keyword  ${xpath}

*** Test Cases ***
Example
    ${xpath}=  My keyword  Hello, world
    Should be equal  ${xpath}  blah blah Hello, world

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...