使用泛型的 Selenium Webdriver 和类型构造函数 - 如何操作
将 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
}
关于我应该如何做到这一点有什么建议吗?
What's the best way to use the PageFactory design with selenium 2 webdriver, but when a page doesn't always load an expected page how can I create the new unexpected page?
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);
}
}
// What if the page that is returned after the submitButton.Click(); is the Confirmation.aspx page? I can't be 100% sure, it depends on previous requirements such as the signing in and then saying how many passengers are required but this is not stored in this page.
Can i use some kind of Type constructor?
So could I do something like the following:
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);
}
}
}
In another class could I use something like this? I've never used Generics like so and I was
considering using something like 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);
}
}
The main concern I have is I won't be able to chain my functions together in my Unit Tests like the following:
[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
}
Any recommendations on how I should I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将该方法链拆分为多个语句。在 C# 中,我会执行如下操作。它没有那么漂亮(如果你相信方法链),也没有那么容易,但如果编程很容易,我们就不会因为这样做而获得报酬。
You're going to have to split that method chain into multiple statements. In C#, I'd do something like the following. It isn't as pretty (if you believe in method chaining), and it isn't as easy, but if programming were easy, we wouldn't get paid to do it.