Skip to content

Commit

Permalink
Refactoring the Screenplay code - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Apr 20, 2016
1 parent 1e58704 commit af83a0d
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public <T extends PageObject> T on(Class<T> pageObjectClass) {

@Subscribe public void beginPerformance(ActorBeginsPerformanceEvent performanceEvent) {
try {
if (performanceEvent.getName().equals(actor.getName())) {
webdriverManager.setCurrentDriver(getDriver());
}
// if (performanceEvent.getName().equals(actor.getName())) {
// webdriverManager.setCurrentDriver(getDriver());
// }
} catch(Throwable e) {
logger.warn("Failed to notify begin performance event for actor " + performanceEvent.getName(),e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public void injectDrivers(final WebdriverManager webdriverManager) {
String suffix = "";
for(ManagedWebDriverAnnotatedField webDriverField : webDriverFields) {
String driverRootName = isEmpty(webDriverField.getDriver()) ?
webdriverManager.getCurrentDriverName() : webDriverField.getDriver();
webdriverManager.getCurrentDriverType() : webDriverField.getDriver();

String driverName = driverRootName + suffix;
webDriverField.setValue(testCase, webdriverManager.getWebdriver(driverName));
webDriverField.setValue(testCase, webdriverManager.getWebdriverByName(driverName));

suffix = nextSuffix(driverCount++);
}
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public WebElement ajaxFindElement() {
}

private int getTimeOutInSeconds() {
return (int) ThucydidesWebdriverManager.inThisTestThread().getCurrentImplicitTimeout().in(TimeUnit.SECONDS);
return (int) SerenityWebdriverManager.inThisTestThread().getCurrentImplicitTimeout().in(TimeUnit.SECONDS);
}

private final static List<WebElement> EMPTY_LIST_OF_WEBELEMENTS = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void configure() {
bind(TemplateManager.class).to(FreeMarkerTemplateManager.class).in(Singleton.class);
bind(Configuration.class).to(SystemPropertiesConfiguration.class).in(Singleton.class);
bind(IssueTracking.class).to(SystemPropertiesIssueTracking.class).in(Singleton.class);
bind(WebdriverManager.class).to(ThucydidesWebdriverManager.class).in(Singleton.class);
bind(WebdriverManager.class).to(SerenityWebdriverManager.class).in(Singleton.class);
bind(BatchManager.class).toProvider(BatchManagerProvider.class).in(Singleton.class);
bind(LinkGenerator.class).to(SaucelabsLinkGenerator.class).in(Singleton.class);
// bind(ScreenshotProcessor.class).to(SingleThreadScreenshotProcessor.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void testFinished(final TestOutcome outcome) {


private String getDriverUsedInThisTest() {
return ThucydidesWebDriverSupport.getCurrentDriverName();// webdriverManager.getCurrentDriverName();
return ThucydidesWebDriverSupport.getCurrentDriverName();// webdriverManager.getCurrentDriverType();
}

private boolean currentTestIsABrowserTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.thucydides.core.webdriver;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
Expand All @@ -21,9 +20,9 @@
* @author johnsmart
*
*/
public class ThucydidesWebdriverManager implements WebdriverManager {
public class SerenityWebdriverManager implements WebdriverManager {

private static final ThreadLocal<WebdriverInstances> webdriverInstancesThreadLocal = new ThreadLocal<WebdriverInstances>();
private static final ThreadLocal<WebdriverInstances> webdriverInstancesThreadLocal = new ThreadLocal<>();

private final WebDriverFactory webDriverFactory;

Expand All @@ -32,7 +31,7 @@ public class ThucydidesWebdriverManager implements WebdriverManager {
private final Set<WebDriver> allWebdriverInstances;

@Inject
public ThucydidesWebdriverManager(final WebDriverFactory webDriverFactory, final Configuration configuration) {
public SerenityWebdriverManager(final WebDriverFactory webDriverFactory, final Configuration configuration) {
this.webDriverFactory = webDriverFactory;
this.configuration = configuration;
this.allWebdriverInstances = Collections.synchronizedSet(new HashSet<WebDriver>());
Expand Down Expand Up @@ -80,13 +79,13 @@ public void closeAllCurrentDrivers() {
public void closeAllDrivers() {
synchronized (allWebdriverInstances) {
for(WebDriver driver : allWebdriverInstances) {
closeSafely(driver);
safelyClose(driver);
}
allWebdriverInstances.clear();
}
}

private void closeSafely(WebDriver driver) {
private void safelyClose(WebDriver driver) {
try {
driver.close();
driver.quit();
Expand All @@ -98,25 +97,22 @@ public void resetDriver() {
}

public WebDriver getWebdriver() {
return currentDriver.or(getThreadLocalWebDriver(configuration, webDriverFactory, inThisTestThread().getCurrentDriverName()));
return instantiatedThreadLocalWebDriver(configuration, webDriverFactory, inThisTestThread().getCurrentDriverName());
}

@Override
public WebdriverContext inContext(String context) {
return new WebdriverContext(this, context);
}

private Optional<WebDriver> currentDriver = Optional.absent();

@Override
public void setCurrentDriver(WebDriver driver) {
currentDriver = Optional.fromNullable(driver);
inThisTestThread().setCurrentDriverTo(driver);
}

@Override
public void clearCurrentDriver() {
currentDriver = Optional.absent();

}

@Override
Expand All @@ -125,13 +121,14 @@ public void registerDriver(WebDriver driver) {
inThisTestThread().setCurrentDriverTo(driver);
}

public String getCurrentDriverName() {
return inThisTestThread().getCurrentDriverName();
public String getCurrentDriverType() {
return inThisTestThread().getCurrentDriverType();
}

public SessionId getSessionId() {
WebDriver driver = getThreadLocalWebDriver(configuration, webDriverFactory,
inThisTestThread().getCurrentDriverName());

WebDriver driver = inThisTestThread().getCurrentDriver();

if(driver instanceof WebDriverFacade){
driver = ((WebDriverFacade) driver).getDriverInstance();
}
Expand All @@ -142,23 +139,28 @@ public SessionId getSessionId() {
}

public WebDriver getWebdriver(final String driverName) {
WebDriver activeDriver;
if (StringUtils.isEmpty(driverName)) {
activeDriver = getWebdriver();
} else {
activeDriver = getThreadLocalWebDriver(configuration, webDriverFactory, driverName);
}

String name = (StringUtils.isEmpty(driverName)) ? inThisTestThread().getCurrentDriverName() : driverName;

WebDriver activeDriver = instantiatedThreadLocalWebDriver(configuration, webDriverFactory, name);

registerDriverInGlobalDrivers(activeDriver);

return activeDriver;
}

public WebDriver getWebdriverByName(String name) {
return getWebdriver(":" + name);
}


private void registerDriverInGlobalDrivers(WebDriver activeDriver) {
allWebdriverInstances.add(activeDriver);
}

private static WebDriver getThreadLocalWebDriver(final Configuration configuration,
final WebDriverFactory webDriverFactory,
final String driver) {
private static WebDriver instantiatedThreadLocalWebDriver(final Configuration configuration,
final WebDriverFactory webDriverFactory,
final String driver) {


if (!inThisTestThread().driverIsRegisteredFor(driver)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static SessionId getSessionId() {
}

public static String getCurrentDriverName() {
return getWebdriverManager().getCurrentDriverName();
return getWebdriverManager().getCurrentDriverType();
}

public static boolean isDriverInstantiated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ public WebdriverContext(WebdriverManager webdriverManager, String context) {
}

public WebDriver getWebdriver() {
String driverType = webdriverManager.getCurrentDriverName();
String driverType = webdriverManager.getCurrentDriverType();
String driverName = driverType + ":" + context;
return webdriverManager.getWebdriver(driverName);
}

WebDriver getWebdriver(String driverType) {
String driverName = driverType + ":" + context;
return webdriverManager.getWebdriver(driverName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public WebDriver getCurrentDriver() {
}

public String getCurrentDriverName() {
return currentDriver == null ? "" : currentDriver;
}

public String getCurrentDriverType() {
if (getCurrentDriver() == null) {
return "";
}
Expand Down Expand Up @@ -133,7 +137,6 @@ private String driverNameFor(WebDriver driver) {
throw new IllegalStateException("No matching driver found in this thread");
}


public final class InstanceRegistration {
private final String driverName;

Expand All @@ -159,4 +162,5 @@ private String normalized(String name) {
return name.toLowerCase();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public interface WebdriverManager {
WebdriverContext inContext(String context);

WebDriver getWebdriver(String driver);
WebDriver getWebdriverByName(String actorName);

String getCurrentDriverName();
String getCurrentDriverType();

SessionId getSessionId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import net.thucydides.core.steps.StepFailure
import net.thucydides.core.util.EnvironmentVariables
import net.thucydides.core.util.MockEnvironmentVariables
import net.thucydides.core.webdriver.StaticTestSite
import net.thucydides.core.webdriver.ThucydidesWebdriverManager
import net.thucydides.core.webdriver.SerenityWebdriverManager
import net.thucydides.core.webdriver.exceptions.ElementShouldBeDisabledException
import net.thucydides.core.webdriver.exceptions.ElementShouldBeEnabledException
import net.thucydides.core.webdriver.exceptions.ElementShouldBeInvisibleException
Expand Down Expand Up @@ -53,7 +53,7 @@ class WhenManagingWebdriverTimeouts extends Specification {
}

def cleanup() {
ThucydidesWebdriverManager.inThisTestThread().closeAllDrivers();
SerenityWebdriverManager.inThisTestThread().closeAllDrivers();
}

def StaticSitePage openTestPageUsing(String browser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import net.thucydides.core.util.EnvironmentVariables
import net.thucydides.core.util.MockEnvironmentVariables
import net.thucydides.core.webdriver.SupportedWebDriver
import net.thucydides.core.webdriver.SystemPropertiesConfiguration
import net.thucydides.core.webdriver.ThucydidesWebdriverManager
import net.thucydides.core.webdriver.TimeoutStack
import net.thucydides.core.webdriver.WebDriverFacade
import net.thucydides.core.webdriver.SerenityWebdriverManager
import net.thucydides.core.webdriver.WebDriverFactory
import net.thucydides.core.webdriver.WebdriverInstanceFactory
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.openqa.selenium.WebDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.remote.RemoteWebDriver
import org.openqa.selenium.remote.SessionId
import spock.lang.Specification
Expand Down Expand Up @@ -52,7 +47,7 @@ class WhenUsingWebDriverFacade extends Specification {
given:
when(remote.getSessionId()).thenReturn(session)
when(webDriverFactory.newInstanceOf(any(SupportedWebDriver.class))).thenReturn(remote)
def manager = new ThucydidesWebdriverManager(webDriverFactory, configuration);
def manager = new SerenityWebdriverManager(webDriverFactory, configuration);
when:
manager.registerDriver(remote)
manager.getSessionId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class WhenOpeningAndClosingBrowserSessions extends Specification {
remote.getCapabilities() >> capabilities
environmentVariables.setProperty("webdriver.driver","htmlunit")
webDriverFactory = new WebDriverFactory(webdriverInstanceFactory, environmentVariables)
webdriverManager = new ThucydidesWebdriverManager(webDriverFactory, new SystemPropertiesConfiguration(environmentVariables));
webdriverManager = new SerenityWebdriverManager(webDriverFactory, new SystemPropertiesConfiguration(environmentVariables));
StepEventBus.eventBus.clear()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.thucydides.core.webdriver.Configuration;
import net.thucydides.core.webdriver.SystemPropertiesConfiguration;
import net.thucydides.core.webdriver.ThucydidesWebDriverSupport;
import net.thucydides.core.webdriver.ThucydidesWebdriverManager;
import org.junit.*;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class StaticTestSite {

private WebDriverFactory factory;
private ThucydidesWebdriverManager webdriverManager;
private SerenityWebdriverManager webdriverManager;
private EnvironmentVariables environmentVariables;

public StaticTestSite() {
Expand All @@ -20,7 +20,7 @@ public StaticTestSite() {
public StaticTestSite(EnvironmentVariables environmentVariables) {
this.environmentVariables = environmentVariables;
factory = new WebDriverFactory(environmentVariables);
webdriverManager = new ThucydidesWebdriverManager(factory, new SystemPropertiesConfiguration(environmentVariables));
webdriverManager = new SerenityWebdriverManager(factory, new SystemPropertiesConfiguration(environmentVariables));
}

private String homepage = "index.html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected FirefoxProfile createNewFirefoxProfile() {

configuration = new SystemPropertiesConfiguration(environmentVariables);

webdriverManager = new ThucydidesWebdriverManager(factory, configuration);
webdriverManager = new SerenityWebdriverManager(factory, configuration);
webdriverManager.closeAllCurrentDrivers();
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public void the_default_output_directory_can_be_overrided_via_a_system_property(

@Test
public void should_close_drivers_in_current_thread() {
ThucydidesWebdriverManager aWebdriverManager = new ThucydidesWebdriverManager(factory, configuration);
SerenityWebdriverManager aWebdriverManager = new SerenityWebdriverManager(factory, configuration);
aWebdriverManager.getWebdriver("firefox");
aWebdriverManager.getWebdriver("htmlunit");
aWebdriverManager.getWebdriver("chrome");
Expand All @@ -131,7 +131,7 @@ public void should_close_drivers_in_current_thread() {

@Test
public void should_close_individual_driver_in_current_thread() {
ThucydidesWebdriverManager aWebdriverManager = new ThucydidesWebdriverManager(factory, configuration);
SerenityWebdriverManager aWebdriverManager = new SerenityWebdriverManager(factory, configuration);
aWebdriverManager.getWebdriver("firefox");
aWebdriverManager.getWebdriver("htmlunit");
aWebdriverManager.getWebdriver("chrome");
Expand All @@ -145,7 +145,7 @@ public void should_close_individual_driver_in_current_thread() {

@Test
public void should_close_drivers_in_all_threads() {
ThucydidesWebdriverManager aWebdriverManager = new ThucydidesWebdriverManager(factory, configuration);
SerenityWebdriverManager aWebdriverManager = new SerenityWebdriverManager(factory, configuration);
aWebdriverManager.getWebdriver("firefox");
aWebdriverManager.getWebdriver("htmlunit");
aWebdriverManager.getWebdriver("chrome");
Expand All @@ -157,7 +157,7 @@ public void should_close_drivers_in_all_threads() {

@Test
public void should_close_drivers_in_all_threads_after_driver_resets() {
ThucydidesWebdriverManager aWebdriverManager = new ThucydidesWebdriverManager(factory, configuration);
SerenityWebdriverManager aWebdriverManager = new SerenityWebdriverManager(factory, configuration);
aWebdriverManager.getWebdriver("firefox");
aWebdriverManager.getWebdriver("htmlunit");
aWebdriverManager.getWebdriver("chrome");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void initWendriverManager() throws IllegalAccessException, Instantiation
environmentVariables.setProperty("webdriver.driver", "phantomjs");
factory = new WebDriverFactory(webdriverInstanceFactory, environmentVariables);

webdriverManager = new ThucydidesWebdriverManager(factory, new SystemPropertiesConfiguration(environmentVariables));
webdriverManager = new SerenityWebdriverManager(factory, new SystemPropertiesConfiguration(environmentVariables));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public SerenityRunner(final Class<?> klass,
final Configuration configuration,
final BatchManager batchManager) throws InitializationError {
this(klass,
new ThucydidesWebdriverManager(webDriverFactory, configuration),
new SerenityWebdriverManager(webDriverFactory, configuration),
configuration,
batchManager
);
Expand Down
Loading

0 comments on commit af83a0d

Please sign in to comment.