简体   繁体   中英

Using a method from one test in another

This seems simple, but for some reason I've not got it to work.

I'm using Selenium with Nunit.

I have one class called 'Registration_Test' in which I have a method named 'completeRegistrationForm()' which does as described.

I would like this method to be available for me to call in a number of different tests.

This is what I have in my second test.

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

This compiles happily but when I run it in Nunit I get the following:

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

and it indicates that reg.completeRegistrationForm(); line is the culprit.

Appreciate any help you can offer.

Here are my two tests:

Registration-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);
        }
    }

}
}

and Test-Links.cs (this test was made really just to test this out)

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");
    }
}
}

You can attach to the nUnit process to debug the failing line http://frater.wordpress.com/2010/05/04/debugging-nunit-tests-under-visual-studio-2010/ .

You can configure visual studio to break on exceptions in Debug>Exceptions. Next to "Common Language Runtime Exceptions" select "User-unhandled".

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM