Question about test classes and Java constructors

I’m a relative newbie to automation and Java, so I’m sure I’m making some mistakes. Based on examples that I’ve seen, I’ve been importing Page Object files and created constructors for every page that my iOS and Android apps hit. Example for my SignIn test:

import Pages.HomePagePO;
import Pages.SignInPO;
….
public class SignInTest {

@Test
public void ValidateSignInByEmail() throws InterruptedException{
HomePagePO homePage;
homePage = new HomePagePO(driver);
homePage.clickSignIn();

SignInPO signInPage;
signInPage = new SignInPO(driver); 
signInPage.setUser(“[email protected]”); 

……

Most of my tests consists of hitting 5-10 different pages, so I’ve been importing that many pages and declaring/creating constructors for each page I land on during the test flow. I guess I can save some code by importing Pages.* (if loading many unnecessary classes isn’t a bad thing), but is there any way to get around declaring the reference and creating a contractor for each page used in each test class? Is it possible to just do this in one main class and extend the test classes to this other class were the declarations and constructors are kept (even though I would only use a subset of them)?

So one thing I’m doing now that I like is to create a simple class called “AllPages” that contains all of my application’s page objects. So in your example it would be something like…

public class AllPages {
    public HomePagePO homePage = new HomePagePO();
    public SignInPO signInPage = new SignInPO();
}

Then, I have a base test case class that includes this AllPages class something like this…

public class BaseTestClass {
    protected AllPages pages = new AllPages();
}

So your test suite looks like this…

public class SignInTestSuite extends BaseTestClass {
    @Test
    public void ValidateSignInByEmail() throws InterruptedException{
        pages.homePage.clickSignIn();
        pages.signInPage.setUser(“[email protected]”);
    }
}

What I like about this is that you don’t have to include or instantiate any pages in your test case suite classes and I get an auto-complete popup with page choices in my IDE by just typing ‘pages.’ each time when writing a test case. I left out a lot of other details, like I have other lower level classes that contain the appium driver and other variables, so I don’t need to pass that thing around everywhere, but this can get you started. Good luck!

1 Like

I didn’t know that all Page Objects can be declared in one class. That would make things a lot simpler. What you describe above is exactly what I’m trying to accomplish. Thanks a lot for the tip!