All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.core.pages.PageObjects Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package net.serenitybdd.core.pages;

import org.openqa.selenium.WebDriver;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

public class PageObjects {

    public static final String NO_WEBDRIVER_CONSTRUCTOR_MESSAGE = "This page object does not appear have a constructor that takes a WebDriver parameter";

    private final WebDriver driver;

    public PageObjects(WebDriver driver) {
        this.driver = driver;
    }

    public static PageObjects usingDriver(WebDriver driver) {
        return new PageObjects(driver);
    }

    public  T ofType(Class pageObjectClass) {
        try {
            Optional simplePageObject = newPageObjectWithSimpleConstructor(pageObjectClass);
            return (simplePageObject.isPresent() ? simplePageObject.get() : newPageObjectWithDriver(pageObjectClass));
        } catch (Throwable somethingWentWrong) {
            throw pageLooksDodgyExceptionBasedOn(somethingWentWrong, pageObjectClass);
        }
    }

    private  PageLooksDodgyException pageLooksDodgyExceptionBasedOn(Throwable somethingWentWrong, Class pageObjectClass) {
        if (somethingWentWrong instanceof NoSuchMethodException) {
            return thisPageObjectLooksDodgy(pageObjectClass, NO_WEBDRIVER_CONSTRUCTOR_MESSAGE, somethingWentWrong);
        }
        if (somethingWentWrong instanceof InvocationTargetException) {
            return thisPageObjectLooksDodgy(pageObjectClass,"Failed to instantiate page",
                                           ((InvocationTargetException) somethingWentWrong).getTargetException());
        }
        return thisPageObjectLooksDodgy(pageObjectClass,"Failed to instantiate page", somethingWentWrong);
    }


    @SuppressWarnings("unchecked")
    private  Optional newPageObjectWithSimpleConstructor(Class pageObjectClass)
            throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        try {
            Class[] constructorArgs = new Class[0];
            Constructor constructor = pageObjectClass.getConstructor(constructorArgs);
            T newPage = (T) constructor.newInstance();
            newPage.setDriver(driver);
            return Optional.of(newPage);

        } catch (NoSuchMethodException e) {
            // Try a different constructor
        }
        return Optional.empty();
    }

    @SuppressWarnings("unchecked")
    private  T newPageObjectWithDriver(Class pageObjectClass)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class[] constructorArgs = new Class[1];
        constructorArgs[0] = WebDriver.class;
        Constructor constructor = pageObjectClass.getConstructor(constructorArgs);
        return (T) constructor.newInstance(driver);
    }

    private PageLooksDodgyException thisPageObjectLooksDodgy(final Class pageObjectClass,
                                               String message,
                                               Throwable e) {
        return new PageLooksDodgyException("The page object " + pageObjectClass + " looks dodgy:\n" + message,e);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy