这可能是一个愚蠢的问题,但我还没有找到可以将我引向解决方案的答案
说我有一个测试方法来验证登录门户的功能。它在TestClassA中。我想在TestClassB的TestInitialize方法中运行该方法,这样我就可以可靠地让硒在空白板上启动,以测试通过该登录门户的功能。
这是有问题的测试代码
using Login_Elements;
using Dashboard;
namespace Test_Dashboard_Elements
{
[TestClass]
public class DashboardTests
{
IWebDriver _driver;
DashboardElements dash;
[TestInitialize]
public void Test_Setup()
{
dash = new DashboardElements(_driver);
LoginPage login = new LoginPage(_driver);
_driver = new FirefoxDriver();
_driver.Navigate().GoToUrl("exampleurl/login");
login.Login();
}
}
它调用DashboardElements的一个实例并传递硒Webdriver,然后调用LoginPage的实例并传递硒WebDriver(我认为是问题),并从LoginPage调用login方法。
IWebDriver _driver;
//Username field
[FindsBy(How = How.Id, Using = "username")]
private IWebElement userName;
//Password field
[FindsBy(How = How.Id, Using = "password")]
private IWebElement password;
//Submit Button
[FindsBy(How = How.ClassName, Using = "btn")]
private IWebElement submit_button;
//Constructor
public LoginPage(IWebDriver driver)
{
this._driver = driver;
PageFactory.InitElements(driver, this);
}
//Sends passed string to username field
public void sendUserName(string strUsername)
{
userName.SendKeys(strUsername);
}
//Sends passed string to password field
public void sendPassword(string strPassword)
{
password.SendKeys(strPassword);
}
//Clicks submit button
public void submit()
{
submit_button.Click();
}
public void Login()
{
sendUserName("username");
sendPassword("password!");
submit();
}
这返回
>Message: Initialization method Test_Dashboard_Elements.DashboardTests.Test_Setup threw exception. System.ArgumentNullException: System.ArgumentNullException: searchContext may not be null Parameter name: searchContext
我觉得这与两次传递_driver有关,但我不确定该怎么做
堆栈跟踪:
>Test Name: Test_Link_Reports
Test FullName: Test_Dashboard_Elements.DashboardTests.Test_Link_Reports
Test Source: c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Tests\Dashboard Tests.cs : line 29
Test Outcome: Failed
Test Duration: 0:00:06.0255821
结果消息:
>Initialization method Test_Dashboard_Elements.DashboardTests.Test_Setup threw exception. System.ArgumentNullException: System.ArgumentNullException: searchContext may not be null
Parameter name: searchContext.
Result StackTrace:
at OpenQA.Selenium.Support.PageObjects.DefaultElementLocatorFactory.LocateElement(ISearchContext searchContext, IEnumerable`1 bys)
at OpenQA.Selenium.Support.PageObjects.WebElementProxy.get_WrappedElement()
at OpenQA.Selenium.Support.PageObjects.WebElementProxy.SendKeys(String text)
at Login_Elements.LoginPage.sendUserName(String strUsername) in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Elements\Login Elements.cs:line 39
at Login_Elements.LoginPage.Login() in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Elements\Login Elements.cs:line 56
at Test_Dashboard_Elements.DashboardTests.Test_Setup() in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Tests\Dashboard Tests.cs:line 24
DefaultElementLocatorFactory.LocateElement(ISearchContext searchContext, IEnumerable 1 bys)
WebElementProxy.get_WrappedElement()
WebElementProxy.SendKeys(String text)
LoginPage.sendUserName(String strUsername)
LoginPage.Login()
DashboardTests.Test_Setup()
似乎问题在于他的driver
实例化。您没有将实例化的驱动程序传递给DashboardElements()
。要解决此问题:
driver
。通过实例化driver
到PageObject
。
using Login_Elements;
using Dashboard;
namespace Test_Dashboard_Elements
{
[TestClass]
public class DashboardTests
{
IWebDriver _driver;
DashboardElements dash;
[TestInitialize]
public void Test_Setup()
{
_driver = new FirefoxDriver();
dash = new DashboardElements(_driver);
LoginPage login = new LoginPage(_driver);
_driver.Navigate().GoToUrl("exampleurl/login");
login.Login();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句