-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor FluentAdapter for TestNG and JUnit Tests to share more code
- Loading branch information
Showing
28 changed files
with
235 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 6 additions & 89 deletions
95
fluentlenium-core/src/main/java/org/fluentlenium/adapter/FluentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,42 @@ | ||
package org.fluentlenium.adapter; | ||
|
||
import org.fluentlenium.adapter.util.ShutdownHook; | ||
import org.fluentlenium.core.FluentAdapter; | ||
import org.fluentlenium.core.FluentPage; | ||
import org.junit.AfterClass; | ||
import org.junit.Rule; | ||
import org.junit.rules.TestName; | ||
import org.junit.rules.TestRule; | ||
import org.junit.rules.TestWatcher; | ||
import org.junit.runner.Description; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
import static org.fluentlenium.adapter.util.SharedDriverHelper.*; | ||
|
||
/** | ||
* All Junit Test should extends this class. It provides default parameters. | ||
*/ | ||
public abstract class FluentTest extends FluentAdapter { | ||
private static WebDriver sharedDriver; | ||
private static boolean isSharedDriverPerClass; | ||
|
||
public abstract class FluentTest extends FluentTestRunnerAdapter { | ||
@Rule | ||
public TestName name = new TestName(); | ||
@Rule | ||
public TestRule watchman = new TestWatcher() { | ||
@Override | ||
public void starting(Description description) { | ||
super.starting(description); | ||
//TODO Refactor | ||
if (isSharedDriverOnce(description.getTestClass())) { | ||
synchronized (this) { | ||
if (sharedDriver == null) { | ||
initFluentFromDefaultDriver(); | ||
sharedDriver = getDriver(); | ||
killTheBrowserOnShutdown(); | ||
} else { | ||
initFluentWithExistingDriver(); | ||
} | ||
} | ||
} else if (isSharedDriverPerClass(description.getTestClass())) { | ||
synchronized (this) { | ||
if (!isSharedDriverPerClass) { | ||
initFluentFromDefaultDriver(); | ||
sharedDriver = getDriver(); | ||
isSharedDriverPerClass = true; | ||
} else { | ||
initFluentWithExistingDriver(); | ||
} | ||
} | ||
} else { | ||
initFluentFromDefaultDriver(); | ||
} | ||
|
||
initTest(); | ||
setDefaultConfig(); | ||
FluentTest.this.starting(description.getTestClass(), description.getDisplayName()); | ||
} | ||
|
||
|
||
@Override | ||
public void finished(Description description) { | ||
super.finished(description); | ||
if (isSharedDriverPerMethod(description.getTestClass()) || | ||
isDefaultSharedDriver(description.getTestClass())) { | ||
quit(); | ||
} else if (isDeleteCookies(description.getTestClass())) { | ||
if (sharedDriver != null) { | ||
sharedDriver.manage().deleteAllCookies(); | ||
} | ||
} | ||
FluentTest.this.finished(description.getTestClass(), description.getDisplayName()); | ||
} | ||
|
||
@Override | ||
public void failed(Throwable e, Description description) { | ||
if (screenshotMode == TriggerMode.ON_FAIL) { | ||
takeScreenShot(description.getTestClass().getSimpleName() + "_" + | ||
description.getMethodName() + ".png"); | ||
} | ||
if (htmlDumpMode == TriggerMode.ON_FAIL) { | ||
takeHtmlDump(description.getTestClass().getSimpleName() + "_" | ||
+ description.getMethodName() + ".html"); | ||
} | ||
super.failed(e, description); | ||
FluentTest.this.failed(e, description.getTestClass(), description.getDisplayName()); | ||
} | ||
|
||
}; | ||
|
||
private void killTheBrowserOnShutdown() { | ||
Runtime.getRuntime().addShutdownHook(new ShutdownHook("fluentlenium", this)); | ||
} | ||
|
||
private void initFluentWithExistingDriver() { | ||
initFluent(sharedDriver).withDefaultUrl(getDefaultBaseUrl()); | ||
} | ||
|
||
private void initFluentFromDefaultDriver() { | ||
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl()); | ||
} | ||
|
||
|
||
public FluentTest() { | ||
super(); | ||
} | ||
|
||
|
||
@AfterClass | ||
public static void afterClass() { | ||
if (isSharedDriverPerClass) { | ||
sharedDriver.quit(); | ||
sharedDriver = null; | ||
isSharedDriverPerClass = false; | ||
} | ||
FluentTest.releaseSharedDriver(); | ||
} | ||
|
||
/** | ||
* Override this method to change the default time to wait for a page to be loaded | ||
*/ | ||
public void setDefaultConfig() { | ||
} | ||
|
||
public static void assertAt(FluentPage fluent) { | ||
fluent.isAt(); | ||
} | ||
|
||
|
||
} |
112 changes: 112 additions & 0 deletions
112
fluentlenium-core/src/main/java/org/fluentlenium/adapter/FluentTestRunnerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.fluentlenium.adapter; | ||
|
||
|
||
import org.fluentlenium.adapter.util.CookieStrategyReader; | ||
import org.fluentlenium.adapter.util.DefaultCookieStrategyReader; | ||
import org.fluentlenium.adapter.util.DefaultSharedDriverStrategyReader; | ||
import org.fluentlenium.adapter.util.SharedDriverStrategyReader; | ||
import org.fluentlenium.adapter.util.SharedDriverStrategy; | ||
import org.fluentlenium.adapter.util.ShutdownHook; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
/** | ||
* 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; | ||
|
||
public FluentTestRunnerAdapter() { | ||
this(new DefaultSharedDriverStrategyReader(), new DefaultCookieStrategyReader()); | ||
} | ||
|
||
public FluentTestRunnerAdapter(SharedDriverStrategyReader sharedDriverExtractor, CookieStrategyReader cookieExtractor) { | ||
this.sdsr = sharedDriverExtractor; | ||
this.csr = cookieExtractor; | ||
} | ||
|
||
/** | ||
* Invoked when a test method is starting. | ||
* | ||
* @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 (this) { | ||
if (sharedDriver == null) { | ||
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl()); | ||
sharedDriver = getDriver(); | ||
Runtime.getRuntime().addShutdownHook(new ShutdownHook("fluentlenium", this)); | ||
} else { | ||
initFluent(sharedDriver).withDefaultUrl(getDefaultBaseUrl()); | ||
} | ||
} | ||
} else if (strategy == SharedDriverStrategy.PER_CLASS) { | ||
synchronized (this) { | ||
if (!isSharedDriverPerClass) { | ||
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl()); | ||
sharedDriver = getDriver(); | ||
isSharedDriverPerClass = true; | ||
} else { | ||
initFluent(sharedDriver).withDefaultUrl(getDefaultBaseUrl()); | ||
} | ||
} | ||
} else { | ||
initFluent(getDefaultDriver()).withDefaultUrl(getDefaultBaseUrl()); | ||
} | ||
|
||
init(); | ||
} | ||
|
||
/** | ||
* Invoked when a test method has finished (whatever the success of failing status) | ||
* | ||
* @param testClass Test class | ||
* @param testName Test name | ||
*/ | ||
protected void finished(Class<?> testClass, String testName) { | ||
close(); | ||
SharedDriverStrategy strategy = sdsr.getSharedDriverStrategy(testClass, testName); | ||
if (strategy == SharedDriverStrategy.PER_METHOD) { | ||
quit(); | ||
} else if (sharedDriver != null && csr.shouldDeleteCookies(testClass, testName)) { | ||
sharedDriver.manage().deleteAllCookies(); | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
protected void failed(Throwable e, Class<?> testClass, String testName) { | ||
if (screenshotMode == TriggerMode.ON_FAIL) { | ||
takeScreenShot(testClass.getSimpleName() + "_" + | ||
testName + ".png"); | ||
} | ||
if (htmlDumpMode == TriggerMode.ON_FAIL) { | ||
takeHtmlDump(testClass.getSimpleName() + "_" | ||
+ testName + ".html"); | ||
} | ||
} | ||
|
||
/** | ||
* Invoked when all methods from the class have been runned. | ||
*/ | ||
public static void releaseSharedDriver() { | ||
if (isSharedDriverPerClass) { | ||
sharedDriver.quit(); | ||
sharedDriver = null; | ||
isSharedDriverPerClass = false; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
fluentlenium-core/src/main/java/org/fluentlenium/adapter/util/CookieStrategyReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.fluentlenium.adapter.util; | ||
|
||
|
||
public interface CookieStrategyReader { | ||
boolean shouldDeleteCookies(Class<?> clazz, String testName); | ||
} |
9 changes: 9 additions & 0 deletions
9
...tlenium-core/src/main/java/org/fluentlenium/adapter/util/DefaultCookieStrategyReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.fluentlenium.adapter.util; | ||
|
||
public class DefaultCookieStrategyReader implements CookieStrategyReader { | ||
@Override | ||
public boolean shouldDeleteCookies(Class<?> clazz, String testName) { | ||
SharedDriver sharedBrowser = clazz.getAnnotation(SharedDriver.class); | ||
return (sharedBrowser != null && sharedBrowser.deleteCookies()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...m-core/src/main/java/org/fluentlenium/adapter/util/DefaultSharedDriverStrategyReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.fluentlenium.adapter.util; | ||
|
||
public class DefaultSharedDriverStrategyReader implements SharedDriverStrategyReader { | ||
@Override | ||
public SharedDriverStrategy getSharedDriverStrategy(Class<?> clazz, String testName) { | ||
SharedDriver sharedDriver = clazz.getAnnotation(SharedDriver.class); | ||
|
||
if (sharedDriver == null) { | ||
return SharedDriverStrategy.PER_METHOD; | ||
} | ||
|
||
if (sharedDriver.type() == SharedDriver.SharedType.ONCE) { | ||
return SharedDriverStrategy.ONCE; | ||
} | ||
|
||
if (sharedDriver.type() == SharedDriver.SharedType.PER_CLASS) { | ||
return SharedDriverStrategy.PER_CLASS; | ||
} | ||
|
||
return SharedDriverStrategy.PER_METHOD; | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
fluentlenium-core/src/main/java/org/fluentlenium/adapter/util/SharedDriverHelper.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.