所以,我有一个具有多个span元素和不同错误消息的警报。我需要测试该警报中是否包含指定的消息。例如,在下面的屏幕截图中,其中一个span元素包含“需要电子邮件字段”消息。我尝试在Python中实现它,然后在RobotFramework中将其用作关键字,但我一直在寻找当前的浏览器实例。 Parser.py文件包含:
from selenium.webdriver.common.by import By
from selenium import webdriver
def parse_alert_msg(elements, message):
alerts = driver.find_elements(By.XPATH, elements)
for item in alerts:
if item.text == message:
return True
return False
这就是我在机器人文件中的内容:
Test Function
Open Website
Login Successfully as ${UserName} ${Password}
${message} Parser.Parse Alert Msg //div[@class='alert-content']/span[@class='alert-description'] You have logged in.
Run Keyword If ${message}=${FALSE} Fail There is no such span with that text.
[Teardown] Close Browser
答案 0 :(得分:1)
在我看来,不需要循环,因为您已经可以使用xpath text()
函数进行检查:
Check If Alert Contains Errormessage
[Arguments] ${alert-message}
Page Should Contain Element
... xpath://div[@class='alert-content']/span[@class='alert-description' and text()='${alert-message}']
... message=The message "${alert-message}" is not found.
答案 1 :(得分:0)
好的,这是我来的解决方案:
Check If Alert Contains
[Arguments] ${alert-path} ${alert-message}
@{alert-msg} Get Web Elements ${alert-path}
: FOR ${element} IN @{alert-msg}
\ ${element_text} Get Text ${element}
\ Run Keyword If '${element_text}' == '${alert-message}' Exit For Loop
Run Keyword Unless '${element_text}' == '${alert-message}' Fail The message "${alert-message}" is not found.
如果有人能看到更好的方法,请随意发布:)。
编辑: 我觉得这是我能得到的最好的答案。无需循环和/或实现过于复杂的逻辑。
Wait Until Alert Contains
[Arguments] ${message}
Wait Until Element Is Visible //div[@class='alert-content']
Element Should Contain //div[@class='alert-content'] ${message}