2017-04-14 27 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,如本示例中)時,第一種方法更好地使用。
第二 - 只有一個就足夠了,其餘的不應該檢查,如果發現一個是真的。

相關問題