How can I ask the Selenium-WebDriver to wait for few seconds after sendkey?

Viewed 19539

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?

3 Answers

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/");
}
Related