我正在使用 Selenium 2 Web Driver 和 C#.Net 创建测试。在阅读了大量 Selenium 文档后,我仍然不确定如何使用 PageObject 设计模式进行测试。
许多 selenium 示例仅在 Java 中显示,.Net 的 API 绑定并不总是像人们想象的那样相似,这是由于某些语言设置的限制和标准。
在 .Net Selenium Web 驱动程序中将 PageObject 设计模式与 PageFactory 结合使用的最佳方法是什么?
最终,我希望我的 PageObjects 处理更多功能,而不是使用 PageObject IWebElements 进行 NUnit 测试。
下面是一个示例,说明我目前将如何创建我的测试。
public class LoginPage
{
private IWebDriver webDriver;
[FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_txtPassword")]
public IWebElement Password { get; set; }
[FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_cmdSubmit")]
public IWebElement SubmitButton { get; set; }
[FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_txtUserName")]
public IWebElement UserName { get; set; }
public LoginPage() { }
public LoginPage(IWebDriver webDriver)
{
this.webDriver = webDriver;
if(!webDriver.Url.Contains("Login.aspx"))
{
throw new StaleElementReferenceException("This is not the login page");
}
PageFactory.InitElements(webDriver, this);
}
public HomePage signIn(string username, string password)
{
UserName.SendKeys(username);
Password.SendKeys(password);
SubmitButton.Submit();
// Even if i create a NUnit test for this
// Issue with page loading still occures when I try and return new object
HomePage homePage = new HomePage(webDriver);
PageFactory.InitElements(webDriver, homePage);
return homePage;
}
}
目前,这就是我目前对 NUnit 所做的事情:
[TestFixture]
public class LoginPageTest : TestBase
{
private IWebDriver driver;
private LoginPage loginPage;
private HomePage homePage;
[SetUp]
[Description("Sets up the test fixture page objects and navigates to the login page.")]
public void SetUp()
{
driver = StartDriver();
Log.Info("Driver started");
driver.Navigate().GoToUrl("http://" + Environment + ");
loginPage = new LoginPage();
PageFactory.InitElements(driver, loginPage);
//driver.Navigate().Refresh();
}
[Test]
[Description("Enters invalid credentials and asserts that a correct error message is displayed.")]
public void SubmitFormInvalidCredentials()
{
loginPage.UserName.SendKeys("invalid");
loginPage.Password.SendKeys("invalid");
loginPage.SubmitButton.Click();
IWebElement invalidCredentials = driver.FindElement(By.Id("ctl00_ctl00_ctl00_insideForm_insideForm_ctl02_title"));
Assert.AreEqual("Invalid user name or password", invalidCredentials.Text);
}
[Test]
[Description("Enters valid credentials and asserts that the user is taken to the home page.")]
public void SubmitFormValidCredentials()
{
loginPage.UserName.SendKeys("valid");
loginPage.Password.SendKeys("valid");
loginPage.SubmitButton.Click();
homePage = new HomePage();
PageFactory.InitElements(driver, homePage);
Assert.AreEqual("pattest", homePage.Username.Text);
}
}
我为 selenium webdriver 设计模式找到的大多数文章和博客文章都与我发现的以前的文章相矛盾。
那么,正确的方法是什么?
最重要的是,我什至尝试了 PageObject 设计模式。
[Test]
[Description("Login using PageObject Design Pattern")]
public void Login()
{
loginPage = new LoginPage(driver);
HomePage signIn = loginPage.SignIn("pattest", "pattest");
}
在我的登录页面内
public LoginPage(IWebDriver driver)
{
this.driver = driver;
if (!driver.Url.Contains("Login.aspx"))
{
throw new ElementNotFoundException("This is not the login page");
}
PageFactory.InitElements(driver, this);
}
public HomePage SignIn(string username, string password)
{
UserName.SendKeys(username);
Password.SendKeys(password);
SubmitButton.Click();
return new HomePage(driver);
}
而且,当然要展示我的 HomePage 应该如何使用它的构造函数启动:
public HomePage(IWebDriver d)
{
webDriver = d;
// I need to use this as its not freaking waiting for my Page to load when I pass the webdriver in the consturctor.
var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(60));
try
{
wait.Until(driver => driver.FindElement(By.Id("ctl00_ctl00_ctl00_insideForm_insideForm_loginStatus")));
}
catch(Exception e)
{
throw new ElementNotFoundException("This is not the home page.");
}
PageFactory.InitElements(webDriver, this);
}
如何在测试中有效地使用 WebDriver PageObject 设计模式。我想不通。