这看起来很简单,但由于某种原因,我没有让它工作。
我将 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");
}
}
}