2012-07-15 100 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

相关问题