问题
I have two questions related to the same problem... Q1) I am using WatiN(3.5) for automation of a website. The situation is that I want to obtain a div tag when the result page is fully loaded but WatiN don't wait for that page to be campletely loaded and tries to obatin that div which results in getting div with null. This div is populated by AJAX. This is th code that I am using to avoid that error but it does not work.
while (resultDiv == null)
{
browser.Div("ui-tabs-1").WaitUntilExists();
resultDiv = browser.Div("ui-tabs-1");
}
So how I can wait for a page to be completely loaded by using WatiN?
Q2) I found a solution for above problem here but I stuck at a point as I could not find a reference of library for these interfaces i.e. IElement and IBrowser. These interfaces are bring used in the extension methods. I have also asked the author of that article and waiting for his reply. I am making this apllication by usng WatiN 2.5 and .Net framework 3.5 in VS 2010.
回答1:
I have ran into similar problem with watin on a site using Ajax. This is the workaround for this.
//After click on link/Tab/Button on which the result is loaded in non Ajax websites.
We have a function here, browser.WaitForComplete()
but it works only when the page is in loading state. but in case of Ajax on a part of browser window gets updated. so no loading state for browser.
So one solution for this problem is
Use Thread.Sleep(10000);
This time can vary upon the normal time the website takes to load the required div.
回答2:
Thread.Sleep can be used but for anything other than a proof of concept that waiting for something to load is indeed the issue Thread.Sleep should be avoided. Sleeps add in unnecessary idle time if you sleep for the max time the action is going to take, and give false positive failures when waiting less time.
See Jeroen's link in his response here if you are testing an ASP.NET Ajax site: In WatiN how to wait until postback is complete - WaitForAsyncPostbackToComplete. I used this idea for some methods and properties to rid my code of a lot of long Sleep calls. Tests ran faster and results were much more reliable.
If the specific JS call won't work as you're using a different clientside framework, using the basic polling concept with shorter sleeps in a loop is going to do you better than long sleeps.
来源:https://stackoverflow.com/questions/18473103/waiting-for-page-to-be-fully-loaded-using-watin