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

net.thucydides.core.Thucydides Maven / Gradle / Ivy

There is a newer version: 0.9.275
Show newest version
package net.thucydides.core;

import net.thucydides.core.annotations.TestCaseAnnotations;
import net.thucydides.core.pages.Pages;
import net.thucydides.core.steps.BaseStepListener;
import net.thucydides.core.steps.StepAnnotations;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.steps.StepFactory;
import net.thucydides.core.steps.StepListener;
import net.thucydides.core.webdriver.Configuration;
import net.thucydides.core.webdriver.WebDriverFactory;
import net.thucydides.core.webdriver.WebdriverManager;
import org.openqa.selenium.WebDriver;

import java.io.File;

/**
 * A utility class that provides services to initialize web testing and reporting-related fields in arbitrary objects.
 * It is designed to help integrate Thucydides into other testing tools such as Cucumber.
 */
public class Thucydides {

    private static final ThreadLocal factoryThreadLocal = new ThreadLocal();
    private static final ThreadLocal webdriverManagerThreadLocal = new ThreadLocal();
    private static final ThreadLocal pagesThreadLocal = new ThreadLocal();
    private static final ThreadLocal stepFactoryThreadLocal = new ThreadLocal();
    private static final ThreadLocal stepListenerThreadLocal = new ThreadLocal();

    /**
     * Initialize Thucydides-related fields in the specified object.
     * This includes managed WebDriver instances,
     */
    public static void initialize(final Object testCase) {
        setupWebDriverFactory();
        setupWebdriverManager();

        initPagesObjectUsing(getDriver());
        initStepListener();
        initStepFactoryUsing(getPages());

        injectDriverInto(testCase);
        injectAnnotatedPagesObjectInto(testCase);
        injectScenarioStepsInto(testCase);

    }

    private static void initStepListener() {
        File outputDirectory = Configuration.loadOutputDirectoryFromSystemProperties();
        StepListener listener  = new BaseStepListener(outputDirectory, getPages());
        stepListenerThreadLocal.set(listener);
        StepEventBus.getEventBus().registerListener(getStepListener());
    }

    private static void setupWebDriverFactory() {
        factoryThreadLocal.set(new WebDriverFactory());
    }

    private static void initPagesObjectUsing(final WebDriver driver) {
        pagesThreadLocal.set(new Pages(driver));
    }

    private static void initStepFactoryUsing(final Pages pagesObject) {
        stepFactoryThreadLocal.set(new StepFactory(pagesObject));
    }

    /**
     * Instantiate the @Managed-annotated WebDriver instance with current WebDriver.
     */
    protected static void injectDriverInto(final Object testCase) {
        TestCaseAnnotations.forTestCase(testCase).injectDriver(getDriver());
    }

    /**
     * Instantiates the @ManagedPages-annotated Pages instance using current WebDriver.
     */
    protected static void injectScenarioStepsInto(final Object testCase) {
        StepAnnotations.injectScenarioStepsInto(testCase, getStepFactory());

    }

    /**
     * Instantiates the @ManagedPages-annotated Pages instance using current WebDriver.
     */
    protected static void injectAnnotatedPagesObjectInto(final Object testCase) {
        getPages().notifyWhenDriverOpens();
        StepAnnotations.injectAnnotatedPagesObjectInto(testCase, getPages());
    }

    /**
     * Indicate that the test run using this object is finished, and reports can be generated.
     */
    public static void done(final Object testClass) {
        getWebdriverManager().closeDriver();

    }

    protected static WebDriver getDriver() {
        return getWebdriverManager().getWebdriver();
    }

    protected static Pages getPages() {
        return pagesThreadLocal.get();
    }

    /**
     * Use a mock driver for testing purposes
     */
    protected static void useMockDriver(final WebDriver mockDriver) {
        setupWebdriverManager(new WebdriverManager(getWebDriverFactory()) {

            @Override
            public WebDriver getWebdriver() {
                return mockDriver;
            }
        });

    }

    protected static void stopUsingMockDriver() {
        setupWebdriverManager();
    }

    private static WebdriverManager getWebdriverManager() {
        return webdriverManagerThreadLocal.get();
    }

    private static WebDriverFactory getWebDriverFactory() {
        return factoryThreadLocal.get();
    }

    private static StepFactory getStepFactory() {
        return stepFactoryThreadLocal.get();
    }

    private static void setupWebdriverManager() {
        setupWebdriverManager(new WebdriverManager(getWebDriverFactory()));
    }

    private static void setupWebdriverManager(WebdriverManager webdriverManager) {
        webdriverManagerThreadLocal.set(webdriverManager);
    }

    public static StepListener getStepListener() {
        return stepListenerThreadLocal.get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy