我已经在很多论坛上检查了很多类似的问题。但与我的不完全相似。另外,我不想将登录方法移动到base。
代码:
LoginTest.cs
using test_TestAutomation.PageObjects;
using NUnit.Framework;
namespace test_TestAutomation.TestClasses
{
class LoginTet
{
// public IWebDriver driver;
[Test]
public void LoginSuccessCheck()
{
LoginPage login = new LoginPage();
login.LoginSuccess();
}
}
}
Base.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
namespace test_TestAutomation.TestClasses
{
class Base
{
public static IWebDriver driver;
[SetUp]
public void SetUp()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[Test]
public void OpenURL()
{
driver.Navigate().GoToUrl("http://www.example.com");
}
[TearDown]
public void TearDown()
{
driver.Close();
}
}
}
LoginPage.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using test_TestAutomation.TestClasses;
namespace test_TestAutomation.PageObjects
{
public class LoginPage
{
[FindsBy(How = How.Id, Using = "Email")]
public IWebElement LoginEmailTxtLocator;
[FindsBy(How = How.Id, Using = "Password")]
public IWebElement LoginPasswordTxtLocator;
[FindsBy(How = How.CssSelector, Using = "#loginForm > div > div > form > div:nth-child(5) > div > input")]
private IWebElement BtnLogin;
public LoginPage()
{
PageFactory.InitElements(Base.driver, this);
}
public LoginPage LoginSuccess()
{
LoginEmailTxtLocator.SendKeys("emailtest@gmail.com");
LoginPasswordTxtLocator.SendKeys("xxxxxxx");
BtnLogin.Click();
return this;
}
}
}
例外:
结果讯息: System.ArgumentException:定位器对象的SearchContext不能为null
Stackstace:
at OpenQA.Selenium.Support.PageObjects.PageFactory.InitElements(Object page, IElementLocator locator, IPageObjectMemberDecorator decorator)
at OpenQA.Selenium.Support.PageObjects.PageFactory.InitElements(ISearchContext driver, Object page)
答案 0 :(得分:1)
我建议您将测试OpenURL
从Base
提取到LoginTest
,并仅在Base中进行设置和拆解。然后让LoginTest
继承Base
。
<强> Base.cs 强>
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
namespace test_TestAutomation.TestClasses
{
public class Base
{
public static IWebDriver driver;
[SetUp]
public void SetUp()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[TearDown]
public void TearDown()
{
driver.Close();
}
}
}
<强> LoginTest.cs 强>
using test_TestAutomation.PageObjects;
using NUnit.Framework;
namespace test_TestAutomation.TestClasses
{
class LoginTest : Base
{
[Test]
public void OpenURL()
{
driver.Navigate().GoToUrl("http://www.example.com");
}
[Test]
public void LoginSuccessCheck()
{
driver.Navigate().GoToUrl("http://www.example.com");
LoginPage login = new LoginPage();
login.LoginSuccess();
}
}
}
<强> LoginPage.cs 强>
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using test_TestAutomation.TestClasses;
namespace test_TestAutomation.PageObjects
{
public class LoginPage
{
[FindsBy(How = How.Id, Using = "Email")]
public IWebElement LoginEmailTxtLocator;
[FindsBy(How = How.Id, Using = "Password")]
public IWebElement LoginPasswordTxtLocator;
[FindsBy(How = How.CssSelector, Using = "#loginForm > div > div > form > div:nth-child(5) > div > input")]
private IWebElement BtnLogin;
public LoginPage()
{
PageFactory.InitElements(Base.driver, this);
}
public LoginPage LoginSuccess()
{
LoginEmailTxtLocator.SendKeys("emailtest@gmail.com");
LoginPasswordTxtLocator.SendKeys("xxxxxxx");
BtnLogin.Click();
return this;
}
}
}