2011-10-17 31 views
0

我写了一个演示代码来测试WatiN的屏保功能。WatiN 2.0 Beta:屏保仍然无效

但是,当我写了下面的一段代码故意失败,并保存截图,它只是停止在测试失败

using System; 
using WatiN.Core; 
using Gallio.Framework; 
using MbUnit.Framework; 
using Gallio.Model; 


namespace Screenshotwhentestfails 
{ 
    [TestFixture] 
    class Program 
    { 

     public IE ie = new IE(); 
     [STAThread] 
     [Test] 
     static void Main(string[] args) 
     { 
      DemoCaptureOnFailure(); 
      DisposeBrowser(); 
     } 
     [Test] 
     [TearDown] 
     public static void DemoCaptureOnFailure() 
     { 
      IE ie = new IE(); 
      using (TestLog.BeginSection("Go to Google, enter MbUnit as a search term and click I'm Feeling Lucky")) 
      { 
       ie.GoTo("http://www.google.com"); 

       ie.TextField(Find.ByName("q")).TypeText("MbUnit"); 
       ie.Button(Find.ByName("btnI")).Click(); 
      } 

      // Of course this is ridiculous, we'll be on the MbUnit homepage... 
      Assert.IsTrue(ie.ContainsText("NUnit"), "Expected to find NUnit on the page."); 
     } 
     [TearDown] 
     public static void DisposeBrowser() 
     { 
      IE ie = new IE(); 
      if (TestContext.CurrentContext.Outcome == TestOutcome.Failed) 
      { 
       ie.CaptureWebPageToFile("C:\\Documents and Settings\\All Users\\Favorites.png"); 
      } 

     } 
     } 
    } 

它是在

抛出异常Assert.True即在执行
   Assert.IsTrue(ie.ContainsText("NUnit"), "Expected to find NUnit on the page."); 

这一步是故意的,但屏幕截图在指定位置的保存不成立。

感谢您的任何帮助:)

回答

1

我以为你在哪里使用NUnit?无论如何,这是你需要做的。

你不是很正确地设置你的测试。

在您的应用程序中,转至File-> New-> Project ...并添加一个“MbUnit V3测试项目”(C#版本)。在解决方案资源管理器中添加对WatiN DLL的引用。

首先添加一个新类为您的测试与[的TestFixture]属性: -

[TestFixture] 
public class ScreenshotTest 

添加尽可能多的测试方法,只要你喜欢: -

[Test] 
public void DoScreenshotTest() 

如果你有一些初始化/最终确定要运行的代码,以便在此类中的所有测试中添加方法: -

[SetUp] 
public void DoTestSetup() 

[TearDown] 
public void DoTestTeardown() 

如果您建立d你的解决方案并打开测试视图窗口(测试 - > Windows->测试视图),你应该看到你的新测试方法。然后,您可以右键单击并“运行选择”或“调试选择”

以下是完整版本的代码HTH!

[TestFixture] 
public class ScreenshotTest 
{ 
    private IE ie; 

    [SetUp] 
    public void DoTestSetup() 
    { 
     ie = new IE(); 
    } 

    [TearDown] 
    public void DoTestTeardown() 
    { 
     if (ie != null) 
     { 
      if (TestContext.CurrentContext.Outcome == TestOutcome.Failed) 
       ie.CaptureWebPageToFile(@"C:\Documents and Settings\All Users\Favorites.png"); 

      ie.Close(); 
      ie.Dispose(); 
      ie = null; 
     } 
    } 

    [Test] 
    public void DoScreenshotTest() 
    { 
     Assert.IsNotNull(ie); 

     using (TestLog.BeginSection("Go to Google, enter MbUnit as a search term and click I'm Feeling Lucky")) 
     { 
      ie.GoTo("http://www.google.com"); 
      ie.TextField(Find.ByName("q")).TypeText("MbUnit"); 
      ie.Button(Find.ByName("btnI")).Click(); 
     } 

     Assert.IsTrue(ie.ContainsText("NUnit"), "Expected to find NUnit on the page."); 
    } 
} 
+0

此外,在你的例子中,你知道你做三个独立的IE实例吗?一个在DemoCaptureOnFailure中,一个在DisposeBrowser中。 –

相关问题