PageObject instance call does something

So I have a page object which handles all the major functionalities of the page the user is on. I want to do it so when the instance of the page object is called something happens.In the current situation I have:

public MyPageObject MY_SCREEN = new MyPageObject(this);

and when I call MY_SCREEN.fillMyScreenFields(); I want MY_SCREEN to navigate to that screen, without implementing a navigate function in fillMyScreenFields()

Upvotes: 0

Views: 61

Answers (1)

Andrejs
Andrejs

Reputation: 11981

I'm still unsure what you're after and how you get a SO exception, but here are your options:

public class HomePage {

    Webdriver driver; // inject an instance using a DI framework

    // option 1: uses the above instance, created by DI or just plain 'new' keyword
    public HomePage(){
        driver.get("https://yourpage.com/");
    }

    // option 2: pass in the driver in your tests
    public HomePage(WebDriver driver){
        driver.get("https://yourpage.com/");
    }

    // option 3: best one, I'd advise against the above two options, 
    // there will come a situation when you want to init a page object, 
    // but you don't want to navigate to it
    public void openPage(){
        driver.get("https://yourpage.com/");
    }
}

Here's a repo with a simple Page Object pattern example

Here's another repo with more complex Page Object pattern example that uses a fluent interface

(Disclaimer: both are mine)

Upvotes: 1

Related Questions