【问题标题】:WatiN 2.0 Beta:Screensaver still not workingWatiN 2.0 Beta:屏幕保护程序仍然无法正常工作
【发布时间】:2011-10-17 21:03:55
【问题描述】:

我编写了一个演示代码来测试 WatiN 的屏幕保护功能。

但是当我故意编写以下代码失败并保存屏幕截图时,它只是在 Assert.True 之后停止执行,即测试失败

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.IsTrue(ie.ContainsText("NUnit"), "Expected to find NUnit on the page.");

这一步是故意的,但没有实现在指定位置保存屏幕截图。

感谢您的帮助:)

【问题讨论】:

    标签: c# automated-tests watin


    【解决方案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()
    

    如果您构建解决方案并打开测试视图窗口(测试->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.");
        }
    }
    

    【讨论】:

    • 另外,在你的例子中你知道你制作了三个独立的 IE 实例吗?一个在课堂上,一个在 DemoCaptureOnFailure 中,一个在 DisposeBrowser 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多