1

我目前有一个使用 Selenium WebDriver 和 .NET Framework 的解决方案,使用以下代码创建与 Web 元素交互的等待,作为我们自动化解决方案的一部分。

wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(60));

这适用于等待元素加载到屏幕中,但它不包含最有用的消息,例如,如果元素不可点击,则返回以下内容:

Message: OpenQA.Selenium.WebDriverTimeoutException : Timed out after 60 seconds

而不是 NoSuchElement 异常,它标识要输入的记录。

当尝试验证记录值时,这将成为一个更大的问题,例如 wait.Until(webdriver => element.GetAttribute("value") == expectedValue);

因为这会返回超时问题,而不是实际声明差异是什么,例如 Nunit Assert 会给你,我在这里用以下方法解决这个问题

try
{
customWait.Until(webdriver => element.GetAttribute("value") == expectedValue);
}
catch
{
// to display assertion error when unable to validate attribute in given time
Assert.AreEqual(expectedValue, element.GetAttribute("value"));
}

有没有更好的方法可以用内部错误的原始消息覆盖默认超时响应,例如在尝试查找元素 x 秒后返回 NoSuchElement 异常,而不是 TimeoutException。

谢谢

4

0 回答 0