1
votes

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?

1
As I can see method is declared to accept arguments of type string and generic class openSecondOrThirdPage(String secondPgUrl, Class<T> expectedPage) but I assume you pass wrong arguments openSecondOrThirdPage(driver, SecondPage.class) because I bet your driver is an instance of WebDriver class. Isn't it? - olyv
Yes, You was right. - i have fixed it - it was just a mistake when i were writing this example for post. - Uniqobot

1 Answers

0
votes

Your problem might be that your Page Object does not have a constructor that matches one of these criteria:

  • Takes "WebDriver" as its sole argument (public SomePage(WebDriver driver)).
  • Default constructor.

Your constructor is something like public SecondPage(DriverBase base) instead, and given that if you write a constructor with arguments for your class then Java does not generate a default constructor, that might be the problem. PageFactory cannot instantiate your page object because it cannot find a suitable constructor.

You can find more information about the PageFactory here. https://code.google.com/p/selenium/wiki/PageFactory

Hope this helps.