Please, help: I am using cucumber-jvm+WebDriver+jUnit+maven with Page Object pattern for Automation Testing.
I want to have a method which can return multiple types of objects. (different expected pages). In my past i used Generics to implement it with clear java+Webdriver. In This Post there is a good explanation of this.
But now i want to inplement it with cucumber.
My Project Structure looks next way:
Driver base class:
public class DriverBase {
public static WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
@After
public void tearDown() throws Exception {
driver.quit();
}
}
Navigator Class for interacting between page objects:
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);
}
}
Page objects classes:
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);
}
and
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);
}
}
My StepsDefinition class:
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");
}
So, when i run this cucumber test - on openSecondOrThirdPage method error appears:
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)
........
Please, suggest me, what i am doind wrong?