如果是Page Factory模型,请分享您在超时实施方面的经验。
如果使用简单的变量(var loginButton = driver.FindElement(By.Id("login")
),则可以进行如下扩展:
public static class WebDriverExtensions
{
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
}
代码属于用户Loudenvier
Reference to original discussion
但我无法向FindsByAttribute密封类添加属性。此时我的WebElements看起来像:
[FindsBy(How = How.Id, Using = "tbUsername")]
private IWebElement Username;