使用 WatiN 和 NUnit 进行单元测试
似乎有关 WatiN 的问题中的共同主题与实际让事情正常运转有关,我也不例外。
我已经下载了最新版本的 WatiN (2.0.20.1089),并尝试创建相当于 Hello, World 的 NUnit / WatiN:
using WatiN.Core;
using NUnit.Framework;
namespace Foo.Browser.Tests
{
[TestFixture]
public class BrowserTests
{
[Test]
[STAThread]
public void ExampleTest()
{
IE ie = new IE("http://www.google.com");
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByValue("Google Search")).Click();
Link link = ie.Link(Find.ByUrl("http://watin.sourceforge.net/"));
Assert.That(link.Text == "WatiN Home");
}
[Test]
public void FirefoxTest()
{
FireFox ff = new FireFox("http://www.google.com");
ff.TextField(Find.ByName("q")).TypeText("WatiN");
ff.Button(Find.ByValue("Google Search")).Click();
Link link = ff.Link(Find.ByUrl("http://watin.sourceforge.net/"));
Assert.That(link.Text == "WatiN Home");
}
}
这会在最终超时后提示 IE(8),并显示以下堆栈跟踪:
在 WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.ThrowTimeOutException(异常 最后异常,字符串消息)位于 WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.HandleTimeOut() 在 WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.Try[T](DoFunc'1 函数)在 WatiN.Core.WaitForCompleteBase.WaitUntil(DoFunc'1 等等, 构建超时异常消息 异常消息)位于 WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitWhileFrameDocumentNotAvailable(IWebBrowser2 帧)在 WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(IHTMLDocument2 主文档)位于 WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(IHTMLDocument2 主文档)位于 WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForCompleteOrTimeout() 在 WatiN.Core.WaitForCompleteBase.DoWait() 在 WatiN.Core.DomContainer.WaitForComplete(IWait 等待完成)在 WatiN.Core.IE.WaitForComplete(Int32 waitForCompleteTimeOut)于 WatiN.Core.DomContainer.WaitForComplete() 在 WatiN.Core.Browser.GoTo(Uri url) 在 WatiN.Core.IE.FinishInitialization(Uri 乌里)在 WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri,IDialogHandler 登录对话框处理程序,布尔值 创建新进程)在 WatiN.Core.IE..ctor(字符串 url) 位于 Foo.Browser.Tests.BrowserTests.ExampleTest() 在 C:\Development\Foo\Foo.Browser.Tests\BrowserTests.cs:line 19
我的 bin\debug 文件夹 (Foo.Browser.Tests.dll.config) 中有必要的配置文件,如 WatiN 文档页面,那么它还可能是什么? 有人对可能导致问题的原因有任何建议吗?
It seems that the common theme among SO questions regarding WatiN have to do with actually getting the thing working and I'm no exception.
I've downloaded the latest release of WatiN (2.0.20.1089) and am trying to create the NUnit / WatiN equivalent of Hello, World:
using WatiN.Core;
using NUnit.Framework;
namespace Foo.Browser.Tests
{
[TestFixture]
public class BrowserTests
{
[Test]
[STAThread]
public void ExampleTest()
{
IE ie = new IE("http://www.google.com");
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByValue("Google Search")).Click();
Link link = ie.Link(Find.ByUrl("http://watin.sourceforge.net/"));
Assert.That(link.Text == "WatiN Home");
}
[Test]
public void FirefoxTest()
{
FireFox ff = new FireFox("http://www.google.com");
ff.TextField(Find.ByName("q")).TypeText("WatiN");
ff.Button(Find.ByValue("Google Search")).Click();
Link link = ff.Link(Find.ByUrl("http://watin.sourceforge.net/"));
Assert.That(link.Text == "WatiN Home");
}
}
This tips IE(8) over after an eventual timeout with the following stack trace:
at
WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.ThrowTimeOutException(Exception
lastException, String message) at
WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.HandleTimeOut()
at
WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.Try[T](DoFunc'1
func) at
WatiN.Core.WaitForCompleteBase.WaitUntil(DoFunc'1
waitWhile,
BuildTimeOutExceptionMessage
exceptionMessage) at
WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitWhileFrameDocumentNotAvailable(IWebBrowser2
frame) at
WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(IHTMLDocument2
maindocument) at
WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(IHTMLDocument2
maindocument) at
WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForCompleteOrTimeout()
at
WatiN.Core.WaitForCompleteBase.DoWait()
at
WatiN.Core.DomContainer.WaitForComplete(IWait
waitForComplete) at
WatiN.Core.IE.WaitForComplete(Int32
waitForCompleteTimeOut) at
WatiN.Core.DomContainer.WaitForComplete()
at WatiN.Core.Browser.GoTo(Uri url) at
WatiN.Core.IE.FinishInitialization(Uri
uri) at
WatiN.Core.IE.CreateNewIEAndGoToUri(Uri
uri, IDialogHandler
logonDialogHandler, Boolean
createInNewProcess) at
WatiN.Core.IE..ctor(String url) at
Foo.Browser.Tests.BrowserTests.ExampleTest()
in
C:\Development\Foo\Foo.Browser.Tests\BrowserTests.cs:line
19
I have the requisite config file in my bin\debug folder (Foo.Browser.Tests.dll.config) as specified on the WatiN documentation page, so what else is it likely to be?
Does anyone have any suggestions for what might be causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Phil,
看一下抛出的 TimeoutException 的 InnerException。这很可能包含更多信息来解决您的问题。
华泰
杰罗恩·范梅宁
首席开发人员 WatiN
Phil,
Have a look at the InnerException of the thrown TimeoutExcepetion. This will most probably contain more info to solve your issue.
HTH
Jeroen van Menen
Lead dev WatiN
看起来菲尔可能已经回答了你的问题,但我发现你的例子也抛出异常。为了解决上述问题,我将链接查找更改为按属性 (href) 查找,如下所示。
请注意,我只测试了 IE,因为这个盒子没有安装 FireFox。
Looks like Phil may have answered your question, but I found your example to throw exceptions as well. To fix said issue I changed the Link Find to look by attribute (href) as below.
Note that I only tested for IE as this box doesn't have FireFox installed.
我对此代码有类似的问题
,因此解决了......
I had a similar issue with this code
which was resolved thus...
我自己通过 xUnit.net WatiN 时没有遇到大问题。
这里有一个详细清单,人们可以随意编辑和/或建议更新:
令我困扰的一个大问题是,在 WS08 上进行数千次调用后,IE 将拒绝合作,直到您注销并再次登录(请参阅我的未决问题之一)。我走的路线(我需要防故障监控)是使用 WatiN 记录器,然后将它们移植到 HtmlAgilityPack,使用 LINQ 查询,使用 WebRequest 手动编码 GET/POST 内容,效果很好,但显然不能处理 JavaScript,这显然会渲染对于许多场景来说它都是学术的。
Didnt run into big issues WatiN via xUnit.net myself.
Here's a laundry list, which people should feel free to edit into and/or suggest updates to:
One big gotcha that bit me wrt WatiN is that after a few thousand invocations on WS08 IE will refuse to cooperate until you log off and on again (see one of my outstanding questions). The route I went down (I needed fail-proof monitoring) was to use the WatiN recorder and then port those to HtmlAgilityPack with LINQ querying with hand-coded GET/POST stuff using WebRequest which works quite well but obviously doesnt handle JavaScript which obviously renders it academic for many scenarios.
关于注销/登录情况,我遇到了与鲁本类似的问题。到目前为止我能找到的最好的推理与 WatiN 使用 COM 服务器运行 IE 的事实有关。处理方面可能存在问题,但这完全是另一个 Oprah...
就超时情况而言,它时不时地发生,但它通常会在 NUnit 控制台中弹出一条消息,然后继续。我发现你的实现和我的实现之间的一大区别是我将 IE 实例作为单例对象运行。这样,我就不必在测试之间关闭并重新打开 IE,并且从那以后我就没有遇到过注销/登录问题。因此,即使 IE 可见并且不在后台运行,我的测试周期也会更短。
I experienced similar problems to Ruben's regarding the logoff/logon situation. The best reasoning I've been able to find so far has to do with the fact that WatiN is running IE using a COM server. There may be issues regarding the disposal, but that's a whole other Oprah...
As far as the timeout situation goes, it happens every now and then, but it typically pops up a message in the NUnit console, and continues on. One big difference that I can see between your implementation and mine is that I'm running the IE instance as a singleton object. That way, I never have to close and reopen IE between tests, and I haven't experienced the logoff/logon issue since. Consequently, my test cycles are shorter, even when IE is visible and not running in the background.