我有以下的方法在一個文件中PhantomJS及硒 - 測試調用另一個文件中的方法(NullReferenceException異常)
public class Login
{
static IWebDriver driver;
public static void LogIn(string Env, string User)
{
driver.Navigate().GoToUrl(Env);
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(3000));
driver.FindElement(By.XPath("//*[@ng-model='email']")).SendKeys(User);
driver.FindElement(By.XPath("//*[@ng-model='password']")).SendKeys("1234");
driver.FindElement(By.XPath("//*[@id=\"ui-view\"]/div/div/div[1]/form/div[2]/button")).Click();
System.Threading.Thread.Sleep(2000);
NUnit.Framework.Assert.IsTrue(driver.FindElement(By.Name("some element")).Displayed,
"Login failed, home page did not display");
}
}
}
並且測試我想打電話從上面的方法,在另一:
[TestFixture]
public class SmokeTest
{
private IWebDriver driver;
[SetUp]
public void SetUp()
{
driver = new PhantomJSDriver();
}
[Test]
public void Test1()
{
**Login.LogIn("myEnv", "user");**
}
}
我沒有問題彙編,但一旦我跑的Test1我得到以下信息:
Automation.SmokeTest.SearchForProduct: System.NullReferenceExc eption:未將對象引用設置爲對象的實例。
Nunit指向Login.LogIn("myEnv", "user");
作爲源或錯誤。
任何想法?
Ta。
對不起,adrianbanks,這不是原因。如果其中一個Xpath路徑無效,則會在'未找到元素'的行上生成一個Selenium異常。 – entrup
靜態IWebDriver驅動程序; << NULL在這裏 –