How to use OR condition for Keywords in Robot Framework?

11,310

Solution 1

You can join XPath result sets with the | to do what is equivalent to an OR.

${count}    Get Matching Xpath Count    //div[@prop='value'|//table[@id='bar']|//p
Run Keyword If    ${count} > 0    Some Keyword

If you just want to fail if none of the XPaths are found:

Page Should Contain Element    xpath=//div[@prop='value'|//table[@id='bar']|//p

Solution 2

What you ask can be done with combination of Run Keyword And Return Status calls.

${el1 present}=  Run Keyword And Return Status     Page Should Contains Element    //some xpath1
${el2 present}=  Run Keyword And Return Status     Page Should Contains Element    //some xpath2
${el3 present}=  Run Keyword And Return Status     Page Should Contains Element    //some xpath3
${el4 present}=  Run Keyword And Return Status     Page Should Contains Element    //some xpath4

Run Keyword If  not ${el1 present} or not ${el2 present} or not ${el3 present} or not ${el4 present}   Fail  None of the elements is present on the page

Though this approach is straightforward, it is suboptimal - it will check for every element, even though the first may be present, and there is no need to check for the 2nd, 3rd and 4th. This (very expresive) version will do that:

${some element present}=  Run Keyword And Return Status     Page Should Contains Element    //some xpath1
${some element present}=  Run Keyword If  not ${some element present}    Run Keyword And Return Status     Page Should Contains Element    //some xpath2    ELSE    Set Variable    ${some element present}
${some element present}=  Run Keyword If  not ${some element present}    Run Keyword And Return Status     Page Should Contains Element    //some xpath3    ELSE    Set Variable    ${some element present}
${some element present}=  Run Keyword If  not ${some element present}    Run Keyword And Return Status     Page Should Contains Element    //some xpath4    ELSE    Set Variable    ${some element present}

Run Keyword If  not ${some element present}   Fail  None of the elements is present on the page

It uses the fact that Run Keyword If returns the value of the called keyword.

In summary, the first approach is better used when all conditions (Page Should Contain Element) should be checked (with the corresponding logical condition at the end - connected with and, not or as in this example).
The second - when just one is sufficient, and the rest shouldn't be checked if one is found to hold true.

Share:
11,310
Umesh
Author by

Umesh

Updated on June 05, 2022

Comments

  • Umesh
    Umesh over 1 year

    I am in need to check the condition that is going to be passed if one of the conditions being true.

    The Keywords like I want to use is as below:

    Page Should Contains Element    //some xpath    OR 
    Page Should Contains Element    //some xpath    OR 
    Page Should Contains Element    //some xpath    OR 
    Page Should Contains Element    //some xpath
    

    Used Run Keyword If but it is not working

  • Umesh
    Umesh over 6 years
    Thank you very much @ombre42 for your help :) I found alternate solution for my question from your answer Thanks again :)
  • Shnigi
    Shnigi over 6 years
    @Umesh It would have been great if you posted your solution here as I am looking alternative for xpath as well =)
  • Umesh
    Umesh over 6 years
    @Shnigi I was wanting to check for the user status whether it is online, offline, away, busy. But later on, I have displayed the number of user along with status using "Get Matching Xpath Count" like: ${USERS_ONLINE} Get Matching Xpath Count //div[@class='participants-list']//span[@class='status available'] Log ${USERS_ONLINE} user(s) available ${USERS_OFFLINE} Get Matching Xpath Count //div[@class='participants-list']//span[@class='status unavailable'] Log ${USERS_OFFLINE} user(s) offline