【发布时间】:2020-02-06 09:07:26
【问题描述】:
我正在尝试等待 WebElement 从空白变为消息 1,然后变为消息 2。问题是我每次都找到第一条消息,但我似乎永远无法等待第二条消息(它超时寻找文字)
我尝试过将等待对象分开但不起作用。我已经尝试了一些预期的条件方法(textToBePresent*),经过一些阅读(我发现关于刷新的 EC)无济于事。
@FindBy(xpath="//p[@class='statusText']")
WebElement statusMsg
public WebElement statusMsg(){
String msg1="Logging in, please wait."
String msg2="Login successful, please wait."
String msg3="Login attempt exception, error code: "
if(statusMsg.getText().contains(msg3)){
log.error(statusMsg.getText())
log.error("Something happened in the frontend")
Assert.fail(statusMsg.getText())
}else{
log.info(statusMsg.getText())
}
WebDriverWait wait = new WebDriverWait(driver,45)
wait.until(ExpectedConditions.textToBe(By.xpath("//p[@class='statusText']"), msg1))
if(statusMsg.getText().contains(msg3)){
log.error(statusMsg.getText())
log.error("Something happened in the backend")
Assert.fail(statusMsg.getText())
}else{
log.info(statusMsg.getText())
}
wait.until(ExpectedConditions.refreshed(ExpectedConditions.textToBe(By.xpath("//p[@class='statusText']"), msg2)))
log.info("Found: "+msg2)
return statusMsg
}
结果是 testNG 没有通过我的测试说:
org.openqa.selenium.TimeoutException:预期条件失败: 等待条件(By.xpath 找到的元素: //p[@class='statusText'] 有文字“登录成功,请 等待。”。当前文本:“正在登录,请稍候。”)
但我可以在测试运行时看到msg2。这是因为我已经通过PageFactory.initElements(driver, this)初始化了页面对象吗?
【问题讨论】:
-
分享您的 HTML 代码。
Login attempt exception, error code:应该出现在错误的凭据尝试之后。 -
不确定您指的是什么 HTML?登录豁免字符串不是有问题的问题,它是行
wait.until(ExpectedConditions.refreshed(ExpectedConditions.textToBe(By.xpath("//p[@class='statusText']"), msg2))) -
也许
Login successful的消息在您的页面上,但您的搜索路径错误。 -
我确实想到了这一点,但如果我禁用第一次等待,我确实会找到它。这部分测试实际上通过了,但我需要能够报告第一条消息和第二条消息。因此,如果我这样做而不是上面写的
wait.until(ExpectedConditions.textToBe(By.xpath("//p[@class='statusText']"),msg2))它会在wait.until(ExpectedConditions.textToBe(By.xpath("//p[@class='statusText']"), msg1))被注释掉时起作用
标签: java selenium selenium-webdriver groovy page-factory