两种测试方法在 Watin、Gallio 和 MbUnit 中不能同时工作

发布于 2024-12-11 01:23:35 字数 2734 浏览 0 评论 0原文

当我编写这段代码时,它工作正常

using System;
using WatiN.Core;
using MbUnit.Framework;
using Gallio.Framework;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
using System.Drawing;
using Gallio.Model;

namespace DialogHandlerTestWithWatin
{
[TestFixture]
class Program
{
    //This is WatiN - create IE instance
    public static IE ie = new IE();
    [SetUp]
    public void DoTestSetup()
    {
        IE.Settings.WaitForCompleteTimeOut = 60;
    }
    [TearDown]
    public static void TestNavigateToMedappz()
    {
        if (ie != null)
        {
            if (TestContext.CurrentContext.Outcome == TestOutcome.Failed)
            {
                Assert.Fail("Unable to navigate to the medappz application");
                ie.Close();
                ie.Dispose();
                ie = null;
            }
        }
    }


    //This funcn navigate us to the Medappz Application
    [Test]
    public static void NavigateToMedappz()
    {
        using (TestLog.BeginSection("Go to Medappz"))
        {
            Assert.IsNotNull(ie);
            ie.GoTo("http://192.168.10.82/Sage/");
            ie.ShowWindow(NativeMethods.WindowShowStyle.Maximize);
        }
        //This is NUnit - check that ie instance is not null
        Assert.IsNotNull(ie, "Error creating IE instance.");
        Assert.AreEqual("Login Page", ie.Title);

    }

但是当我在这段代码中添加另一个测试方法时,

    [TearDown]
    public static void TestLoginToMedappz()
    {
        if (ie != null)
        {
            if (TestContext.CurrentContext.Outcome == TestOutcome.Failed)
            {
                Assert.Fail("Unable to Login");
            }
        }
    }
    [Test]
    public static void LoginToMedappz()
    {
        using (TestLog.BeginSection("Login to Medappz"))
        {
            TextField UserName = ie.TextField(Find.ByName("txtUserName"));
            UserName.TypeText("<username>");
            TextField Password = ie.TextField(Find.ByName("txtPassword"));
            Assert.IsTrue(UserName.Exists, "UserName Textbox does not exist");
            Assert.IsTrue(Password.Exists, "Password Textbox does not exist");
            Password.TypeText("<password>");
            Button btnLogin = ie.Button(Find.ByName("btnLogin"));
            Assert.IsTrue(btnLogin.Exists, "btnLogin button does not exist");
            btnLogin.Blur();
            btnLogin.Click();
        }
    }

测试开始失败,只打开一个空白的 IE 窗口,几秒钟后,第一个测试方法被执行,但第二个方法没有执行没有被处决。加里奥测试报告说是

鲁特 110 结果:2 次运行,1 次通过,1 次失败,0 次不确定,0 次跳过 持续时间:44.042秒 断言:3 WatiN.Core.Exceptions.ElementNotFoundException:找不到 INPUT(文本密码文本区域隐藏)或 TEXTAREA 元素标记匹配条件:属性“名称”,值为“txtUserName”

When I am writing this piece of code it works fine

using System;
using WatiN.Core;
using MbUnit.Framework;
using Gallio.Framework;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
using System.Drawing;
using Gallio.Model;

namespace DialogHandlerTestWithWatin
{
[TestFixture]
class Program
{
    //This is WatiN - create IE instance
    public static IE ie = new IE();
    [SetUp]
    public void DoTestSetup()
    {
        IE.Settings.WaitForCompleteTimeOut = 60;
    }
    [TearDown]
    public static void TestNavigateToMedappz()
    {
        if (ie != null)
        {
            if (TestContext.CurrentContext.Outcome == TestOutcome.Failed)
            {
                Assert.Fail("Unable to navigate to the medappz application");
                ie.Close();
                ie.Dispose();
                ie = null;
            }
        }
    }


    //This funcn navigate us to the Medappz Application
    [Test]
    public static void NavigateToMedappz()
    {
        using (TestLog.BeginSection("Go to Medappz"))
        {
            Assert.IsNotNull(ie);
            ie.GoTo("http://192.168.10.82/Sage/");
            ie.ShowWindow(NativeMethods.WindowShowStyle.Maximize);
        }
        //This is NUnit - check that ie instance is not null
        Assert.IsNotNull(ie, "Error creating IE instance.");
        Assert.AreEqual("Login Page", ie.Title);

    }

But when I add another test method in this code as

    [TearDown]
    public static void TestLoginToMedappz()
    {
        if (ie != null)
        {
            if (TestContext.CurrentContext.Outcome == TestOutcome.Failed)
            {
                Assert.Fail("Unable to Login");
            }
        }
    }
    [Test]
    public static void LoginToMedappz()
    {
        using (TestLog.BeginSection("Login to Medappz"))
        {
            TextField UserName = ie.TextField(Find.ByName("txtUserName"));
            UserName.TypeText("<username>");
            TextField Password = ie.TextField(Find.ByName("txtPassword"));
            Assert.IsTrue(UserName.Exists, "UserName Textbox does not exist");
            Assert.IsTrue(Password.Exists, "Password Textbox does not exist");
            Password.TypeText("<password>");
            Button btnLogin = ie.Button(Find.ByName("btnLogin"));
            Assert.IsTrue(btnLogin.Exists, "btnLogin button does not exist");
            btnLogin.Blur();
            btnLogin.Click();
        }
    }

Then the test started failing, only a blank IE window opens up and after few seconds the first test method is executed but the second method doesn't gets executed.The Gallio test report says

Root
110
Results: 2 run, 1 passed, 1 failed, 0 inconclusive, 0 skipped
Duration: 44.042s
Assertions: 3
WatiN.Core.Exceptions.ElementNotFoundException: Could not find INPUT (text password textarea hidden) or TEXTAREA element tag matching criteria: Attribute 'name' with value 'txtUserName'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

捎一片雪花 2024-12-18 01:23:35

两次考试是在同一个班级吗?我从来没有在同一个班级见过两次拆解。另外,如果 TestNavigateToMedappz 在 LoginToMedappz 之前调用,那么您的 ie 变量将已经被释放。您只需进行一次拆解,然后在 LoginToMedappz 开始时调用 NavigateToMedappz 怎么样?

Are the two tests in the same class? I've never seen two teardowns in the same class. Also, if TestNavigateToMedappz is called before LoginToMedappz then your ie variable will have already been disposed. How about you have just one teardown, then at the start of LoginToMedappz you call NavigateToMedappz?

雨落□心尘 2024-12-18 01:23:35

这就是我认为你想要做的......

你这里有两个测试。 NavigateToMedappz 测试尝试导航到您的应用程序。 LoginToMedappz 测试首先尝试导航到您的应用程序,然后尝试登录。

[TestFixture]
public class MedappzTests
{
    private IE ie;

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

    [TearDown]
    public void DoTestTeardown()
    {
        if (ie != null)
        {
            ie.Close();
            ie.Dispose();
            ie = null;
        }
    }

    [Test]
    public void NavigateToMedappz()
    {
        //...
    }

    [Test]
    public void LoginToMedappz()
    {
        NavigateToMedappz();
        //...
        //Here I'm assuming that if you've successfully logged in then the login button should no longer exist
        Assert.IsFalse(btnLogin.Exists);
    }
}

哈!

Here's what I think you're trying to do...

You have two tests here. The NavigateToMedappz test tries to navigate to your app. The LoginToMedappz test first tries to navigate to your app, and THEN it tries to login.

[TestFixture]
public class MedappzTests
{
    private IE ie;

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

    [TearDown]
    public void DoTestTeardown()
    {
        if (ie != null)
        {
            ie.Close();
            ie.Dispose();
            ie = null;
        }
    }

    [Test]
    public void NavigateToMedappz()
    {
        //...
    }

    [Test]
    public void LoginToMedappz()
    {
        NavigateToMedappz();
        //...
        //Here I'm assuming that if you've successfully logged in then the login button should no longer exist
        Assert.IsFalse(btnLogin.Exists);
    }
}

HTH!

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