2012-07-15 97 views
1

這看起來很簡單,但由於某種原因,我沒有得到它的工作。使用另一種測試的方法

我使用硒和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"); 
    } 
} 
} 
+0

可以請你顯示completeRegistrationForm代碼() – HatSoft 2012-07-15 07:03:32

+0

@HatSoft肯定的是,我已經更新了我的帖子的代碼。謝謝! – Goose 2012-07-15 07:07:25

+0

方法中的代碼看起來不錯,你可以複製/粘貼堆棧跟蹤顯示InnerException – HatSoft 2012-07-15 07:14:53

回答

0

您可以附加到nUnit進程以調試故障線路http://frater.wordpress.com/2010/05/04/debugging-nunit-tests-under-visual-studio-2010/

您可以將Visual Studio配置爲在「調試」>「例外」中中斷異常。在「公共語言運行時例外」旁邊,選擇「用戶未處理」。

enter image description here

+0

謝謝,這是相當有用的:) – Goose 2012-07-15 11:58:04

+0

這有助於找到有害的代碼行嗎? – 2012-07-15 11:59:58

+0

它給了我相同的信息,但是當我想要一步一步看看事情進展的時候,這將會很棒。非常感謝。我想我的問題是,測試鏈接不知道任何關於'硒'。現在我需要弄清楚爲什麼......哈哈 – Goose 2012-07-15 12:21:30

相關問題