Skip to content

Commit

Permalink
Tests can now manage whether cookies should be cleared between each test
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Feb 4, 2016
1 parent 0a9e50a commit 2e2450a
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.thucydides.core.annotations;

public enum ClearCookiesPolicy {
Never, BeforeEachTest
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static net.thucydides.core.annotations.ClearCookiesPolicy.BeforeEachTest;

/**
* Annotation that marks a WebDriver field as one that is managed by the Test Runner.
* The Thucydides Test Runner will instantiate this WebDriver before the tests start,
* The Serenity Test Runner will instantiate this WebDriver before the tests start,
* and close it once they have all finished.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Managed {
boolean uniqueSession() default false;
ClearCookiesPolicy clearCookies() default BeforeEachTest;
String driver() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public boolean isUniqueSession() {
return field.getAnnotation(Managed.class).uniqueSession();
}

public ClearCookiesPolicy getClearCookiesPolicy() {
return field.getAnnotation(Managed.class).clearCookies();
}

public String getDriver() {
return field.getAnnotation(Managed.class).driver();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class TestCaseAnnotations {

private final Object testCase;

private TestCaseAnnotations(final Object testCase) {
public TestCaseAnnotations(final Object testCase) {
this.testCase = testCase;
}

Expand Down Expand Up @@ -80,4 +80,8 @@ public static boolean isWebTest(Class<?> testClass) {
return findOptionalAnnotatedField(testClass).isPresent();
}

public static boolean shouldClearCookiesBeforeEachTestIn(Class<?> testClass) {
ManagedWebDriverAnnotatedField webDriverField = findFirstAnnotatedField(testClass);
return webDriverField.getClearCookiesPolicy() == ClearCookiesPolicy.BeforeEachTest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface Configuration {

int getElementTimeout();

boolean getUseUniqueBrowser();
boolean shouldUseAUniqueBrowser();

void setOutputDirectory(File outputDirectory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public int getElementTimeout() {

}

public boolean getUseUniqueBrowser() {
public boolean shouldUseAUniqueBrowser() {
return ThucydidesSystemProperty.THUCYDIDES_USE_UNIQUE_BROWSER.booleanFrom(getEnvironmentVariables());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,26 @@ public void the_unique_browser_value_can_be_defined_in_a_system_property() {
public void the_output_directory_can_be_defined_in_a_system_property() {
environmentVariables.setProperty("thucydides.use.unique.browser", "true");

assertThat(configuration.getUseUniqueBrowser(), is(true));
assertThat(configuration.shouldUseAUniqueBrowser(), is(true));
}

@Test
public void system_properties_cannot_be_set_if_defined() {
environmentVariables.setProperty("thucydides.use.unique.browser", "true");
configuration.setIfUndefined("thucydides.use.unique.browser", "false");

assertThat(configuration.getUseUniqueBrowser(), is(true));
assertThat(configuration.shouldUseAUniqueBrowser(), is(true));
}

@Test
public void system_properties_can_be_set_if_undefined() {
configuration.setIfUndefined("thucydides.use.unique.browser", "false");

assertThat(configuration.getUseUniqueBrowser(), is(false));
assertThat(configuration.shouldUseAUniqueBrowser(), is(false));
}

@Test
public void the_default_unique_browser_value_should_be_false() {
assertThat(configuration.getUseUniqueBrowser(), is(false));
assertThat(configuration.shouldUseAUniqueBrowser(), is(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import net.serenitybdd.core.injectors.EnvironmentDependencyInjector;
import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.annotations.ManagedWebDriverAnnotatedField;
import net.thucydides.core.annotations.Manual;
import net.thucydides.core.annotations.Pending;
import net.thucydides.core.annotations.TestCaseAnnotations;
import net.thucydides.core.batches.BatchManager;
import net.thucydides.core.batches.BatchManagerProvider;
Expand All @@ -28,7 +26,6 @@
import net.thucydides.core.webdriver.*;
import net.thucydides.junit.listeners.JUnitStepListener;
import org.apache.commons.lang3.StringUtils;
import org.junit.Ignore;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
Expand Down Expand Up @@ -66,6 +63,7 @@ public class SerenityRunner extends BlockJUnit4ClassRunner {
private String requestedDriver;
private ReportService reportService;
private final TestCount testCount;
private final TestConfiguration theTest;
/**
* Special listener that keeps track of test step execution and results.
*/
Expand Down Expand Up @@ -156,6 +154,8 @@ public SerenityRunner(final Class<?> klass,
final Configuration configuration,
final BatchManager batchManager) throws InitializationError {
super(klass);

this.theTest = TestConfiguration.forClass(klass).withSystemConfiguration(configuration);
this.webdriverManager = webDriverManager;
this.configuration = configuration;
this.requestedDriver = getSpecifiedDriver(klass);
Expand Down Expand Up @@ -330,7 +330,7 @@ private void buildAndConfigureListeners() {
}

private RunNotifier initializeRunNotifier(RunNotifier notifier) {
if (shouldRetryTest()) {
if (theTest.shouldRetryTest()) {
notifier.addListener(getStepListener());
return notifier;
} else {
Expand Down Expand Up @@ -408,21 +408,22 @@ private void generateReportsFor(final List<TestOutcome> testOutcomeResults) {
getReportService().generateConfigurationsReport();
}


@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {

TestMethodConfiguration theMethod = TestMethodConfiguration.forMethod(method);

clearMetadataIfRequired();

if (shouldSkipTest(method)) {
return;
}

if (isManual(method)) {
if (theMethod.isManual()) {
markAsManual(method);
notifier.fireTestIgnored(describeChild(method));
return;
} else if (isPending(method)) {
} else if (theMethod.isPending()) {
markAsPending(method);
notifier.fireTestIgnored(describeChild(method));
return;
Expand All @@ -433,18 +434,18 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) {
FailureDetectingStepListener failureDetectingStepListener = new FailureDetectingStepListener();
StepEventBus.getEventBus().registerListener(failureDetectingStepListener);

int maxRetries = shouldRetryTest() ? getConfiguration().maxRetries() : 0;
for (int attemptCount = 0; attemptCount < maxRetries + 1; attemptCount += 1) {
for (int attemptCount = 0; attemptCount < theTest.getMaxRetries() + 1; attemptCount += 1) {
if (notifier instanceof RetryFilteringRunNotifier) {
((RetryFilteringRunNotifier) notifier).reset();
}
if (attemptCount > 0) {
logger.warn("{} failed, making attempt number {} out of 1 base call + {} retries", method.getName(), attemptCount, maxRetries);
logger.warn("{} failed, making attempt number {} out of 1 base call + {} retries",
method.getName(), attemptCount, theTest.getMaxRetries());
StepEventBus.getEventBus().testRetried();
}

initializeTestSession();
resetBroswerFromTimeToTime();
prepareBrowserForTest();
additionalBrowserCleanup();
failureDetectingStepListener.reset();

Expand All @@ -461,7 +462,7 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) {
}

private void clearMetadataIfRequired() {
if (!ThucydidesSystemProperty.THUCYDIDES_MAINTAIN_SESSION.booleanFrom(configuration.getEnvironmentVariables())) {
if (theTest.shouldClearMetadata()) {
Serenity.getCurrentSession().clearMetaData();
}
}
Expand Down Expand Up @@ -503,52 +504,12 @@ private void processTestMethodAnnotationsFor(FrameworkMethod method) {
}
}


private boolean isPending(FrameworkMethod method) {
return method.getAnnotation(Pending.class) != null;
}

private boolean isManual(FrameworkMethod method) {
return method.getAnnotation(Manual.class) != null;
}

protected boolean isIgnored(FrameworkMethod method) {
return method.getAnnotation(Ignore.class) != null;
}

protected boolean restartBrowserBeforeTest() {
return notAUniqueSession() || dueForPeriodicBrowserReset();
}

private boolean dueForPeriodicBrowserReset() {
return shouldRestartEveryNthTest() && (currentTestNumber() % restartFrequency() == 0);
}

private boolean notAUniqueSession() {
return !isUniqueSession();
}

protected int restartFrequency() {
return getConfiguration().getRestartFrequency();
}

protected int currentTestNumber() {
return testCount.getCurrentTestNumber();
}


protected boolean shouldRestartEveryNthTest() {
return (restartFrequency() > 0);
}

protected boolean isUniqueSession() {
return (TestCaseAnnotations.isUniqueSession(getTestClass().getJavaClass()) || configuration.getUseUniqueBrowser());
}

protected void resetBroswerFromTimeToTime() {
if (isAWebTest() && restartBrowserBeforeTest()) {
protected void prepareBrowserForTest() {
if (theTest.needsToRestartTheBrowser()) {
WebdriverProxyFactory.resetDriver(getDriver());
} else {
}

if (theTest.shouldClearTheBrowserSession()) {
WebdriverProxyFactory.clearBrowserSession(getDriver());
}
}
Expand Down Expand Up @@ -639,8 +600,4 @@ public List<TestOutcome> getTestOutcomes() {
protected Collection<AcceptanceTestReporter> getDefaultReporters() {
return ReportService.getDefaultReporters();
}

public boolean isAWebTest() {
return TestCaseAnnotations.isWebTest(getTestClass().getJavaClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.serenitybdd.junit.runners;

import net.thucydides.core.annotations.TestCaseAnnotations;
import org.junit.runners.model.TestClass;

/**
* Created by john on 4/02/2016.
*/
public class TestClassAnnotations {


private final TestClass testClass;

public TestClassAnnotations(Class<?> testClass) {
this.testClass = new TestClass(testClass);
}

public static TestClassAnnotations forTestClass(Class<?> testClass) {
return new TestClassAnnotations(testClass);
}

public boolean toUseAUniqueSession() {
return TestCaseAnnotations.isUniqueSession(testClass.getJavaClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package net.serenitybdd.junit.runners;

import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.annotations.TestCaseAnnotations;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.statistics.TestCount;
import net.thucydides.core.webdriver.Configuration;
import org.junit.runners.model.TestClass;

public class TestConfiguration {


private final Class<?> testClass;
private final Configuration configuration;
private final TestCount testCount;
private final TestClassAnnotations theTestIsAnnotated;

public TestConfiguration(Class<?> testClass, Configuration configuration) {
this.testClass = testClass;
this.configuration = configuration;
this.testCount = Injectors.getInjector().getInstance(TestCount.class);
this.theTestIsAnnotated = TestClassAnnotations.forTestClass(testClass);
}

public boolean shouldRetryTest() {
return (ThucydidesSystemProperty.JUNIT_RETRY_TESTS.booleanFrom(configuration.getEnvironmentVariables()));
}

public int getMaxRetries() {
return shouldRetryTest() ? configuration.maxRetries() : 0;
}

public boolean shouldClearMetadata() {
return (!ThucydidesSystemProperty.THUCYDIDES_MAINTAIN_SESSION.booleanFrom(configuration.getEnvironmentVariables()));
}

public static TestConfigurationBuilder forClass(Class<?> testClass) {
return new TestConfigurationBuilder(testClass);
}

public boolean needsToRestartTheBrowser() {
return (isAWebTest() && restartBrowserBeforeTest());
}

protected boolean restartBrowserBeforeTest() {
return notAUniqueSession() || dueForPeriodicBrowserReset();
}

private boolean dueForPeriodicBrowserReset() {
return shouldRestartEveryNthTest() && (currentTestNumber() % restartFrequency() == 0);
}

private boolean notAUniqueSession() {
return !isUniqueSession();
}

protected boolean isUniqueSession() {
return (theTestIsAnnotated.toUseAUniqueSession() || configuration.shouldUseAUniqueBrowser());
}

protected boolean shouldRestartEveryNthTest() {
return (restartFrequency() > 0);
}

protected int restartFrequency() {
return configuration.getRestartFrequency();
}

protected int currentTestNumber() {
return testCount.getCurrentTestNumber();
}

public boolean shouldClearTheBrowserSession() {
return (isAWebTest() && TestCaseAnnotations.shouldClearCookiesBeforeEachTestIn(testClass().getJavaClass()));
}

public static class TestConfigurationBuilder {

private final Class<?> testClass;

public TestConfigurationBuilder(Class<?> testClass) {
TestClass t = new TestClass(testClass);
this.testClass = testClass;
}

public TestConfiguration withSystemConfiguration(Configuration configuration) {
return new TestConfiguration(testClass, configuration);
}
}

private TestClass testClass() {
return new TestClass(testClass);
}

public boolean isAWebTest() {
return TestCaseAnnotations.isWebTest(testClass().getJavaClass());
}

}
Loading

0 comments on commit 2e2450a

Please sign in to comment.