使用另一个测试中的方法

时间:2012-07-15 06:56:24

标签: c# selenium nunit

这看起来很简单,但出于某种原因,我没有让它发挥作用。

我正在使用Selenium和Nunit。

我有一个名为'Registration_Test'的类,其中有一个名为'completeRegistrationForm()'的方法,如上所述。

我希望这种方法可以在多种不同的测试中使用。

这是我在第二次测试中所拥有的。

Registration_Test reg = new Registration_Test();
reg.completeRegistrationForm();

这很快乐地编译,但是当我在Nunit中运行它时,我得到以下结果:

SeleniumTests.Check_Links.Check_linksTest:
System.NullReferenceException : Object reference not set to an instance of an object.

它表示reg.completeRegistrationForm();线是罪魁祸首。

感谢您提供的任何帮助。

以下是我的两项测试:

注册-test.cs中

namespace SeleniumTests
{
[TestFixture]
public class Registration_Test
{
    private ISelenium selenium;
    private StringBuilder verificationErrors;

    [SetUp]
    public void SetupTest()
    {
        selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://custom-creative-404-dropbox:8080/");
        selenium.Start();
        verificationErrors = new StringBuilder();
    }

    [TearDown]
    public void TeardownTest()
    {
        try
        {
            selenium.Stop();
        }
        catch (Exception)
        {
            // Ignore errors if unable to close the browser
            Console.Write("Unable to Stop Selenium and/or close the browser");
        }
        Assert.AreEqual("", verificationErrors.ToString());
    }

    [Test]
    public void Registration()
    {

        completeRegistrationForm();

        //back to home page.
        selenium.Click("link=Home");
        selenium.WaitForPageToLoad("30000");
    }


    public void completeRegistrationForm()
    {
        selenium.Open("/");
        selenium.Click("link=Register");
        selenium.WaitForPageToLoad("30000");

        string uniqueVal = selenium.GetEval("\"AutomatedTest\" + (new Date().getDate()) + (new Date().getTime())");
        //use moment instead.

        selenium.Type("id=RegistrationForm_TxtName", uniqueVal);

        selenium.Type("id=RegistrationForm_TxtUserName", uniqueVal + "@creative-404.com");
        selenium.Type("id=RegistrationForm_TxtPassword", uniqueVal);
        selenium.Type("id=RegistrationForm_TxtRePassword", uniqueVal);
        selenium.Click("id=RegistrationForm_Button1");
        selenium.WaitForPageToLoad("30000");

        for (int second = 0; ; second++)
        {
            if (second >= 60) Assert.Fail("timeout");
            try
            {
                if (selenium.IsTextPresent("Registration was successful!")) break;
            }
            catch (Exception)
            {
                Console.Write("Unable to find the Registration successful message. Something has gone wrong..");
            }
            Thread.Sleep(1000);
        }
    }

}
}

和Test-Links.cs(这个测试真的只是为了测试它)

namespace SeleniumTests
{
[TestFixture]
public class Check_Links
{
    private ISelenium selenium;
    private StringBuilder verificationErrors;

    [SetUp]
    public void SetupTest()
    {
        selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://custom-creative-404-dropbox:8080/");
        selenium.Start();
        verificationErrors = new StringBuilder();
    }

    [TearDown]
    public void TeardownTest()
    {
        try
        {
            selenium.Stop();
        }
        catch (Exception)
        {
            // Ignore errors if unable to close the browser
        }
        Assert.AreEqual("", verificationErrors.ToString());
    }

    [Test]
    public void Check_linksTest()
    {
        Registration_Test reg = new Registration_Test();
        reg.completeRegistrationForm();

        selenium.Open("/home.aspx");
        selenium.Click("link=Home");
        selenium.WaitForPageToLoad("30000");
        selenium.Click("link=Gallery");
        selenium.WaitForPageToLoad("30000");
    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以附加到nUnit进程以调试失败的行http://frater.wordpress.com/2010/05/04/debugging-nunit-tests-under-visual-studio-2010/

您可以将Visual Studio配置为在Debug> Exceptions中中断异常。在“Common Language Runtime Exceptions”旁边,选择“User-unhandled”。

enter image description here