我在MS Visual c# 2010 Express中创建了两个项目。第一个项目有一个类SugarcrmLogin,它的方法是TheSugarCrmLoginTest()。
第二个项目有一个类Sugarcrm,其方法为Main。该项目有对第一个项目的项目dll的引用。
在Main方法中,我已经实例化了类SugarcrmLogin并调用了SugarCrmLoginTest()方法。
下面是第二个项目中Main方法的代码,我在其中实例化了类并调用了方法:public static void Main()
{
SugarcrmLogin Login;
Login = new SugarcrmLogin();
Login.TheSugarcrmLoginTest();
}
两个项目都在MS Visual c#中成功构建,但是当我尝试使用Nunit运行第二个项目的项目dll时,我得到错误"对象引用未设置为对象的实例",并引用了第一个项目中的TheSugarCrmLoginTest()方法。
第一个项目
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
[TestFixture]
public class SugarcrmLogin
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
//driver = new FirefoxDriver();
driver = new InternetExplorerDriver();
baseURL = "http://127.0.0.1/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheSugarcrmLoginTest()
{
driver.Navigate().GoToUrl(baseURL + "/sugarcrm/index.php?module=Users&action=Login");
driver.FindElement(By.Id("user_name")).Clear();
driver.FindElement(By.Id("user_name")).SendKeys("admin");
driver.FindElement(By.Id("user_password")).Clear();
driver.FindElement(By.Id("user_password")).SendKeys("admin");
driver.FindElement(By.Id("login_button")).Click();
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
}
}
第二个项目:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
[TestFixture]
public class Sugarcrm
{
public IWebDriver driver;
private StringBuilder verificationErrors;
public string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
//driver = new FirefoxDriver();
driver = new InternetExplorerDriver();
baseURL = "http://127.0.0.1/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public static void Main()
{
SugarcrmLogin Login;
Login = new SugarcrmLogin();
Login.TheSugarcrmLoginTest();
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
}
}
显示代码的编辑:driver
是null
。构造函数不给它赋值,也不像那样调用SetupTest()
方法。
坦率地说,你的Sugarcrm
单元测试不应该像那样实例化和调用另一个(SugarcrmLogin
)单元测试。这看起来像是"不正确地使用单元测试框架"。