com.mycompany.selenium.factory.tests.Init.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.selenium.factory.tests.Init.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.selenium.factory.tests;

import com.mycompany.selenium.factory.Page;
import cucumber.api.PendingException;

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

import org.apache.commons.lang3.reflect.FieldUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.reflections.Reflections;

/**
 *
 * @author sidochenko
 */
public class Init {

    public static WebDriver driver;

    public static String currentPageTitle;
    public static Page currentPage;

    private static final String pagesPackage = "com.mycompany.selenium.factory.pages";

    public static WebDriver getDriver() {
        if (null == driver) {
            driver = new FirefoxDriver();
            driver.get("http://yandex.ru");
        }
        return driver;
    }

    /*
     * TODO: ?     ?.   
     *  ? ?  ??  ?. UPD: ??
     * ?     ,    ??  .
     * ?   ? ? .
     */
    public static Page getPage(String title) throws Exception {
        if (null == currentPage || !currentPageTitle.equals(title)) {
            if (null != currentPage) {
                currentPage = getPage(currentPage.getClass().getPackage().getName(), title);
            }

            if (null == currentPage) {
                currentPage = getPage(pagesPackage, title);

            }
            if (null == currentPage) {

                throw new PendingException("Page Object with title " + title + " is not registered");
            }

        }
        return currentPage;
    }

    private static Page getPage(String packageName, String title) throws Exception {
        Reflections reflections = new Reflections(packageName);
        Set<Class<? extends Page>> allClasses = reflections.getSubTypesOf(Page.class);
        for (Class<? extends Page> page : allClasses) {
            String pageTitle = (String) FieldUtils.readStaticField(page, "title", true);
            if (pageTitle.equals(title)) {
                @SuppressWarnings("unchecked")
                Constructor<Page> c = ((Constructor<Page>) page.getConstructor());
                currentPage = c.newInstance();
                currentPageTitle = title;
                return currentPage;
            }
        }
        return null;
    }

    public static Page getCurrentPage() throws Exception {
        if (null == currentPage) {
            throw new Exception("Current page not initialized!");
        } else {
            return currentPage;
        }
    }

    public static void dispose() {
        Init.currentPage = null;
        Init.currentPageTitle = null;
        Init.driver = null;
    }

}