Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I'm using Selenium 2 tests (written in C#) that choose values from a "select" control. Selection causes a post-back to the server, which updates the state of the page. I am therefore performing a manual wait (thread.sleep) after choosing a value to wait for the page to be changed. and it works fine with Thread.Sleep. However, Thread.Sleep is a bad idea to use with number of good reasons so when I take out all my Thread.Sleep line of code then all my test cases fall apart and I have tried WebDriverWait, Implicitly and Explicitly none works and very frustration

below is the sample code that I have tried....

//WebDriverWait

 public IWebElement WaitForElement(By by)
 {
            // Tell webdriver to wait
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            wait.PollingInterval = TimeSpan.FromSeconds(2);
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(NoSuchFrameException));
            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException), typeof(StaleElementReferenceException));

            IWebElement myWait = wait.Until(x => x.FindElement(by));
            return myWait;
}

Tried this too:

   WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));

//Implicitly:

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

//Explicit Wait:

IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("someDynamicElement"));
    });
share|improve this question
    
What do you mean by it does not work? Are there any errors or that the statement does not result in wait? –  Suchit Parikh Oct 19 '12 at 23:56
    
meaning, i get stale and element not found exception –  Abu Hamzah Oct 23 '12 at 17:47
    
2 seconds may not be long enough which would result in the Element not found exceptions. The stale element exceptions imply that the element you are looking for is changing. You should wait for the element to become stale before waiting for it to appear again. –  Ardesco Feb 4 '13 at 22:33
add comment

2 Answers 2

Does the “select” control require refresh page? If so, you might have to add the following line inside WebDriverWait.

driver.navigate().refresh(); //refresh the page (java code.  C# should have a similar function) 

Also, you cannot use the both (Implicitly wait and WebDrvierWait ) inside the same test. You have to nullify ImplicitlyWait before calling WebDriverWait because ImplicitlyWait controls the “d.FindElement()” wait time. For example, if you set 30 seconds Implicitly Wait time, “d.FindElement()” will also wait 30 seconds. So, the fourth example WebDriverWait will not work.

//nullify implicitlyWait (java code)  
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); 

There is WaitTool utility class at Github. It is written in Java (similar to C#). It shows how to use ImplicitlyWait and WebDriverWait in the same test.

Information here, and WaitTool here

share|improve this answer
    
So you are saying I cant do this: element=wait.until(ExpectedConditions.visibilityOf( driver.findElement( locator ) ) ); ? –  djangofan Mar 7 '13 at 18:17
add comment

When you need to wait for something to happen, there are two ways to proceed. The first is to guess how long you need to wait, sleep for that interval, and then proceed under the assumption that you have waited long enough. That is ill-advised unless you know without a doubt that you waited long enough.

The second way is to poll in a loop. Sleeping in a polling loop is a standard practice in UI automation. There is nothing wrong with that.

Can you expand your question to describe why it is bad to use thread.sleep?

share|improve this answer
    
That's sleeping for 8 seconds (example). Every Single Time What happens if the element you're looking for is present in the page after 2 seconds? You're waiting an extra 6 seconds that you don't have to. –  Abu Hamzah Oct 23 '12 at 2:44
    
Yes, if you make a bad guess at the sleep time, you will either sleep too long or poll too much. But that is not an indictment of sleeping or polling. –  user246 Oct 23 '12 at 13:04
    
Sleeps are bad because they add to the test time for no good reason. They are different on different machines, your big beefy i7 may only have to wait 2 seconds whilst an old P100 running the same site may have to wait 15 seconds. Thread.sleep is the lazy solution used by people who can't be bothered to do it properly/don't understand how to do it properly. –  Ardesco Feb 4 '13 at 22:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.