2017-04-14 26 views
2

如果其中一个条件成立,我需要检查将要通过的条件。如何使用OR条件来处理Robot Framework中的关键字?

像我想要使用的关键字是如下:

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 

使用运行关键字如果,但它不工作

回答

4

你可以加入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 
+0

非常感谢你@ ombre42您的帮助:) 我发现我的问题替代的解决方案,从你的回答再次 谢谢:) – Umesh

+0

@Umesh这本来是巨大的,如果你张贴在这里您的解决方案,因为我期待替代xpath以及=) – Shnigi

+0

@Shnigi我想检查用户状态,无论是在线,离线,离开,忙碌。但稍后,我使用“获取匹配Xpath计数”显示了用户数量以及状态,如下所示: $ {USERS_ONLINE}获取匹配Xpath计数// div [@ class ='participants-list'] // span [@ [= class ='status available'] Log $ {USERS_ONLINE} user available(s)available $ {USERS_OFFLINE}获得匹配Xpath计数// div [@ class ='participants-list'] // span [@ class ='status unavailable '] 登录$ {USERS_OFFLINE}个用户离线 – Umesh

1

什么你问能不能用Run Keyword And Return Status呼叫组合来完成。

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

虽然这种方法很简单,它是次优的 - 它会检查每一个元素,即使第一个可能存在,并且没有需要检查的第二,第三和第四位。这(很expresive)版本将做到这一点:

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

它使用Run Keyword If返回称为关键字的值的事实。总之,当应该检查所有条件(Page Should Contain Element)(在末端具有相应的逻辑条件 - 与and连接,而不是or,如本示例中)时,第一种方法更好地使用。
第二 - 只有一个就足够了,其余的不应该检查,如果发现一个是真的。

相关问题