C# 在另一个测试中使用一种测试方法
这似乎很简单,但由于某种原因,我没有让它工作 我正在用硒和Nunit 我有一个名为“Registration\u Test”的类,其中我有一个名为“completeRegistrationForm”的方法,它的作用如所述 我希望这个方法可以用于调用许多不同的测试 这是我第二次测试的结果C# 在另一个测试中使用一种测试方法,c#,selenium,nunit,C#,Selenium,Nunit,这似乎很简单,但由于某种原因,我没有让它工作 我正在用硒和Nunit 我有一个名为“Registration\u Test”的类,其中我有一个名为“completeRegistrationForm”的方法,它的作用如所述 我希望这个方法可以用于调用许多不同的测试 这是我第二次测试的结果 Registration_Test reg = new Registration_Test(); reg.completeRegistrationForm(); 这可以很好地编译,但当我在Nunit中运行它时,
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.
表明注册登记表齐全;这是罪魁祸首
感谢你能提供的任何帮助
以下是我的两个测试:
注册-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");
}
}
}
您可以附加到nUnit进程以调试失败的行 您可以将visual studio配置为在调试>异常中中断异常。在公共语言运行时异常旁边,选择用户未处理
你能告诉我你的密码吗completeRegistrationForm@HatSoft当然,我已经用代码更新了我的帖子。谢谢方法中的代码看起来不错,您可以复制/粘贴堆栈跟踪吗InnerException@HatSoft我不确定我是否知道如何在Nunit中显示内部异常?我正在那里运行测试,而不是VisualStudio。我只有这个。System.NullReferenceException:对象引用未设置为对象的实例。在SeleniumTests.Registration\u Test.completeRegistrationForm中的F:\Programs\Dropbox\Dropbox\Custom-Creative-404\Selenium\Creative-404-Tests\Creative-404\Registration Tests.cs:SeleniumTests.Check\u Links.Check\u Links第54行在F:\Programs\Dropbox\Dropbox\Dropbox\Custom-Creative-404\Selenium\Creative-404-Tests\Creative-404\Creative-404\Tests\Creative-404\Tests中列出。cs:在RegistrationTest.cs的第54行?当您创建一个Registration_测试对象时,它是从哪里获得selenium的?谢谢,这非常有帮助:它给了我相同的信息,但是当我想逐步了解情况时,这将非常有用。非常感谢。我想我的问题是测试链接对“selenium”一无所知。现在我需要弄清楚为什么。。。哈哈