From b96abd440256eaf58f4165611cc36c481b450bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Doug=20No=C3=ABl?= Date: Mon, 12 Dec 2022 16:24:57 -0500 Subject: [PATCH 1/3] Increased the speed of retrieving env and os by setting them once when the Configuration object is created. Added missing javadocs. --- .../configurations/Configuration.java | 73 ++++++++++++++----- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java b/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java index 08a06ee0f..b667743e4 100644 --- a/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java +++ b/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java @@ -53,6 +53,41 @@ 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. + */ + private static void initializeEnvironment() { + env = System.getProperty("env"); + if (env == null) { + env = "localhost"; + String warningMessage = "localhost env being used by default. " + + Configuration.configurationNotFoundErrorMessage("env"); + log.warn(warningMessage); + } + } + + /** + * 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. @@ -242,20 +277,10 @@ 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; } @@ -425,6 +450,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<String, String> the locators for an element + */ public static Map getElement(String elementName, String pageName) { return PAGE_DATA.computeIfAbsent(pageName, Configuration::loadPageData).getElement(elementName); } @@ -472,10 +506,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); } @@ -504,13 +548,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"); } /** From 7584b9502095de77040635d58b26a15ce4f71af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Doug=20No=C3=ABl?= Date: Mon, 12 Dec 2022 20:00:15 -0500 Subject: [PATCH 2/3] Updated Time object to initialize values and removed a bunch of tests that reduced the default timeout value to no effect. --- .../configurations/Configuration.java | 20 ++---- .../sentinel/configurations/Time.java | 66 +++++++++++++------ .../sentinel/assertions/TableAssertTests.java | 7 -- .../ConfigurationDefaultTests.java | 6 +- .../configurations/ConfigurationDevTests.java | 5 +- .../ConfigurationProdTests.java | 7 +- .../configurations/ConfigurationTests.java | 5 +- .../sentinel/configurations/TimeTests.java | 20 +++--- .../sentinel/elements/ElementTests.java | 11 ---- .../elements/WindowsElementTests.java | 6 -- .../sentinel/system/DownloadManagerTests.java | 1 - .../sentinel/webdrivers/WindowListTest.java | 11 ---- 12 files changed, 73 insertions(+), 92 deletions(-) diff --git a/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java b/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java index b667743e4..8dae84cb1 100644 --- a/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java +++ b/src/main/java/com/dougnoel/sentinel/configurations/Configuration.java @@ -34,7 +34,6 @@ public class Configuration { private static final Map PAGE_DATA = new ConcurrentHashMap<>(); private static final String ENV_REPLACE_STRING = "{env}"; - private static String env = null; private static Properties appProps = new Properties(); @@ -65,14 +64,15 @@ private Configuration() { * 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. */ - private static void initializeEnvironment() { - env = System.getProperty("env"); + 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); } /** @@ -281,19 +281,7 @@ public static void update(String property, long value) { * @return String text of system env info */ public static String environment() { - 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"); } /** diff --git a/src/main/java/com/dougnoel/sentinel/configurations/Time.java b/src/main/java/com/dougnoel/sentinel/configurations/Time.java index 261e85981..ecb9ba045 100644 --- a/src/main/java/com/dougnoel/sentinel/configurations/Time.java +++ b/src/main/java/com/dougnoel/sentinel/configurations/Time.java @@ -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); @@ -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. @@ -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; } @@ -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; } @@ -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; } } diff --git a/src/test/java/com/dougnoel/sentinel/assertions/TableAssertTests.java b/src/test/java/com/dougnoel/sentinel/assertions/TableAssertTests.java index 803f4fe51..53eae855e 100644 --- a/src/test/java/com/dougnoel/sentinel/assertions/TableAssertTests.java +++ b/src/test/java/com/dougnoel/sentinel/assertions/TableAssertTests.java @@ -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; @@ -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(); } diff --git a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDefaultTests.java b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDefaultTests.java index c76bff55d..f3f3baf8c 100644 --- a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDefaultTests.java +++ b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDefaultTests.java @@ -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()); } } \ No newline at end of file diff --git a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDevTests.java b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDevTests.java index c0f990799..662b0b509 100644 --- a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDevTests.java +++ b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationDevTests.java @@ -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"; @@ -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(); } diff --git a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationProdTests.java b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationProdTests.java index ae580d105..9f51bbdc0 100644 --- a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationProdTests.java +++ b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationProdTests.java @@ -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")); } } diff --git a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationTests.java b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationTests.java index 642d9d024..4d9f764ff 100644 --- a/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationTests.java +++ b/src/test/java/com/dougnoel/sentinel/configurations/ConfigurationTests.java @@ -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"; @@ -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(); } diff --git a/src/test/java/com/dougnoel/sentinel/configurations/TimeTests.java b/src/test/java/com/dougnoel/sentinel/configurations/TimeTests.java index 6ad74cb1c..90eedc284 100644 --- a/src/test/java/com/dougnoel/sentinel/configurations/TimeTests.java +++ b/src/test/java/com/dougnoel/sentinel/configurations/TimeTests.java @@ -5,35 +5,35 @@ import java.time.Duration; import org.junit.After; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; public class TimeTests { private static final String TIMEOUT = "timeout"; + private static Duration previousTimeout; - @BeforeClass - public static void setUpBeforeAnyTestsAreRun() { - Time.reset(); + @Before + public void setUpBetweenTests() { + previousTimeout = Time.out(); } @After public void cleanUpBetweenTests() { - Time.reset(); + Time.setTimeout(previousTimeout); + Configuration.clear(TIMEOUT); } @Test public void defaultTimeoutTest() { - var previousTimeout = Time.out(); - Time.reset(); - System.clearProperty("timeout"); + System.clearProperty(TIMEOUT); + Time.initializeTimeOut(); assertEquals("Time.out() is using the default when none is set.", Duration.ofSeconds(10), Time.out()); - System.setProperty("timeout", Long.toString(previousTimeout.getSeconds())); - Configuration.update("timeout", previousTimeout.getSeconds()); } @Test public void SetTimeoutTest() { Configuration.update(TIMEOUT, 9L); + Time.initializeTimeOut(); assertEquals("Time.out() is using the passed value when set.", Duration.ofSeconds(9), Time.out()); } } diff --git a/src/test/java/com/dougnoel/sentinel/elements/ElementTests.java b/src/test/java/com/dougnoel/sentinel/elements/ElementTests.java index 9a308c9ff..bbdce509f 100644 --- a/src/test/java/com/dougnoel/sentinel/elements/ElementTests.java +++ b/src/test/java/com/dougnoel/sentinel/elements/ElementTests.java @@ -13,7 +13,6 @@ import com.dougnoel.sentinel.steps.*; import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.Point; @@ -22,23 +21,13 @@ import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.NoSuchElementException; -import com.dougnoel.sentinel.configurations.Configuration; -import com.dougnoel.sentinel.configurations.Time; import com.dougnoel.sentinel.webdrivers.Driver; public class ElementTests { - @BeforeClass - public static void setUpBeforeClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 1); - } - @AfterClass public static void tearDownAfterClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 10); Driver.quitAllDrivers(); } diff --git a/src/test/java/com/dougnoel/sentinel/elements/WindowsElementTests.java b/src/test/java/com/dougnoel/sentinel/elements/WindowsElementTests.java index 933a0b0f4..169ff3adb 100644 --- a/src/test/java/com/dougnoel/sentinel/elements/WindowsElementTests.java +++ b/src/test/java/com/dougnoel/sentinel/elements/WindowsElementTests.java @@ -7,8 +7,6 @@ import org.openqa.selenium.InvalidSelectorException; import org.openqa.selenium.NoSuchElementException; -import com.dougnoel.sentinel.configurations.Configuration; -import com.dougnoel.sentinel.configurations.Time; import com.dougnoel.sentinel.steps.BaseSteps; import com.dougnoel.sentinel.webdrivers.Driver; @@ -17,15 +15,11 @@ public class WindowsElementTests { @BeforeClass public static void setUpBeforeClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 1); BaseSteps.navigateToPage("WindowsElements"); } @AfterClass public static void tearDownAfterClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 10); Driver.quitAllDrivers(); } diff --git a/src/test/java/com/dougnoel/sentinel/system/DownloadManagerTests.java b/src/test/java/com/dougnoel/sentinel/system/DownloadManagerTests.java index 954a95d7f..3d1f85733 100644 --- a/src/test/java/com/dougnoel/sentinel/system/DownloadManagerTests.java +++ b/src/test/java/com/dougnoel/sentinel/system/DownloadManagerTests.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.net.URL; -import com.dougnoel.sentinel.steps.DownloadVerificationSteps; import org.junit.After; import org.junit.Test; diff --git a/src/test/java/com/dougnoel/sentinel/webdrivers/WindowListTest.java b/src/test/java/com/dougnoel/sentinel/webdrivers/WindowListTest.java index 11a0f257b..416bc5875 100644 --- a/src/test/java/com/dougnoel/sentinel/webdrivers/WindowListTest.java +++ b/src/test/java/com/dougnoel/sentinel/webdrivers/WindowListTest.java @@ -1,10 +1,7 @@ package com.dougnoel.sentinel.webdrivers; -import com.dougnoel.sentinel.configurations.Configuration; -import com.dougnoel.sentinel.configurations.Time; import com.dougnoel.sentinel.steps.BaseSteps; import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; import static com.dougnoel.sentinel.webdrivers.Driver.doesWindowExist; @@ -13,16 +10,8 @@ public class WindowListTest { - @BeforeClass - public static void setUpBeforeClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 3); - } - @AfterClass public static void tearDownAfterClass() throws Exception { - Time.reset(); - Configuration.update("timeout", 10); Driver.quitAllDrivers(); } From 91037f4d5ee13ea09d39f0c3ba7a14b2c4153be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Doug=20No=C3=ABl?= Date: Tue, 13 Dec 2022 19:55:18 -0500 Subject: [PATCH 3/3] Code cleanup. --- src/test/java/com/dougnoel/sentinel/files/ZipFileTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/dougnoel/sentinel/files/ZipFileTests.java b/src/test/java/com/dougnoel/sentinel/files/ZipFileTests.java index 05f0d3ee3..95bf387b3 100644 --- a/src/test/java/com/dougnoel/sentinel/files/ZipFileTests.java +++ b/src/test/java/com/dougnoel/sentinel/files/ZipFileTests.java @@ -55,7 +55,7 @@ public void verifyBadZipFileDownloadFromWebCannotOpen() throws InterruptedExcept BaseSteps.navigateToPage("RadioButtonPage"); DownloadManager.setFileExtension("pdf"); BaseSteps.click("sample_download_link"); - String filename = DownloadManager.monitorDownload(); + DownloadManager.monitorDownload(); DownloadVerificationSteps.verifyFileContentsOfZip("", "pdf"); } }