PageObject Initialization Best Practices

@crujzo

public abstract class Page {

    protected WebDriver driver;
    private int defaultLook = 20; //default look for elements
    private int fastLook = 7; // wait for 7 sec

    public Page(WebDriver driver) {
        this.driver = driver;
        setDefaultTiming();
    }

    public void setDefaultTiming() {
        PageFactory.initElements(new AppiumFieldDecorator(driver, defaultLook, TimeUnit.SECONDS), this);
    }

    public void setFastLookTiming() {
        PageFactory.initElements(new AppiumFieldDecorator(driver, fastLook, TimeUnit.SECONDS), this);
    }
}

public class SomePage extends Page {

    // top
    @iOSFindBy(id = "headerTitle")
    private List<IOSElement> headerTitle;

    // main container
    @iOSFindBy(id = "someViewControllerScene_someStory")
    private List<IOSElement> mainContainer;

    // button
    @iOSFindBy(id = "myButton")
    private List<IOSElement> myButton;

    public SomePage(WebDriver driver) {
        super(driver);
    }

    public boolean isSomePageLoaded() {
        System.out.println("  verify 'Some Page' Screen loaded");
        boolean bool;
        setFastLookTiming();
        bool = !mainContainer.isEmpty();
        setDefaultTiming();
        return bool;
    }

    public boolean tapMyButton() {
        System.out.println("  tap 'My' button");
        try {
            return tapElement(myButton.get(0));
        } catch (Exception e) {
            return false;
        }
    }
}


public class BaseTest {
    protected static AppiumDriver driver = null;
    protected static AppiumDriver driver_2 = null;
    protected static WebDriver webDriver = null;
    private static DesiredCapabilities capabilities = null;

    public String user1_Email = "mail"
    public String user1_Pass = "pass"


    @BeforeTest(alwaysRun=true)
    @Parameters ({"myParameter", ...})
    public void beforeTest(@Optional String myParameter, ITestContext iTestContext) throws Exception {
        System.out.println("BeforeTest: ");
        // some code what needed
    }


    @BeforeMethod(alwaysRun=true)
    @Parameters ({"myAnotherParameter", ...})
    public void beforeMethod(@Optional String myAnotherParameter, Object[] testArgs, Method method, ITestContext iTestContext) throws Exception {
        // another serious code
    }

    @AfterMethod(alwaysRun = true)
    public void afterMethod(ITestResult iTestResult, ITestContext iTestContext) throws Exception {
        System.out.println("AfterMethod: ");
        System.out.println("Test " + iTestResult.getName() + ", status " + getResultAsText(iTestResult.getStatus()));
        // some shit after test e.g. take screenshot if it fails
    }

    @AfterClass(alwaysRun=true)
    @Parameters ()
    public void afterClass(ITestContext iTestContext) throws Exception {
        System.out.println("AfterClass: ");

        System.out.println("  ---------------------------");
        System.out.println("  partially completed results");
        System.out.println("   passed  tests: "+iTestContext.getPassedTests().size());
        System.out.println("   skipped tests: "+iTestContext.getSkippedTests().size());
        System.out.println("   failed  tests: "+iTestContext.getFailedTests().size());
    }

    @AfterTest(alwaysRun = true)
    public void afterTest(ITestContext iTestContext) throws Exception {
        System.out.println("AfterTest: ");
        // something more
    }

    @AfterSuite(alwaysRun=true)
    @Parameters()
    public void tearDown(ITestContext iTestContext) throws Exception{
        System.out.println("AfterSuite: ");
        // any finals
    }

}

public class BaseTest_iOS extends BaseTest {
    // e.g. accept iOS Alert
    public boolean acceptAlert(int waitTimeInSec) {
        System.out.println("   wait to dismiss dialog");
        WebDriverWait wait = new WebDriverWait(driver, waitTimeInSec);
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            driver.switchTo().alert().accept();
            return true;
        } catch (Exception e) {
            System.err.println("   no alert visible after "+waitTimeInSec+" sec.");
            return false;
        }
    }
}

// now some real test
public class mySuperTest extends BaseTest_iOS {

    private MainPage mainPage;
    private SwtichPage switchPage;

    @Test
    public void do_mySuperTest() {

        //do test
        System.out.println("---------- do test ----------");
        doLogin(user1_Email,user1_Pass);
        mainPage = PageFactory.initElements(driver, MainPage.class);
        assertTrue("'Switch Account' icon NOT loaded", mainPage.tapSwitch());

        switchPage = PageFactory.initElements(driver, SwitchPage.class);
        assertTrue("'Switch' screen NOT loaded", switchPage.isSwitchPageLoaded());
        assertTrue("Tap by name SomeUserName FAILED", switchPage.tapByName("SomeUserName"));

    }
}
2 Likes