使用 WatiN 和 NUnit 进行单元测试

发布于 2024-08-18 02:30:27 字数 2601 浏览 7 评论 0原文

似乎有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断舍离 2024-08-25 02:30:27

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

谎言月老 2024-08-25 02:30:27

看起来菲尔可能已经回答了你的问题,但我发现你的例子也抛出异常。为了解决上述问题,我将链接查找更改为按属性 (href) 查找,如下所示。

[Test]
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.By("href", "http://watin.sourceforge.net/"));

    Assert.That(link.Text == "WatiN Home"); 
}

请注意,我只测试了 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.

[Test]
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.By("href", "http://watin.sourceforge.net/"));

    Assert.That(link.Text == "WatiN Home"); 
}

Note that I only tested for IE as this box doesn't have FireFox installed.

清风挽心 2024-08-25 02:30:27

我对此代码有类似的问题

var link1 = WebBrowser.Current.Link(Find.ByText("Log Off"));
var exists = link1.Exists;

,因此解决了......

var link1 = WebBrowser.Current.Link(Find.ByText("Log Off"));
link1.WaitUntilExists();
var exists = link1.Exists;

I had a similar issue with this code

var link1 = WebBrowser.Current.Link(Find.ByText("Log Off"));
var exists = link1.Exists;

which was resolved thus...

var link1 = WebBrowser.Current.Link(Find.ByText("Log Off"));
link1.WaitUntilExists();
var exists = link1.Exists;
蓝眼泪 2024-08-25 02:30:27

我自己通过 xUnit.net WatiN 时没有遇到大问题。

这里有一个详细清单,人们可以随意编辑和/或建议更新:

  • NUnit 流程是管理员吗? IE 是它的生成管理员吗?看看任务管理器。 (并不是说以管理员身份运行是万能的 - 您最好希望避免这种情况,而是作为消除其不起作用的原因的一种方法)
  • 通过 TestDriven.net、CodeRush 或 ReSharper 运行有帮助吗?
  • 您是否[引用了相关 dll 作为获取]依赖 DLL(例如与测试程序集并置的 shdocvvw.dll)?
  • 您是否尝试过查看 Fiddler 是否显示超时?
  • 您是否尝试过手动浏览以确保上下文中的所有安全对话框均已正常?例如,有时您的测试上下文可能是管理员,而您的正常浏览配置文件可能不是(取决于浏览器) - 例如,如果您忽略了一个上下文中的证书警告,但没有忽略另一个上下文中的证书警告,则可能会导致问题。

令我困扰的一个大问题是,在 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:

  • Is the NUnit process admin? Is the IE its spawning admin? have a look in Task Manager. (Not that running as admin is a panacea - you ideally want to avoid this, but as a way of eliminating reasons for it not to work)
  • Does running via TestDriven.net, CodeRush or ReSharper help?
  • Have you [referenced the relevant dlls as a way to get] the dependent DLLs such as shdocvvw.dll sitting alongside your test assembly] ?
  • Have you tried seeing if Fiddler shows anything timing out?
  • Have you tried browsing by hand to make sure any security dialogs have been OK'd in the context? e.g., sometimes your test context might be admin whereas your normal browsing profile might not be (depends on browser) - e.g., if you have ignored a cert warning in one context but not the other this might cause an issue.

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.

后知后觉 2024-08-25 02:30:27

关于注销/登录情况,我遇到了与鲁本类似的问题。到目前为止我能找到的最好的推理与 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.

花落人断肠 2024-08-25 02:30:27
public void ExampleTest() 
    { 

        IE ie = new IE("http://www.google.com"); 

        ie.TextField(Find.ByName("q")).TypeText("WatiN"); 
        var btnSearch = ie.Button(Find.ByValue("Google Search"));
        btnSearch.ClickNoWait();
        ie.WaitforComplete(); 
        Link link = ie.Link(Find.ByUrl("http://watin.sourceforge.net/")); 

        Assert.That(link.Text == "WatiN Home"); 
    } 
public void ExampleTest() 
    { 

        IE ie = new IE("http://www.google.com"); 

        ie.TextField(Find.ByName("q")).TypeText("WatiN"); 
        var btnSearch = ie.Button(Find.ByValue("Google Search"));
        btnSearch.ClickNoWait();
        ie.WaitforComplete(); 
        Link link = ie.Link(Find.ByUrl("http://watin.sourceforge.net/")); 

        Assert.That(link.Text == "WatiN Home"); 
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文