請幫助: 我正在使用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