0

I have a question about waits in selenium and want to know what is the best way to just perform a general wait.

Virtually I type into a test bar and then a drop down appears. The design in the HTML is bad so what I need to do is:

1: write my text in the test input which is below

    WebDriverWait waitAgency = new WebDriverWait(_driver, TimeSpan.FromSeconds(3));
    waitAgency.Until(ExpectedConditions.ElementToBeClickable(By.Id(_webElement["agency"])))
        .SendKeys("test agent");

2: I will then just need to wait 2 seconds becasue the drop down may need to load up the value. This is what I need help with.

3: I select the 'TAB' button so it selects the value:

 _driver.FindElement(By.Id(_webElement["agency"])).SendKeys("TAB");

My question is in regards to point 2, what is the best way to just wait for 2 seconds before selecting the 'TAB' button? I have nothing to wait until, I just want to wait 2 seconds.

Thanks,

BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
  • I would read this thread - https://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present – Dazed Aug 16 '17 at 13:54

5 Answers5

2

if you just want to wait for 2 seconds, (which I don't think as a good practice) you could just simply pause your thread.

Thread.Sleep(2000); //time is in milliseconds

To better thing would be to wait the drop-down element is visible.

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
2

You can use something like this:

public static void Wait(int miliseconds, int maxTimeOutSeconds = 60)
{
    var wait = new WebDriverWait(Driver, new TimeSpan(0, 0, 1, maxTimeOutSeconds));
    var delay = new TimeSpan(0, 0, 0, 0, miliseconds);
    var timestamp = DateTime.Now;
    wait.Until(webDriver => (DateTime.Now - timestamp) > delay);
}

details here

Mykola
  • 352
  • 1
  • 9
  • While the code is different, this really isn't any different than a `Thread.Sleep()` which is a bad practice. The point is waiting for a hardcoded time to expire is the bad practice. Best practice is to wait for the specific element, etc. to be actionable so that the script continues as soon as it can. – JeffC Aug 16 '17 at 17:19
  • @JeffC Agree about bad practices, especially pausing Threads unless it's really a necessity. Would be nice to see a few examples of good practices in C#. I am nore into Java, so could provide good solid examples, but it's not the point of this topic – Mykola Aug 16 '17 at 17:27
  • OP has an example in this question using an explicit wait in C#, which is a best practice. There are many others on the web if you do a little searching. – JeffC Aug 16 '17 at 17:29
0

I have a 3 point resolution for 3 of your steps, one for each of your steps as follows:

  1. When you write the text in the test input, if the page was loaded previously then you don't need to induce any ExplicitWait. ExplicitWait stands meaningless there. So you can remove the ExplicitWait.
  2. As you need to wait 2 seconds for the dropdown to be loaded with values (through JavaScript/AjaxCalls) you need a ExplicitWait there. So induce a ExplicitWait when you start looking for the Dropdown elements with xpath or css.
  3. Next you are selecting an option through the 'TAB' button to select a value which is not_so_perfect approach. If the control is a simple Dropdown (Select class) you can use the options either by selectByIndex(), selectByValue() or selectByVisibleText(). Else if the dropdown (auto-suggestions) are in seperate div, ul or li, try to select through id, name or construct an unique xpath or a css to click/select a value.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Waiting for a hard-coded 2 seconds is a bad practice. There's plenty of info on the web describing why but Thread.Sleep() or any wait that waits, not for an event, but for a fixed time all falls into that category.

For step 2, why not wait until the element you are looking for is clickable? If it's an autosuggest list or something similar, the HTML for the list will be built so you can wait for the element you want to be clickable and then just click it (instead of using TAB).

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

You can set ImpicitWait. By default, ImplicitWait set to 0 milliseconds. Change it to the amount of time you want it to wait.

_driver.GetDriver().Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
OriBr
  • 324
  • 2
  • 13