使用Cucumber-jvm和Page Objects的泛型不起作用

时间:2014-09-18 19:15:54

标签: java generics webdriver cucumber pageobjects

请帮助: 我正在使用cucumber-jvm + WebDriver + jUnit + maven和Page Object模式进行自动化测试。

我想要一个可以返回多种类型对象的方法。 (不同的预期页面)。 在我过去,我使用Generics用清晰的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();
    }
}

用于在页面对象之间进行交互的Navigator类:

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)
    ........

请建议我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的问题可能是您的Page Object没有符合以下条件之一的构造函数:

  • 采取&#34; WebDriver&#34;作为唯一的参数( public SomePage(WebDriver driver))。
  • 默认构造函数。

您的构造函数类似于 public SecondPage(DriverBase base),并且假设您编写了一个带有类参数的构造函数,那么Java不会生成默认构造函数,这可能是问题。 PageFactory无法实例化您的页面对象,因为它无法找到合适的构造函数。

您可以在此处找到有关PageFactory的更多信息。 https://code.google.com/p/selenium/wiki/PageFactory

希望这有帮助。