将 PageFactory 设计与 selenium 2 webdriver 一起使用的最佳方法是什么,但是当页面并不总是加载预期的页面时,如何创建新的意外页面?
public class PassengerPage
{
[FindsBy(How = How.Id, Using "submitId")
IWebElement submitButton { get; set; }
private readonly IWebDriver driver;
public PassengerPage(IWebdriver driver)
{
if(!driver.url.Contains"Passengers.aspx")
{
throw new NotFoundException("Not the passenger page");
}
this.driver = driver;
PageFactory.InitElements(driver, this);
}
public PassengerPage NewPassenger(Passenger p)
{
// fill out some forms
// Example:
// someInputField.sendKeys(p.Name);
submitButton.Click(); // Ok, submit clicked, possibility of passenger page
// not coming back, depending on how many passengers were filled out
// But what if its not...
return new PassengerPage(driver);
}
}
// 如果是 submitButton.Click() 之后返回的页面;是 Confirmation.aspx 页面吗?我不能 100% 确定,这取决于以前的要求,例如签到然后说需要多少乘客,但这并没有存储在此页面中。
我可以使用某种类型的构造函数吗?
所以我可以做如下的事情:
public class PassengerPage
{
[FindsBy(How = How.Id, Using "submitId")
IWebElement submitButton { get; set; }
private readonly IWebDriver driver;
public PassengerPage(IWebdriver driver)
{
if(!driver.url.Contains"Passengers.aspx")
{
throw new NotFoundException("Not the passenger page");
}
this.driver = driver;
PageFactory.InitElements(driver, this);
}
public Page<T> NewPassenger(Passenger p)
{
// fill out some forms
// Example:
// someInputField.sendKeys(p.Name);
submitButton.Click(); // Ok, submit clicked, possibility of passenger page
// not coming back, depending on how many passengers were filled out
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(d => d.url.Contains("Passenger.aspx") || d => d.url.Contains("Confirmation.aspx");
if(driver.Url.Contains("Passenger.aspx")
{
return Page.Create<PassengerPage>(driver);
}
if(driver.Url.Contains("Confirmation.aspx")
{
return Page.Create<ConfirmationPage>(driver);
}
}
}
在另一个班级我可以使用这样的东西吗?我从来没有像这样使用过泛型,我正在考虑使用 Java Class.forName(name); 之类的东西。
// Some kind of Type Constructor
public class Page
{
public static T Create<T>(IWebDriver driver) where T : new()
{
return new T(driver);
}
}
我主要担心的是我无法在单元测试中将我的函数链接在一起,如下所示:
[Test(Description = "Some description about my unit test")
public void TestAddPassengerAndConfirm()
{
new LoginPage(driver).Login("username", "password")
.NavigateToSearch()
.Search( /* input search criteria */ )
.SelectSearchResult(Result.First)
.AddPassenger(new Passenger()
{
FirstName = "first",
LastName = "last",
})
.AddPassenger(new Passenger()
{
// More properties
})
.ConfirmPassengers() // This would be a function on my ConfirmationPage
// That would return the HomePage or something
}
关于我应该如何做到这一点的任何建议?