這看起來很簡單,但由於某種原因,我沒有得到它的工作。使用另一種測試的方法
我使用硒和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);
}
}
}
}
和測試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");
}
}
}
可以請你顯示completeRegistrationForm代碼() – HatSoft 2012-07-15 07:03:32
@HatSoft肯定的是,我已經更新了我的帖子的代碼。謝謝! – Goose 2012-07-15 07:07:25
方法中的代碼看起來不錯,你可以複製/粘貼堆棧跟蹤顯示InnerException – HatSoft 2012-07-15 07:14:53