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

net.thucydides.core.annotations.TestCaseAnnotations Maven / Gradle / Ivy

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

import net.serenitybdd.core.environment.*;
import net.thucydides.core.configuration.*;
import net.thucydides.core.webdriver.*;
import org.junit.runner.*;
import org.openqa.selenium.*;

import java.util.*;

import static net.thucydides.core.annotations.ManagedWebDriverAnnotatedField.*;
import static org.apache.commons.lang3.StringUtils.*;

/**
 * Utility class used to inject fields into a test case.
 * @author johnsmart
 *
 */
public final class TestCaseAnnotations {

    private final Object testCase;
    private final DriverConfiguration configuration;

    public TestCaseAnnotations(final Object testCase, WebDriverConfiguration configuration) {
        this.testCase = testCase;
        this.configuration = configuration;
    }

    public TestCaseAnnotations(final Object testCase) {
        this.testCase = testCase;
        this.configuration = WebDriverConfiguredEnvironment.getDriverConfiguration();
    }

    public static TestCaseAnnotations forTestCase(final Object testCase) {
        return new TestCaseAnnotations(testCase);
    }

    /**
     * Instantiate the @Managed-annotated WebDriver instance with current WebDriver if the annotated field is present.
     */
    public void injectDriver(final WebDriver driver) {
        java.util.Optional webDriverField
                = findOptionalAnnotatedField(testCase.getClass());
        webDriverField.ifPresent(managedWebDriverAnnotatedField -> managedWebDriverAnnotatedField.setValue(testCase, driver));
    }

    public void injectDrivers(final WebdriverManager webdriverManager) {
        injectDrivers(ThucydidesWebDriverSupport.getDriver(),webdriverManager);
    }

    public void injectDrivers(final WebDriver defaultDriver, final WebdriverManager webdriverManager) {
        List webDriverFields = findAnnotatedFields(testCase.getClass());
        int driverCount = 1;

        String suffix = "";
        for(ManagedWebDriverAnnotatedField webDriverField : webDriverFields) {
            String driverRootName = isNotEmpty(webDriverField.getDriver()) ?  webDriverField.getDriver() : configuredDriverType();
            String driverName = driverRootName + suffix;
            String driverOptions = webDriverField.getOptions();
            WebDriver driver = (isEmpty(driverName)) ? defaultDriver : requestedDriverFrom(webdriverManager, webDriverField.getName(), driverName, driverOptions);
            webDriverField.setValue(testCase, driver);

            suffix = nextSuffix(driverCount++);
        }
    }

    private WebDriver requestedDriverFrom(WebdriverManager webdriverManager, String fieldName, String driverName, String driverOptions) {

        return RequestedDrivers.withEnvironmentVariables(configuration.getEnvironmentVariables())
                               .andWebDriverManager(webdriverManager)
                               .requestedDriverFor(fieldName, driverName, driverOptions);
    }

    private String configuredDriverType() {
        if (ThucydidesWebDriverSupport.isInitialised()) {
            return ThucydidesWebDriverSupport.getCurrentDriverName();
        }
        return configuration.getDriverType().name();
    }

    private String nextSuffix(int driverCount) {
        return String.format(":%d", driverCount + 1);
    }

    /**
     * Does this class support web tests?
     * Test cases that support web tests need to have at least a WebDriver field annotated with the @Managed
     * annotation.
     */
    public static boolean supportsWebTests(Class clazz) {
        return hasManagedWebdriverField(clazz);
    }

    public boolean isUniqueSession() {
        return isUniqueSession(testCase.getClass());
    }


    public static boolean isUniqueSession(Class testClass) {
        ManagedWebDriverAnnotatedField webDriverField = findFirstAnnotatedField(testClass);
        return webDriverField.isUniqueSession();
    }

    public static boolean isWebTest(Class testClass) {
        return (testClass != null) && findOptionalAnnotatedField(testClass).isPresent();
    }

    public static boolean shouldClearCookiesBeforeEachTestIn(Class testClass) {
        ManagedWebDriverAnnotatedField webDriverField = findFirstAnnotatedField(testClass);
        return webDriverField.getClearCookiesPolicy() == ClearCookiesPolicy.BeforeEachTest;
    }

    public static boolean shouldUsePersistantStepLibraries(Class testClass) {
        return (testClass != null) && (testClass.getAnnotation(UsePersistantStepLibraries.class) != null);
    }

    public static boolean isASerenityTestCase(Class testClass) {
        return (testClass != null)
                && (testClass.getAnnotation(RunWith.class) != null)
                && (testClass.getAnnotation(RunWith.class).value().toString().contains("Serenity") || testClass.getAnnotation(RunWith.class).value().toString().contains("Thucydides"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy