Home > Enterprise >  Check value of an xpath in if statement
Check value of an xpath in if statement

Time:11-23

Hi i am trying to check if my table is sorted by using an IF statement i am attempting to use the value i get from an xpath as follows.

Check if sorted by alphabet
sleep   3s
${stuurman}=    Stuurman
${sorted}=    Get Text    xpath:/html/body/div[1]/div/div[2]/div/div/div/div[1]/div/div[2]/table/tbody/tr[12]/td[2]
IF    ${sorted} == ${stuurman}
    Log To Console  "Sorted"
ELSE
    Log To Console  "Not sorted"
END

But i get the following error:No keyword with name 'Stuurman' found. What am i doing wrong here?

CodePudding user response:

In addition to what Matthew King answered for the variable creation, there is another problem with your code:

The expression inside the IF is passed directly to the python interpreter, with the framework substituting any variables with their values. So

${sorted} == ${stuurman}

is evaluated as

Stuurman == the text from the UI

Now this in python is actually "the variable Stuurman should be equal to the variable the text from the UI", but there are no such variables, they haven't been defined for py; thus an exception will be raised.

There are two options to make it right; as both are strings, you can surround them with quotes, and when RF substitutes them with the values, it'll be a plain string comparison:

"${sorted}" == "${stuurman}"     
# will be evaluated as:
# "Stuurman" == "the text from the UI"

The other option is to use the advanced variable syntax (I think this was the name in the RF user guide) and pass not the values, but the variables themselves. This is done by omitting the curly brackets, prefix a var just by the dollar sign.

Thus no substitution will take place, Python will get actual variables and will compare them:

$sorted == $stuurman

CodePudding user response:

When you're setting a variable within a keyword you would need to use something like "Set Variable" followed by the value. (A keyword is expected on the right side)

e.g.

${stuurman}=  Set Variable   Stuurman

You can omit a keyword when setting the variable within *** Variables *** section e.g.

*** Variables ***
${stuurman}=   Stuurman
  • Related