I'm working on a C# Selenium-WebDriver. After send key, I want to wait few seconds. I do the following code to wait for 2 seconds.
public static void press(params string[] keys)
{
foreach (string key in keys)
{
WebDriver.SwitchTo().ActiveElement().SendKeys(key);
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
And I call like these:
press(Keys.Tab, Keys.Tab, Keys.Tab);
It works fine. Which one is a better way?
asked Dec 27, 2013 at 10:10
LeMoussel
5,80915 gold badges78 silver badges130 bronze badges
-
Why aren't you using an implicit wait for the element you need to interact with next?Curtis Miller– Curtis Miller2013年12月28日 03:33:36 +00:00Commented Dec 28, 2013 at 3:33
-
Because for some tests, I have to simulate "human" way of typing characters.LeMoussel– LeMoussel2013年12月28日 08:46:40 +00:00Commented Dec 28, 2013 at 8:46
3 Answers 3
I would avoid at all cost using something like that since it slows down tests, but I ran into a case where I didn't had other choices.
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);
}
It's always better to observe the DOM somehow, e.g.:
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);
}
answered Jan 8, 2014 at 19:23
Julien Bérubé
1,2661 gold badge13 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
LeMoussel
Julien,
delay is the timeout after which the condition is deemed to have failed. Is it possible to specify interval at which the condition function is polled to determine whether the wait has succeeded? For ex : Wait(Func<IWebDriver, bool> condition, double delay, double interval)You can use Thread.Sleep(miliseconds) function. This stop your C# thread and selenium continue
[TestMethod]
public void LoginTest()
{
var loginForm = driver.FindElementByXPath("//form[@id='login-form']");
if (loginForm.Enabled)
{
MainPageLogin(loginForm);
CheckLoginForm();
}
else
{
CheckLoginForm();
}
**Thread.Sleep(5000);**
Assert.AreEqual(driver.Url, "https://staging.mytigo.com/en/");
}
Ashish Kamble
2,8254 gold badges23 silver badges30 bronze badges
Comments
public static void press(params string[] keys)
{
foreach (string key in keys)
{
WebDriver.SwitchTo().ActiveElement().SendKeys(key);
Sleep(2000);
}
}
which serves the same
answered Dec 27, 2013 at 10:26
Mahesh Reddy Atla
5295 silver badges12 bronze badges
1 Comment
User
Sleep() is not a function.lang-cs