如何在Robot Framework中使用OR条件?

时间:2017-04-14 11:07:26

标签: if-statement robotframework

如果其中一个条件成立,我需要检查将要通过的条件。

我想使用的关键字如下:

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

使用运行关键字但无法使用

2 个答案:

答案 0 :(得分:5)

您可以使用|连接XPath结果集做一个等同于OR的东西。

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

如果您只是想要失败,如果找不到任何XPath:

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

答案 1 :(得分:1)

您可以通过${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 来电的组合来完成您的要求。

${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

虽然这种方法很简单,但它不是最理想的 - 它会检查每个元素,即使第一个元素可能存在,也不需要检查第2个,第3个和第4个元素。这个(非常有表现力的)版本会这样做:

Run Keyword If

它使用Page Should Contain Element返回被调用关键字的值的事实。

总之,当应检查所有条件(and)时最好使用第一种方法(最后使用相应的逻辑条件 - 与or相关联,而不是{{1}}在这个例子中) 第二个 - 只要一个就足够了,如果发现其中一个是真的,则不应检查其余部分。