13

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
2
  • Why aren't you using an implicit wait for the element you need to interact with next? Commented Dec 28, 2013 at 3:33
  • Because for some tests, I have to simulate "human" way of typing characters. Commented Dec 28, 2013 at 8:46

3 Answers 3

17

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
Sign up to request clarification or add additional context in comments.

1 Comment

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)
4

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
answered Sep 19, 2017 at 23:48

Comments

-1
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

1 Comment

Sleep() is not a function.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.