Skip to content

Commit

Permalink
Refactor SharedDriver lifecycle in FluentAdapter (JUnit, TestNG & Cuc…
Browse files Browse the repository at this point in the history
…umber)
  • Loading branch information
Toilal committed Aug 10, 2016
1 parent 35162e1 commit 1c422ba
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fluentlenium.adapter;

import org.fluentlenium.adapter.util.SharedDriverStrategy;

/**
* Default implementation of {@link SharedMutator}, returning unchanged parameters.
*/
public class DefaultSharedMutator implements SharedMutator {
@Override
public <T> EffectiveParameters<T> getEffectiveParameters(Class<T> testClass, String testName, SharedDriverStrategy strategy) {
return new EffectiveParameters<>(testClass, testName, strategy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public FluentAdapter initFluent(WebDriver webDriver) {
return this;
}

public FluentAdapter quit() {
if (this.getFluentDriver() != null) {
this.getFluentDriver().quit();
this.setFluentDriver(null);
public FluentAdapter releaseFluent() {
if (getFluentDriver() != null) {
getFluentDriver().releaseFluent();
setFluentDriver(null);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.fluentlenium.adapter;

import org.junit.AfterClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
* All Junit Test should extends this class. It provides default parameters.
Expand All @@ -14,6 +15,12 @@ public abstract class FluentTest extends FluentTestRunnerAdapter {
@Rule
public TestName name = new TestName();

private static Class<?> testClass;

public FluentTest() {
testClass = this.getClass();
}

@Rule
public TestRule watchman = new FluentTestRule() {

Expand All @@ -36,9 +43,23 @@ public void failed(Throwable e, Description description) {
}
};

@AfterClass
public static void afterClass() {
FluentTestRunnerAdapter.releaseSharedDriver();
}
@ClassRule
public static TestRule classWatchman = new TestRule() {

@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {

@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} finally {
afterClass(description.getTestClass());
}
}
};
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public void evaluate() throws Throwable {
} catch (Throwable e) {
failed(e, description);
throw e;
} finally {
finished(description);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
package org.fluentlenium.adapter;

import com.google.common.base.Supplier;
import org.fluentlenium.adapter.util.CookieStrategyReader;
import org.fluentlenium.adapter.util.DefaultCookieStrategyReader;
import org.fluentlenium.adapter.util.DefaultSharedDriverStrategyReader;
import org.fluentlenium.adapter.util.SharedDriverOnceShutdownHook;
import org.fluentlenium.adapter.util.SharedDriverStrategy;
import org.fluentlenium.adapter.util.SharedDriverStrategyReader;
import org.openqa.selenium.WebDriver;

import java.util.List;

/**
* Adapter used by any class based Test Runners adapters.
*/
public class FluentTestRunnerAdapter extends FluentAdapter {

private static WebDriver sharedDriver;

private static boolean isSharedDriverPerClass;

private final SharedDriverStrategyReader sdsr;

private final CookieStrategyReader csr;

private final SharedMutator sharedMutator;
protected SharedMutator staticSharedMutator;

public FluentTestRunnerAdapter() {
this(new DefaultDriverContainer());
}

public FluentTestRunnerAdapter(DriverContainer driverContainer) {
this(driverContainer, new DefaultSharedDriverStrategyReader(),
new DefaultCookieStrategyReader());
this(driverContainer, new DefaultSharedDriverStrategyReader(), new DefaultCookieStrategyReader(), new DefaultSharedMutator());
}

public FluentTestRunnerAdapter(SharedDriverStrategyReader sharedDriverExtractor,
CookieStrategyReader cookieExtractor) {
this(new DefaultDriverContainer(), sharedDriverExtractor, cookieExtractor);
public FluentTestRunnerAdapter(SharedDriverStrategyReader sharedDriverExtractor, CookieStrategyReader cookieExtractor) {
this(new DefaultDriverContainer(), sharedDriverExtractor, cookieExtractor, new DefaultSharedMutator());
}

public FluentTestRunnerAdapter(DriverContainer driverContainer,
SharedDriverStrategyReader sharedDriverExtractor,
CookieStrategyReader cookieExtractor) {
public FluentTestRunnerAdapter(SharedDriverStrategyReader sharedDriverExtractor, CookieStrategyReader cookieExtractor, SharedMutator sharedMutator) {
this(new DefaultDriverContainer(), sharedDriverExtractor, cookieExtractor, sharedMutator);
}

public FluentTestRunnerAdapter(DriverContainer driverContainer, SharedDriverStrategyReader sharedDriverExtractor, CookieStrategyReader cookieExtractor, SharedMutator sharedMutator) {
super(driverContainer);
this.sdsr = sharedDriverExtractor;
this.csr = cookieExtractor;
this.sharedMutator = sharedMutator;
}

/**
Expand All @@ -53,8 +54,7 @@ protected void starting() {
/**
* Invoked when a test method is starting.
*
* @param testName
* Test name
* @param testName Test name
*/
protected void starting(String testName) {
starting(getClass(), testName);
Expand All @@ -63,8 +63,7 @@ protected void starting(String testName) {
/**
* Invoked when a test method is starting.
*
* @param testClass
* Test class
* @param testClass Test class
*/
protected void starting(Class<?> testClass) {
starting(testClass, testClass.getName());
Expand All @@ -73,38 +72,22 @@ protected void starting(Class<?> testClass) {
/**
* Invoked when a test method is starting.
*
* @param testClass
* Test class
* @param testName
* Test name
* @param testClass Test class
* @param testName Test name
*/
protected void starting(Class<?> testClass, String testName) {
SharedDriverStrategy strategy = sdsr.getSharedDriverStrategy(testClass, testName);

if (strategy == SharedDriverStrategy.ONCE) {
synchronized (FluentTestRunnerAdapter.class) {
if (sharedDriver == null) {
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl());
sharedDriver = getDriver();
Runtime.getRuntime().addShutdownHook(
new SharedDriverOnceShutdownHook("SharedDriver-ONCE-ShutdownHook"));
} else {
initFluent(sharedDriver).withDefaultUrl(getDefaultBaseUrl());
}
}
} else if (strategy == SharedDriverStrategy.PER_CLASS) {
synchronized (FluentTestRunnerAdapter.class) {
if (!isSharedDriverPerClass) {
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl());
sharedDriver = getDriver();
isSharedDriverPerClass = true;
} else {
initFluent(sharedDriver).withDefaultUrl(getDefaultBaseUrl());
}
SharedMutator.EffectiveParameters<?> sharedParameters = this.sharedMutator.getEffectiveParameters(testClass, testName, strategy);

SharedWebDriver sharedWebDriver = SharedWebDriverContainer.INSTANCE.getOrCreateDriver(new Supplier<WebDriver>() {
@Override
public WebDriver get() {
return FluentTestRunnerAdapter.this.getDefaultDriver();
}
} else {
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl());
}
}, sharedParameters.getTestClass(), sharedParameters.getTestName(), sharedParameters.getStrategy());

initFluent(sharedWebDriver.getDriver()).withDefaultUrl(getDefaultBaseUrl());
}

/**
Expand All @@ -117,8 +100,7 @@ protected void finished() {
/**
* Invoked when a test method has finished (whatever the success of failing status)
*
* @param testName
* Test name
* @param testName Test name
*/
protected void finished(String testName) {
finished(getClass(), testName);
Expand All @@ -127,8 +109,7 @@ protected void finished(String testName) {
/**
* Invoked when a test method has finished (whatever the success of failing status)
*
* @param testClass
* Test class
* @param testClass Test class
*/
protected void finished(Class<?> testClass) {
finished(testClass, testClass.getName());
Expand All @@ -137,17 +118,41 @@ protected void finished(Class<?> testClass) {
/**
* Invoked when a test method has finished (whatever the success of failing status)
*
* @param testClass
* Test class
* @param testName
* Test name
* @param testClass Test class
* @param testName Test name
*/
protected void finished(Class<?> testClass, String testName) {
SharedDriverStrategy strategy = sdsr.getSharedDriverStrategy(testClass, testName);

if (strategy == SharedDriverStrategy.PER_METHOD) {
quit();
} else if (sharedDriver != null && csr.shouldDeleteCookies(testClass, testName)) {
sharedDriver.manage().deleteAllCookies();
SharedMutator.EffectiveParameters<?> sharedParameters = this.sharedMutator.getEffectiveParameters(testClass, testName, strategy);

SharedWebDriver sharedWebDriver = SharedWebDriverContainer.INSTANCE.getDriver(sharedParameters.getTestClass(), sharedParameters.getTestName(), sharedParameters.getStrategy());
if (sharedWebDriver != null) {
SharedWebDriverContainer.INSTANCE.quit(sharedWebDriver);
}
} else if (csr.shouldDeleteCookies(testClass, testName)) {
SharedMutator.EffectiveParameters<?> sharedParameters = this.sharedMutator.getEffectiveParameters(testClass, testName, strategy);

SharedWebDriver sharedWebDriver = SharedWebDriverContainer.INSTANCE.getDriver(sharedParameters.getTestClass(), sharedParameters.getTestName(), sharedParameters.getStrategy());
if (sharedWebDriver != null) {
sharedWebDriver.getDriver().manage().deleteAllCookies();
}
}

releaseFluent();

}

/**
* Invoked when a test class has finished (whatever the success of failing status)
*
* @param testClass
*/
public static void afterClass(Class<?> testClass) {
List<SharedWebDriver> sharedWebDrivers = SharedWebDriverContainer.INSTANCE.getTestClassDrivers(testClass);
for (SharedWebDriver sharedWebDriver : sharedWebDrivers) {
SharedWebDriverContainer.INSTANCE.quit(sharedWebDriver);
}
}

Expand All @@ -161,8 +166,7 @@ protected void failed() {
/**
* Invoked when a test method has failed (before finished)
*
* @param testName
* Test name
* @param testName Test name
*/
protected void failed(String testName) {
failed(null, getClass(), testName);
Expand All @@ -171,8 +175,7 @@ protected void failed(String testName) {
/**
* Invoked when a test method has failed (before finished)
*
* @param testClass
* Test class
* @param testClass Test class
*/
protected void failed(Class<?> testClass) {
failed(null, testClass, testClass.getName());
Expand All @@ -181,10 +184,8 @@ protected void failed(Class<?> testClass) {
/**
* Invoked when a test method has failed (before finished)
*
* @param testClass
* Test class
* @param testName
* Test name
* @param testClass Test class
* @param testName Test name
*/
protected void failed(Class<?> testClass, String testName) {
failed(null, testClass, testName);
Expand All @@ -193,12 +194,9 @@ protected void failed(Class<?> testClass, String testName) {
/**
* Invoked when a test method has failed (before finished)
*
* @param e
* Throwable thrown by the failing test.
* @param testClass
* Test class
* @param testName
* Test name
* @param e Throwable thrown by the failing test.
* @param testClass Test class
* @param testName Test name
*/
protected void failed(Throwable e, Class<?> testClass, String testName) {
if (isFluentDriverAvailable()) {
Expand All @@ -210,16 +208,4 @@ protected void failed(Throwable e, Class<?> testClass, String testName) {
}
}
}

/**
* Invoked when all methods from the class have been runned.
*/
public static void releaseSharedDriver() {
if (isSharedDriverPerClass) {
sharedDriver.quit();
sharedDriver = null;
isSharedDriverPerClass = false;
}
}

}
Loading

0 comments on commit 1c422ba

Please sign in to comment.