请帮助: 我正在使用cucumber-jvm + WebDriver + jUnit + maven页面对象模式进行自动化测试。与黄瓜jvm和页面对象的泛型不起作用
我想有一个方法可以返回多种类型的对象。 (不同的预期页面)。 在我过去,我使用泛型来实现它与清晰的Java + Webdriver。 在This Post这里有一个很好的解释。
但现在我想用黄瓜补充它。
我的项目结构看起来接下来方式:
驱动程序的基类:
public class DriverBase {
public static WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
@After
public void tearDown() throws Exception {
driver.quit();
}
}
导航类的网页对象之间的互动:
public class Navigator {
DriverBase base;
WebDriver driver;
public NavigationActions(DriverBase base) {
this.base = base;
this.driver = base.driver;
}
public FirstPage openFirstPage(){
driver.get("someUrl");
return new FirstPage(base);
}
}
页面对象类:
public class FirstPage {
WebDriver driver;
DriverBase base;
//...
//Elements locators...
//Some methods...
//...
public FirstPage(DriverBase base) {
this.base = base;
this.driver = base.driver;
PageFactory.initElements(driver, this);
}
public <T> T openSecondOrThirdPage(String secondPgUrl, Class<T> expectedPage) {
driver.get("secondPgUrl");
return PageFactory.initElements(driver, expectedPage);
}
和
public class SecondPage {
WebDriver driver;
DriverBase base;
//...
//Elements locators...
//Some methods...
//...
public SecondPage(DriverBase base) {
this.base = base;
this.driver = base.driver;
PageFactory.initElements(driver, this);
}
}
我StepsDefinition类:
public class MyTestStepsDefs {
DriverBase base;
Navigator navigator;
@Given("^bla-bla$"){
public void go_from_first_to_second_page() {
navigator.openFirstPage().openSecondOrThirdPage("http://urlOfMyPage.com", SecondPage.class);
}
@When("^blu-blu$")
public void login_with_selected_role() {
System.out.println("Some log");
}
@Then("^blo-blo$")
public void check_links_available(List<String> availableLinks) {
System.out.println("Some log");
}
所以,当我运行这个黄瓜测试 - 在openSecondOrThirdPage方法错误显示:
java.lang.RuntimeException: java.lang.InstantiationException: myprjct.pages.SecondPage
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66)
at myprjct.pages.FirstPage.openSecondOrThirdPage(FirstPage.java:31)
.......
Caused by: java.lang.InstantiationException: myprjct.pages.SecondPage
at java.lang.Class.newInstance(Class.java:359)
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:133)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66)
........
请告诉我,我doind错了什么?
正如我所看到的方法被声明为接受类型字符串和泛型类'openSecondOrThirdPage(String secondPgUrl,类 expectedPage)'的参数''但我假设你传递了错误的参数'openSecondOrThirdPage(driver,SecondPage。class)'因为我敢打赌你的'driver'是WebDriver类的一个实例。不是吗? –
olyv
2014-09-19 05:18:57
是的,你是对的。 - 我修正了它 - 当我为这篇文章写这个例子时,这只是一个错误。 – Uniqobot 2014-09-19 07:10:36