Hi i am using WatiN (version 2.0.10.928) with NUnit (2.5.2.9222)
if I have something like
[Test]
public void WebPageTest()
{
string url = "www.google.com";
IE ie = new IE(url);
ie.TextField(Find.ByTitle("Google Search")).TypeText("Watin");
ie.Button(Find.ByName("btnG")).Click();
ie.Element(Find.B开发者_StackOverflow社区yText("WatiN")).Click();
// ie.WaitForComplete();
Assert.IsTrue(ie.Text.Contains("Welcome at the WatiN"));
ie.Close();
}
Then usually this will work and the test will pass but sometimes when I hit the assert it seems that Watin hasn't finished loading the page and is still on the previous page. I have this problem using the IE.Text or the IE.Url properties. I tried using WaitForComplete() (even though that shouldn't be neccessary) but still sometimes have the same problem.
Has Anybody had this problem with WatiN before? Has anybody succesufully managed to use WatiN with NUnit like this? Or Maybe it would work better with a different unit testing framework like MBUnit? Has anyone had better luck with MBunit?The test framework you use will make no difference, I'm afraid -- this is one of the "gotchas" of any screen-scraping test framework, and WaTin is no different.
The WaitForComplete()
call is definitely necessary, I'm afraid.
Some of my colleagues have reported that the version of IE can make a difference; IE6 in particular has some internal timing issues that can cause problems. IE8 appears to be quite a bit better.
I've had the same problem with my tests; unfortunately it doesn't seem as though you can assume that the WaitForComplete()
that's supposed to be inherent in the Click()
method will function correctly. Even explicitly calling WaitForComplete()
afterward hasn't always worked.
As a last resort we have used System.Threading.Thread.Sleep(int timeout_in_milliseconds)
to force the browser to give the page time to load. This isn't a completely bulletproof means of doing it, but it has eliminated about 90% of these sorts of errors. For the timeout we have used anything from 500 to 2000 milliseconds, depending on how long it takes to load and how quickly we want the test to run.
Try using
[Test]
public void WebPageTest()
{
string url = "www.google.com";
IE ie = new IE(url);
ie.TextField(Find.ByTitle("Google Search")).TypeText("Watin");
var btnG = ie.Button(Find.ByName("btnG"));
btnG.ClickNoWait();
ie.WaitForComplete(400);
var elementWatin = ie.Element(Find.ByText("WatiN"));
elementWatin.ClickNoWait();
ie.WaitForComplete(400);
Assert.IsTrue(ie.Text.Contains("Welcome at the WatiN"));
ie.Close();
}
Thanks Gandhi Rajan
I have used ie.WaitForComplete() but it still does enforce waiting, and sometimes it times out, so I use
Settings.AttachToBrowserTimeOut = 200;
Settings.WaitForCompleteTimeOut = 200;
This worked for me.
精彩评论