Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class Configuration {
private static final Map<String,PageData> PAGE_DATA = new ConcurrentHashMap<>();

private static final String ENV_REPLACE_STRING = "{env}";
private static String env = null;

private static Properties appProps = new Properties();

Expand All @@ -53,6 +52,42 @@ public class Configuration {
private Configuration() {
}

/**
* Set the test environment and os values, as these cannot change throughout the run of the tests.
*/
static {
initializeEnvironment();
initializeOS();
}

/**
* Initializes the test environment used to retrieve environment-specific info during this test execution.
* If no environment is set, a warning message is logged and a default value of "localhost" is set.
*/
protected static void initializeEnvironment() {
var env = System.getProperty("env");
if (env == null) {
env = "localhost";
String warningMessage = "localhost env being used by default. " +
Configuration.configurationNotFoundErrorMessage("env");
log.warn(warningMessage);
}
appProps.setProperty("env", env);
}

/**
* Initializes the operating system value if passed (for example passing it to Saucelabs and we
* don' want it to be this os.) If it's not passed, we detect it and set it. OS does not change
* during test execution.
*/
private static void initializeOS() {
var operatingSystem = Configuration.toString("os");
if (operatingSystem == null) {
operatingSystem = detectOperatingSystem();
appProps.setProperty("os", operatingSystem);
}
}

/**
* Returns the configuration value for the given configuration property and the given environment from
* the ConfigurationData class.
Expand Down Expand Up @@ -242,33 +277,11 @@ public static void update(String property, long value) {

/**
* Returns the system environment.
* If no environment is set, a warning message is logged and a default value of "localhost" is set.
*
* @return String text of system env info
*/
public static String environment() {
if (env == null) {
env = System.getProperty("env");
if (env == null) {
env = "localhost";
String warningMessage = "localhost env being used by default. " +
Configuration.configurationNotFoundErrorMessage("env");
log.warn(warningMessage);
}
}
return env;
}

/**
* Setter intended only for unit testing. Sets the stored value and also the System Property.
* @param env String env to set, null to clear
*/
protected static void environment(String env) {
Configuration.env = env;
if (env == null)
System.clearProperty("env");
else
System.setProperty("env", env);
return appProps.getProperty("env");
}

/**
Expand Down Expand Up @@ -425,6 +438,15 @@ public static String accountInformation(String account, String key) {
return data;
}

/**
* Returns an element's list of locators if it exists in the page object yaml file. If we have
* already retrieved the item once, it is stored in the PAGE_DATA map under the name of the page
* for faster recall.
*
* @param elementName String the name of the element in the page object under the 'elements' section
* @param pageName String the page object to check
* @return Map&lt;String, String&gt; the locators for an element
*/
public static Map <String,String> getElement(String elementName, String pageName) {
return PAGE_DATA.computeIfAbsent(pageName, Configuration::loadPageData).getElement(elementName);
}
Expand Down Expand Up @@ -472,10 +494,20 @@ public static String getTestdataValue(String testdataObjectName, String testdata
return data;
}

/**
* Returns a list of page objects that are included in the passed page object.
* @param pageName String the page object to check for included page parts
* @return String[] the list of pages to include as part of this page object
*/
public static String[] getPageParts(String pageName) {
return PAGE_DATA.computeIfAbsent(pageName, Configuration::loadPageData).getPageParts();
}

/**
* Creates an error message for an expected configuration not being found.
* @param configurtaionValue String the value we couldn't find
* @return String the formatted error message
*/
private static String configurationNotFoundErrorMessage(String configurtaionValue) {
return SentinelStringUtils.format("No {} property set. This can be set in the sentinel.yml config file with a '{}=' property or on the command line with the switch '-D{}='.", configurtaionValue, configurtaionValue, configurtaionValue);
}
Expand Down Expand Up @@ -504,13 +536,8 @@ public static String browser() {
* Returns a sanitized version of the operating system set in the config file or on the command line.
* @return String a sanitized string containing the operating system
*/
public static String operatingSystem() {
var operatingSystem = Configuration.toString("os");
if (operatingSystem == null) {
operatingSystem = detectOperatingSystem();
}

return operatingSystem;
public static String operatingSystem() {
return appProps.getProperty("os");
}

/**
Expand Down
66 changes: 45 additions & 21 deletions src/main/java/com/dougnoel/sentinel/configurations/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Time {
private static final Logger log = LogManager.getLogger(Time.class);

private static Duration timeout = Duration.ZERO;
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10);
private static final Duration DEFAULT_LONG_TIMEOUT = Duration.ofSeconds(60);
private static final Duration interval = Duration.ofMillis(10);
private static final Duration loopInterval = Duration.ofMillis(100);

Expand All @@ -17,6 +19,43 @@ private Time() {

}

static {
initializeTimeOut();
initalizeLongProcessTimeout();
}

/**
* Checks to see if a custom timeout value is configured. If so, that is the timeout used
* to initialize all timeout checks. Otherwise the DEFAULT_TIMEOUT is used.
*/
protected static void initializeTimeOut() {
timeout = getTimeoutInitializationValue("timeout", DEFAULT_TIMEOUT);
}

/**
* Checks to see if a custom longProcessTimeout value is configured. If so, that is the timeout used
* to initialize all longProcessTimeout checks. Otherwise the DEFAULT_LONG_TIMEOUT is used.
*/
protected static void initalizeLongProcessTimeout() {
longProcessTimeout = getTimeoutInitializationValue("longProcessTimeout", DEFAULT_LONG_TIMEOUT);
}

/**
* Common code for setting timeouts.
*
* @param timeOutName String the name of the timeout value to read and write
* @param defaultValue String the default value to use if the value isn't set.
* @return Duration the value is returned to be set to the internal value for tracking.
*/
private static Duration getTimeoutInitializationValue(String timeOutName, Duration defaultValue) {
var timeout = Duration.ofSeconds(Configuration.toLong(timeOutName));
if (timeout.isZero()) {
timeout = defaultValue;
log.debug("No {} property set, using the default timeout value of {} seconds. This can be set in the sentinel.yml config file with a '{}=' property or on the command line with the switch '-Dtimeout='.", timeOutName, timeout, timeOutName);
}
return timeout;
}

/**
* Wait functionality that takes a double in seconds and converts it to milliseconds before waiting.
* @param seconds double number of seconds or fraction thereof (up to milliseconds 10^-3) to wait.
Expand All @@ -39,13 +78,6 @@ public static void wait(double seconds) {
*
*/
public static Duration out() {
if (timeout.isZero()) {
timeout = Duration.ofSeconds(Configuration.toLong("timeout"));
if (timeout.isZero()) {
timeout = Duration.ofSeconds(10);
log.debug("No timeout property set, using the default timeout value of {} seconds. This can be set in the sentinel.yml config file with a 'timeout=' property or on the command line with the switch '-Dtimeout='.", timeout);
}
}
return timeout;
}

Expand All @@ -56,13 +88,6 @@ public static Duration out() {
*
*/
public static Duration longProcessTimeout(){
if (longProcessTimeout.isZero()) {
longProcessTimeout = Duration.ofSeconds(Configuration.toLong("longProcessTimeout"));
if (longProcessTimeout.isZero()) {
longProcessTimeout = Duration.ofSeconds(60);
log.debug("No longProcessTimeout property set, using the default timeout value of {} seconds. This can be set in the sentinel.yml config file with a 'longProcessTimeout=' property or on the command line with the switch '-longProcessTimeout='.", longProcessTimeout);
}
}
return longProcessTimeout;
}

Expand All @@ -81,15 +106,14 @@ public static Duration interval() {
public static Duration loopInterval() {
return loopInterval;
}

/**
* Resets the timeout value so it will be re-read from the configuration.
* Set the timeout to a new value for unit testing.
*
* @param Duration long the amount of time to set the timeout to
*/
public static void reset() {
timeout = Duration.ZERO;
longProcessTimeout = Duration.ZERO;
Configuration.clear("timeout");
Configuration.clear("longProcessTimeout");
protected static void setTimeout(Duration time) {
timeout = time;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dougnoel.sentinel.assertions;

import com.dougnoel.sentinel.configurations.Configuration;
import com.dougnoel.sentinel.configurations.Time;
import com.dougnoel.sentinel.elements.tables.Table;
import com.dougnoel.sentinel.steps.BaseSteps;
import com.dougnoel.sentinel.webdrivers.Driver;
Expand All @@ -16,17 +14,12 @@ public class TableAssertTests {

@BeforeClass
public static void setUpBeforeClass() {
Time.reset();
Configuration.update("timeout", 1);

BaseSteps.navigateToPage("InternetTablesPage");
table = getElementAsTable("table 1");
}

@AfterClass
public static void tearDownAfterClass() {
Time.reset();
Configuration.update("timeout", 10);
Driver.quitAllDrivers();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
import org.junit.Test;

public class ConfigurationDefaultTests {
private static final String ENV = "env";
private static String originalEnvironment = null;

@BeforeClass
public static void setUpBeforeAnyTestsAreRun() {
originalEnvironment = Configuration.environment();
Configuration.environment(null);
Configuration.clear(ENV);
}

@AfterClass
public static void tearDownAfterAllTestsAreFinished() throws Exception {
Configuration.environment(originalEnvironment);
Configuration.update(ENV, originalEnvironment);
}

@Test
public void getEnvironmentDefault() {
Configuration.initializeEnvironment();
assertEquals("Expecting the default env to be set if none is given.", "localhost", Configuration.environment());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class ConfigurationDevTests {
private static String originalEnvironment = null;
private static final String ENV = "env";
private static final String DEV = "dev";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
Expand All @@ -20,14 +21,14 @@ public class ConfigurationDevTests {
@BeforeClass
public static void setUpBeforeAnyTestsAreRun() {
originalEnvironment = Configuration.environment();
Configuration.environment(DEV);
Configuration.update(ENV, DEV);
System.setProperty("download", "downloads");
PageManager.setPage("MockTestPage");
}

@AfterClass
public static void tearDownAfterAllTestsAreFinished() throws Exception {
Configuration.environment(originalEnvironment);
Configuration.update(ENV, originalEnvironment);
Driver.quitAllDrivers();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@

public class ConfigurationProdTests {
private static String originalEnvironment = null;
private static final String ENV = "env";
private static final String PROD = "prod";

@BeforeClass
public static void setUpBeforeAnyTestsAreRun() {
originalEnvironment = Configuration.environment();
Configuration.environment(PROD);
Configuration.update(ENV, PROD);
}

@AfterClass
public static void tearDownAfterAllTestsAreFinished() throws Exception {
Configuration.environment(originalEnvironment);
Configuration.update(ENV, originalEnvironment);
}

@Test
public void loadProdUrl() {
Configuration.environment(PROD);
Configuration.update(ENV, PROD);
assertEquals("Expecting loaded Url.", "http://dougnoel.com/", Configuration.url("DefaultUrls"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class ConfigurationTests {
private static String originalEnvironment = null;
private static final String ENV = "env";
private static final String STAGE = "stage";
private static final String DEV = "dev";
private static final String DEFAULT = "default";
Expand All @@ -28,13 +29,13 @@ public class ConfigurationTests {
@BeforeClass
public static void setUpBeforeAnyTestsAreRun() {
originalEnvironment = Configuration.environment();
Configuration.environment(STAGE);
Configuration.update(ENV, STAGE);
PageManager.setPage("MockTestPage");
}

@AfterClass
public static void tearDownAfterAllTestsAreFinished() throws Exception {
Configuration.environment(originalEnvironment);
Configuration.update(ENV, originalEnvironment);
Driver.quitAllDrivers();
}

Expand Down
Loading