使用selenium 2 webdriver的PageFactory设计的最佳方法是什么,但是当页面不总是加载预期的页面时,我如何创建新的意外页面?
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()之后返回的页面;就是坚信礼。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
}
有什么建议我应该怎么做吗?
您将不得不将该方法链拆分为多个语句。在c#中,我会做如下的事情。它不那么漂亮(如果你相信方法链),也不那么容易,但如果编程很容易,我们就不会得到报酬了。
public void TestAddPassengerAndConfirm()
{
Page uncertainPage = new LoginPage(driver).Login("username", "password")
.NavigateToSearch()
.Search( /* input search criteria */ )
.SelectSearchResult(Result.First);
if (uncertainPage is PassengerPage)
{
PassengerPage passengerPage = (PassengerPage) uncertainPage;
uncertainPage = passengerpage.AddPassenger(new Passenger()
{
FirstName = "first",
LastName = "last",
})
.AddPassenger(new Passenger()
{
// More properties
});
}
if (uncertainPage is ConfirmationPage)
{
ConfirmationPage confirmationPage = (ConfirmationPage) uncertainPage;
confirmationPage.ConfirmPassengers();
}
}