我正在开发 C# Selenium-WebDriver。发送密钥后,我想等待几秒钟。我执行以下代码等待 2 秒。
public static void press(params string[] keys)
{
foreach (string key in keys)
{
WebDriver.SwitchTo().ActiveElement().SendKeys(key);
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
我这样称呼:
press(Keys.Tab, Keys.Tab, Keys.Tab);
效果很好。哪一种是更好的方法?
我会不惜一切代价避免使用类似的东西,因为它会减慢测试速度,但我遇到了一个我没有其他选择的情况。
public void Wait(double delay, double interval)
{
// Causes the WebDriver to wait for at least a fixed delay
var now = DateTime.Now;
var wait = new WebDriverWait(myWebDriver, TimeSpan.FromMilliseconds(delay));
wait.PollingInterval = TimeSpan.FromMilliseconds(interval);
wait.Until(wd=> (DateTime.Now - now) - TimeSpan.FromMilliseconds(delay) > TimeSpan.Zero);
}
以某种方式观察 DOM 总是更好,例如:
public void Wait(Func<IWebDriver, bool> condition, double delay)
{
var ignoredExceptions = new List<Type>() { typeof(StaleElementReferenceException) };
var wait = new WebDriverWait(myWebDriver, TimeSpan.FromMilliseconds(delay)));
wait.IgnoreExceptionTypes(ignoredExceptions.ToArray());
wait.Until(condition);
}
public void SelectionIsDoneDisplayingThings()
{
Wait(driver => driver.FindElements(By.ClassName("selection")).All(x => x.Displayed), 250);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)