使用WatiN和NUnit进行单元测试

Phi*_*ler 5 nunit watin unit-testing

看来,关于WatiN的SO问题的共同主题与实际让事情有效有关,我也不例外.

我已经下载了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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这通过以下堆栈跟踪在最终超时后提示IE(8):

at WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.ThrowTimeOutException(Exception lastException,String message)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)在WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitWhileFrameDocumentNotAvailable(IWebBrowser2的帧)在WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(的IHTMLDocument2 maindocument)在华廷.在WatiN.Core.WaitForCompleteBase的WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForCompleteOrTimeout()的Core.Native.InternetExplorer.IEWaitForComplete.WaitForFramesToComplete(IHTMLDocument2 maindocument).在WatiN.Core.Bore.Browser.GoTo(Uri url)的WatiN.Core.DomContainer.WaitForComplete()的WatiN.Core.IE.WaitForComplete(Int32 waitForCompleteTimeOut)的WatiN.Core.DomContainer.WaitForComplete(IWait waitForComplete)上的DoWait() WatiN.Core.IE.FinishInitialization(Uri uri)at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri,IDialogHandler logonDialogHandler,Boolean createInNewProcess)at WooN.Core.IE..ctor(String url)at Foo.Browser.Tests.BrowserTests C:\ Development\Foo\Foo.Browser.Tests\BrowserTests.cs中的.ExampleTest():第19行在C:\ Development\Foo\Foo.Browser.Tests \中的Foo.Browser.Tests.BrowserTests.ExampleTest()中的WatiN.Core.IE..ctor(String url)创建NewNewIEAndGoToUri(Uri uri,IDialogHandler logonDialogHandler,Boolean createInNewProcess) BrowserTests.cs:第19行在C:\ Development\Foo\Foo.Browser.Tests \中的Foo.Browser.Tests.BrowserTests.ExampleTest()中的WatiN.Core.IE..ctor(String url)创建NewNewIEAndGoToUri(Uri uri,IDialogHandler logonDialogHandler,Boolean createInNewProcess) BrowserTests.cs:第19行

我在我的bin\debug文件夹(Foo.Browser.Tests.dll.config)中有必要的配置文件,如WatiN文档页面上指定的那样,那么它还有什么可能呢?有没有人对可能导致问题的原因有任何建议?

Jer*_*nen 5

菲尔,

看看抛出的TimeoutExcepetion的InnerException.这很可能包含更多信息来解决您的问题.

HTH Jeroen van Menen领导开发WatiN


小智 5

看起来Phil可能已经回答了你的问题,但我发现你的例子也会抛出异常.为了解决上述问题,我将链接查找更改为按属性(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"); 
}
Run Code Online (Sandbox Code Playgroud)

请注意,我只测试了IE,因为此框没有安装FireFox.