2

I am fairly new to using POM and I am wondering if there is a best practice when returning to a previously visited page. Is it better to create a new page object every time there is a page navigation? Or is it ok to reuse variables where the page is already instantiated?

Example of Page object reuse: Since loginpage is already defined I just use it again

public void LoginTwice()
{
    LoginPage loginpage= new LoginPage();
    loginpage.GoTo();
    loginpage.login("user","pass");
    WelcomePage wlcmPage= new WelcomePage();
    wlcmPage.Logout();
    
    //login again
    login.GoTo();
    loginpage.login("user","pass");
    wlcmPage.clickMenu();
}

Example of new page object creation: loginpage is set to a new instance of LoginPage

public void LoginTwice()
{
    LoginPage loginpage= new LoginPage();
    loginpage.GoTo();
    loginpage.login("user","pass");
    WelcomePage wlcmPage= new WelcomePage();
    wlcmPage.Logout();
    
    //login again
    loginpage= new LoginPage();
    login.GoTo();
    loginpage.login("user","pass");
    wlcmPage= new WelcomePage();
    wlcmPage.clickMenu();
}
1
 there is a best practice when

"There are good practices in context, but there are no best practices."

When you first create the LoginPage object, it has the context of the first landing on the page. It may have loaded internal information relevant to this first landing (which the tests don't need to know).

When you perform login and logout and then use the same object from before, its internal state may not match the current state of the application (which is different from the first landing).

One approach you can do is to delegate to the page objects the responsibility of understanding under which state the page object should be, using Method Chaining.

Your tests could something like this:

LoginPage loginpage = new LoginPage();
loginpage.visit()
         .login("user","pass")
         .logout()
         .login("user","pass")
         .clickMenu();

When you perform visit, the method will return a LoginPage object, login will return a WelcomePage object, logout will return a LoginPage object, so on and so forth...

Whatever details these objects will need to have can be configured by the methods you are calling in your tests. These methods should know the full context of what happens when you perform some action, not your tests. The tests should care only about the user flow.

4
  • Ok so each of these methods would return a new object every time? – Eric Apr 5 at 17:22
  • Yes. Typically, if the action doesn't change the page, the method would "return this;" - otherwise, it would create the proper page object representing the page the action led to and "return new FollowingPage();" – João Farias Apr 6 at 7:06
  • Ok thank you for your input. One other quick question related to pages... what if the action doesn't always change the page? Or what if the action has the possibility of returning two different pages depending on the scenario? Would you just create a method that returns each possible page? – Eric Apr 26 at 15:28
  • "doesn't always change the page" => "return this;" || "two different pages depending on the scenario" => I can imagine you refer to a possible second tab or windowed page. In this case, you can return a data structure that contains references to the two page-objects and let the client decide how to drive the user flow. – João Farias Apr 27 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.