Skip to content

Commit

Permalink
Minor enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Apr 25, 2019
1 parent ae2ace4 commit 581c65b
Show file tree
Hide file tree
Showing 30 changed files with 304 additions and 115 deletions.
17 changes: 17 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,22 @@ subprojects {
bintrayRepository = 'maven'
bintrayPackage = 'serenity-core'
projectDescription = 'Serenity core libraries'
os = "windows"
if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
os = "macos"
}
if (org.gradle.internal.os.OperatingSystem.current().isLinux()) {
os = "linxu"
}
if (!project.hasProperty("bintrayUsername")) {
bintrayUsername = 'wakaleo'
}
if (!project.hasProperty("bintrayApiKey")) {
bintrayApiKey = ''
}

chromeDriverBinary = project.file("../src/test/resources/drivers/${os}/chromedriver").absolutePath
geckoDriverBinary = project.file("../src/test/resources/drivers/${os}/geckodriver").absolutePath
}
group = 'net.serenity-bdd'
version = rootProject.version
Expand All @@ -143,6 +153,8 @@ subprojects {
}

task integrationTests(type: Test) {
systemProperty "webdriver.chrome.driver", chromeDriverBinary
systemProperty "webdriver.gecko.driver", geckoDriverBinary
exclude '**/*$*'
exclude '**/samples/**'
exclude '**/*Sample*'
Expand Down Expand Up @@ -466,3 +478,8 @@ Changelog of Git Changelog Gradle plugin.
"""
}
}

gradle.taskGraph.whenReady { graph ->
println "SETTING CHROME DRIVER TO src/test/resources/drivers/macos/chromedriver"
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/macos/chromedriver")
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public PageObject(final WebDriver driver, final EnvironmentVariables environment
this();
this.environmentVariables = environmentVariables;
setDriver(driver);
setupPageUrls();
}

protected void setDriver(WebDriver driver, long timeout) {
Expand Down Expand Up @@ -243,7 +244,7 @@ private boolean isDefinedRemoteUrl() {
}

private void setupPageUrls() {
setPageUrls(new PageUrls(this));
setPageUrls(new PageUrls(this, environmentVariables));
}

/**
Expand Down Expand Up @@ -638,13 +639,29 @@ public long implicitTimoutMilliseconds() {
return getImplicitWaitTimeout().toMillis();
}

public String updateUrlWithBaseUrlIfDefined(final String startingUrl) {
public String updateUrlWithBaseUrlIfDefined(String startingUrl) {

if (pageUrls.getDeclaredDefaultUrl().isPresent()) {
startingUrl = pageUrls.addDefaultUrlTo(startingUrl);
}

String baseUrl = pageUrls.getSystemBaseUrl();
if ((baseUrl != null) && (!StringUtils.isEmpty(baseUrl))) {
return replaceHost(startingUrl, baseUrl);
} else {
return startingUrl;
if ((baseUrl != null) && (!StringUtils.isEmpty(baseUrl)) && isFullUrl(startingUrl)) {
return replaceHost(startingUrl, baseUrl);//pageUrls.addBaseUrlTo(startingUrl);// replaceHost(startingUrl, baseUrl);
}
// else if (pageUrls.getDeclaredDefaultUrl().isPresent()) {
// return pageUrls.addDefaultUrlTo(startingUrl);
// }

return startingUrl;
}

private boolean isFullUrl(String startingUrl) {
try {
new URL(startingUrl);
return true;
} catch (MalformedURLException e) {
return false;
}
}

Expand Down Expand Up @@ -705,6 +722,25 @@ private void open(final OpenMode openMode, final String... parameterValues) {
LOGGER.debug("Page opened");
}

public final OpenWithParams open(final String urlTemplateName) {
return new OpenWithParams(this, urlTemplateName);
}

public static class OpenWithParams {

private PageObject pageObject;
private String urlTemplateName;

public OpenWithParams(PageObject pageObject, String urlTemplateName) {
this.pageObject = pageObject;
this.urlTemplateName = urlTemplateName;
}

public void withParameters(String... parameters) {
pageObject.open(urlTemplateName, parameters);
}
}

public final void open(final String urlTemplateName,
final String[] parameterValues) {
open(OpenMode.CHECK_URL_PATTERNS, urlTemplateName, parameterValues);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.serenitybdd.core.pages;

import cucumber.runtime.Env;
import net.serenitybdd.core.environment.ConfiguredEnvironment;
import net.serenitybdd.core.environment.EnvironmentSpecificConfiguration;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.NamedUrl;
import net.thucydides.core.annotations.NamedUrls;
import net.thucydides.core.configuration.SystemPropertiesConfiguration;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.webdriver.Configuration;
Expand Down Expand Up @@ -37,7 +39,12 @@ public PageUrls(final Object pageObject, final Configuration configuration) {
}

public PageUrls(final Object pageObject) {
this(pageObject, ConfiguredEnvironment.getConfiguration());
this(pageObject, Injectors.getInjector().getInstance(EnvironmentVariables.class));
}

public PageUrls(final Object pageObject, EnvironmentVariables environmentVariables) {
this(pageObject,
ConfiguredEnvironment.getConfiguration().withEnvironmentVariables(environmentVariables));
}

public String getStartingUrl() {
Expand All @@ -51,7 +58,7 @@ public String getStartingUrl() {
return verified(url, pageObject);
}

private Optional<String> getDeclaredDefaultUrl() {
public Optional<String> getDeclaredDefaultUrl() {
DefaultUrl urlAnnotation = pageObject.getClass().getAnnotation(DefaultUrl.class);
if (urlAnnotation != null) {
return Optional.ofNullable(urlAnnotation.value());
Expand Down Expand Up @@ -174,7 +181,13 @@ private String urlWithParametersSubstituted(final String template,
return addBaseUrlTo(url);
}

private String addBaseUrlTo(final String url) {


public String addDefaultUrlTo(final String url) {
return prefixedWithDefaultUrl(url);
}

public String addBaseUrlTo(final String url) {
if (isANamedUrl(url)) {
return namedUrlFrom(url);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.thucydides.core.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Options that will be passed to a driver specified by the @WithDriver annotation
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DriverOptions {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.serenitybdd.core.environment.*;
import net.thucydides.core.configuration.*;
import net.thucydides.core.webdriver.*;
import org.apache.commons.lang3.StringUtils;
import org.junit.runner.*;
import org.openqa.selenium.*;

Expand Down Expand Up @@ -58,8 +59,10 @@ public void injectDrivers(final WebDriver defaultDriver, final WebdriverManager
String driverName = driverRootName + suffix;
String driverOptions = webDriverField.getOptions();

ThucydidesWebDriverSupport.useDefaultDriver(driverName);
ThucydidesWebDriverSupport.useDriverOptions(driverOptions);
if (!ThucydidesWebDriverSupport.getDefaultDriverType().isPresent()) {
ThucydidesWebDriverSupport.useDefaultDriver(driverName);
ThucydidesWebDriverSupport.useDriverOptions(driverOptions);
}

WebDriver driver = (isEmpty(driverName)) ? defaultDriver : requestedDriverFrom(webdriverManager, webDriverField.getName(), driverName, driverOptions);
webDriverField.setValue(testCase, driver);
Expand All @@ -76,7 +79,7 @@ private WebDriver requestedDriverFrom(WebdriverManager webdriverManager, String
}

private String configuredDriverType() {
if (ThucydidesWebDriverSupport.isInitialised()) {
if (ThucydidesWebDriverSupport.isInitialised() && (StringUtils.isNotEmpty(ThucydidesWebDriverSupport.getCurrentDriverName()))) {
return ThucydidesWebDriverSupport.getCurrentDriverName();
}
return configuration.getDriverType().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public SupportedWebDriver getDriverType() {
Optional<String> driverDefinedInEnvironment = Optional.ofNullable(WebDriverFactory.getDriverFrom(getEnvironmentVariables()));
Optional<String> driverDefinedInTest = ThucydidesWebDriverSupport.getDefaultDriverType();

String driverType = driverDefinedInTest.orElse(driverDefinedInEnvironment.orElse(DEFAULT_WEBDRIVER_DRIVER));
String driverType = driverTypeOf(driverDefinedInTest.orElse(driverDefinedInEnvironment.orElse(DEFAULT_WEBDRIVER_DRIVER)));

return lookupSupportedDriverTypeFor(driverType);
}

private String driverTypeOf(String driverName) {
return (driverName.contains(":") ? driverName.substring(0, driverName.indexOf(":")) : driverName);
}

/**
* Transform a driver type into the SupportedWebDriver enum. Driver type can
* be any case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public void testSuiteFinished() {
closeDarkroom();
clearStorywideTagsAndIssues();
ThucydidesWebDriverSupport.clearStepLibraries();
ThucydidesWebDriverSupport.clearDefaultDriver();

if (this.currentTestIsABrowserTest()) {
this.closeBrowsers.forTestSuite(this.testSuite).closeIfConfiguredForANew(RestartBrowserForEach.FEATURE);
Expand Down Expand Up @@ -484,6 +485,7 @@ public void testFinished(final TestOutcome outcome) {
}

currentStepStack.clear();
ThucydidesWebDriverSupport.clearDefaultDriver();
}

private void testAndTopLevelStepsShouldBeIgnored() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import java.util.*;
import java.util.concurrent.TimeUnit;

import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_CREATION_RETRY_CAUSES;
import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_CREATION_RETRY_MAX_TIME;
import static net.thucydides.core.ThucydidesSystemProperty.*;
import static net.thucydides.core.webdriver.DriverStrategySelector.inEnvironment;

/**
Expand Down Expand Up @@ -72,6 +71,22 @@ public WebDriverFactory(EnvironmentVariables environmentVariables,
this.closeBrowser = WebDriverInjectors.getInjector().getInstance(CloseBrowser.class);
}

public WebDriverFactory(EnvironmentVariables environmentVariables,
FixtureProviderService fixtureProviderService,
SaucelabsRemoteDriverCapabilities saucelabsRemoteDriverCapabilities,
TimeoutStack timeoutStack,
CloseBrowser closeBrowser) {
this.environmentVariables = environmentVariables;
this.fixtureProviderService = fixtureProviderService;
this.sauceRemoteDriverCapabilities = saucelabsRemoteDriverCapabilities;
this.timeoutStack = timeoutStack;
this.closeBrowser = closeBrowser;
}

public WebDriverFactory withEnvironmentVariables(EnvironmentVariables environmentVariables) {
return new WebDriverFactory(environmentVariables, fixtureProviderService, sauceRemoteDriverCapabilities, timeoutStack, closeBrowser);
}

/**
* Create a new WebDriver instance of a given type.
*/
Expand Down Expand Up @@ -120,7 +135,8 @@ private Map<SupportedWebDriver, DriverProvider> driverProviders() {
* with each other.
*/
protected synchronized WebDriver newWebdriverInstance(final Class<? extends WebDriver> driverClass) {
return newWebdriverInstance(driverClass, "");
String driverOptions = DRIVER_OPTIONS.from(environmentVariables,"");
return newWebdriverInstance(driverClass, driverOptions);
}

private synchronized WebDriver newWebdriverInstance(final Class<? extends WebDriver> driverClass, String options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.thucydides.core.steps.StepEventBus
import net.thucydides.core.webdriver.SerenityWebdriverManager
import net.thucydides.core.webdriver.integration.PageWithFindBys
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities
import spock.lang.Shared
import spock.lang.Specification
Expand All @@ -29,7 +30,13 @@ class WhenLocatingWebElementsUsingEnhancedFindBys extends Specification {

def setup() {
StepEventBus.eventBus.clear()
driver = chromeService.newDriver(DesiredCapabilities.chrome())

def desiredCapabilities = DesiredCapabilities.chrome();
def chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

driver = chromeService.newDriver(desiredCapabilities);
}

def cleanup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ class WhenManagingWebdriverTimeouts extends Specification {
}

WebDriver newDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
def desiredCapabilities = DesiredCapabilities.chrome();
def chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome()
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

driver = driverService.newDriver(desiredCapabilities);
return driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.apache.commons.io.FileUtils
import org.openqa.selenium.Dimension
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import spock.lang.Ignore
import spock.lang.Specification

Expand All @@ -25,13 +26,13 @@ class WhenAPhotographerTakesScreenshots extends Specification {

def "when a photographer takes a screenshot the photographer returns the future path of the screenshot"() {
given:
def photographer = new Photographer(darkroom);
def photographer = new Photographer(darkroom)
when:
ScreenshotPhoto photo = photographer.takesAScreenshot()
.with(driver)
.andSaveToDirectory(screenshotDirectory);
then:
darkroom.waitUntilClose();
darkroom.waitUntilClose()
photo.getPathToScreenshot().startsWith(screenshotDirectory)
}

Expand Down Expand Up @@ -116,7 +117,9 @@ class WhenAPhotographerTakesScreenshots extends Specification {

def setup() {
screenshotDirectory = Files.createTempDirectory("screenshots");//Files.createDirectories(Paths.get("./build/screenshots"));// Files.createTempDirectory("screenshots")
driver = new ChromeDriver()
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
driver = new ChromeDriver(chromeOptions)
driver.get(siteFromUrlAt("/static-site/unchanging-page.html"))
startTime = System.currentTimeMillis()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ class WhenInjectingWebdriverInstancesIntoATestCase extends Specification {


static class WithMultipleDriversOfDifferentTypeWithADefaultValueLast {
@Managed(driver = "chrome") driver1;
@Managed driver2;
@Managed driver3;
@Managed driver1;
@Managed(driver = "chrome") driver2;
@Managed(driver = "firefox") driver3;
@Managed driver4;
}

def "should inject a different driver for each @Managed field with a mixture of types and defaults with the default last"() {
Expand All @@ -139,11 +140,13 @@ class WhenInjectingWebdriverInstancesIntoATestCase extends Specification {
when:
TestCaseAnnotations.forTestCase(testCase).injectDrivers(webdriverManager)
then:
testCase.driver1 && testCase.driver1.driverClass.name.contains("Chrome")
testCase.driver1 && testCase.driver1.driverClass.name.contains("Firefox")
and:
testCase.driver2 && testCase.driver2.driverClass.name.contains("Firefox")
testCase.driver2 && testCase.driver2.driverClass.name.contains("Chrome")
and:
testCase.driver3 && testCase.driver3.driverClass.name.contains("Firefox")
and:
testCase.driver4 && testCase.driver4.driverClass.name.contains("Firefox")
and:
testCase.driver3 != testCase.driver2
}
Expand Down
Loading

0 comments on commit 581c65b

Please sign in to comment.