diff --git a/docs/BROWSERSETUP.md b/docs/BROWSERSETUP.md index 29e0f80..f7b23af 100644 --- a/docs/BROWSERSETUP.md +++ b/docs/BROWSERSETUP.md @@ -1,6 +1,22 @@ # Browser Setup for Testing -It is possible to activate browser-specific settings before test execution. -E.g. when a special proxy setting is needed for Firefox on a System Under Test (SUT) environment under Linux which is different to the proxy setting under Windows, it is possible to create a JSON-File called **browserSetup.json** in the directory **src/test/resources** of the actual test project. These specified settings will be used for test execution. +It is possible to activate browser-specific settings before test execution. +There are two posibilities to activate these settings. +1. through an environment variable named ```TEST_EDITOR_BROWSER_SETUP_PATH``` +When this possibility will be chosen, the tester can determine which file name he wants to use and the path where the file should be located. + + + 1. **Example for Linux:** + setting the environment variable in your HOME directory under testsetup + + ``` export TEST_EDITOR_BROWSER_SETUP_PATH=~/testsetup/firefoxBrowserSettings.json``` + + 1. **Example for Windows:** + setting the environment variable on drive C:\ in directory testsetup + + ``` set TEST_EDITOR_BROWSER_SETUP_PATH=C:/testsetup/firefoxBrowserSettings.json``` + +2. through JSON-File called ```browserSetup.json``` +E.g. when a special proxy setting is needed for Firefox on a System Under Test (SUT) environment under Linux which is different to the proxy setting under Windows, it is possible to create a JSON-File called ```browserSetup.json``` in the directory ```src/test/resources``` of the actual test project. These specified settings will be used for test execution. ## Documentation for Options For [Firefox Options](https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Firefox/Options.html) diff --git a/src/main/java/org/testeditor/fixture/web/WebDriverFixture.java b/src/main/java/org/testeditor/fixture/web/WebDriverFixture.java index 6ac7198..ec5dcd2 100644 --- a/src/main/java/org/testeditor/fixture/web/WebDriverFixture.java +++ b/src/main/java/org/testeditor/fixture/web/WebDriverFixture.java @@ -52,6 +52,7 @@ import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerOptions; import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.logging.NeedsLocalLogs; import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; @@ -497,7 +498,12 @@ private void populateWithBrowserSpecificSettings(String browserName, List browserSettings = manager.getBrowserSettings(); + List browserSettings = null; + try { + browserSettings = manager.getBrowserSettings(); + } catch (IOException e) { + throw new FixtureException("An error occured when resolving browser settings", e); + } browserSettings.forEach((setting) -> { if (setting.getBrowserName().equalsIgnoreCase(browserName)) { List separateOptions = setting.getOptions(); diff --git a/src/main/java/org/testeditor/fixture/web/io/FileReader.java b/src/main/java/org/testeditor/fixture/web/io/FileReader.java index 559f964..710f81b 100644 --- a/src/main/java/org/testeditor/fixture/web/io/FileReader.java +++ b/src/main/java/org/testeditor/fixture/web/io/FileReader.java @@ -13,44 +13,81 @@ package org.testeditor.fixture.web.io; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testeditor.fixture.core.FixtureException; import com.google.common.io.CharStreams; public class FileReader { private static final Logger logger = LoggerFactory.getLogger(FileReader.class); + public static final String PATH_TO_BROWSER_JSON_FILE = "TEST_EDITOR_BROWSER_SETUP_PATH"; /** * Opens a file defined as fileName and read the content line by line. * * @return File content as String + * @throws IOException */ - public String getFileContentAsString(String fileName) throws FixtureException { - - // Get file from resources folder - ClassLoader classLoader = getClass().getClassLoader(); - InputStream inputStream = classLoader.getResourceAsStream(fileName); - + public String getFileContentAsString(String fileName) throws IOException { String result = null; - try { - result = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); - } catch (IOException e) { - logger.info("The file with the name {} can not be read in the resource folder. {}" , fileName, e); - throw new FixtureException("file could not be found in resource folder", - FixtureException.keyValues("fileName", fileName)); - } catch (NullPointerException e) { - logger.info("The file with the name {} can not be found in the resource folder.", fileName); - result = ""; + String browserSetupFilePath = System.getenv(PATH_TO_BROWSER_JSON_FILE); + // First try to resolve the file through environment variable + result = getResourceOverEnvironmentVariable(browserSetupFilePath); + if (StringUtils.isNotBlank(result)) { + logger.debug("Browser capabilities read from file : {}", browserSetupFilePath); + } else { + // Get file from resources folder + ClassLoader classLoader = getClass().getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream(fileName); + logger.debug("Browser capabilities read from file : {} in \"src/test/resources\" folder", fileName); + try { + result = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + + } catch (IOException e) { + logger.info("The file with the name {} can not be read in the resource folder." + + " Exception occured: {}" , fileName, e); + // throw new FixtureException("file could not be found in resource folder", + // FixtureException.keyValues("fileName", fileName)); + } catch (NullPointerException e) { + logger.error("The file with the name {} can not be found in the resource folder.", fileName); + result = ""; + } } return result; } - + + /** + * For reading content of a JSON-File where the path is defined through the environment variable + * TEST_EDITOR_BROWSER_SETUP_PATH.
+ * Usage under Linux : Set TEST_EDITOR_BROWSER_SETUP_PATH=~/your_prefered_folder/fileName.json + * Usage under Windows : Set TEST_EDITOR_BROWSER_SETUP_PATH=C:/your_prefered_folder/fileName.json + * + * @return The content of the the file with the path defined in an environment variable + * named TEST_EDITOR_BROWSER_SETUP_PATH + * @throws IOException + * When File can not be read + * @throws FileNotFoundException + * When File is not present on given path. + */ + protected String getResourceOverEnvironmentVariable(String path) throws IOException { + String fileContent = null; + if (StringUtils.isNotBlank(path)) { + fileContent = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8); + } else { + logger.debug("Can not find environment variable '{}' with path entry for browser " + + "settings JSON file, trying local resource folder src/test/resources", PATH_TO_BROWSER_JSON_FILE); + } + return fileContent; + } + } diff --git a/src/main/java/org/testeditor/fixture/web/json/BrowserSettingsManager.java b/src/main/java/org/testeditor/fixture/web/json/BrowserSettingsManager.java index b4701ef..6c4ae6e 100644 --- a/src/main/java/org/testeditor/fixture/web/json/BrowserSettingsManager.java +++ b/src/main/java/org/testeditor/fixture/web/json/BrowserSettingsManager.java @@ -13,6 +13,7 @@ package org.testeditor.fixture.web.json; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -41,8 +42,9 @@ public class BrowserSettingsManager { } * + * @throws IOException */ - public List getBrowserSettings() throws FixtureException { + public List getBrowserSettings() throws FixtureException, IOException { BrowserSetupReader reader = new BrowserSetupReader(); List elements = reader.readElements(FILE_NAME); Platform currentPlattform = getCurrentPlatform(); @@ -54,8 +56,9 @@ public List getBrowserSettings() throws FixtureException { * * @param fileName of JSON test file for browserSetup.json. * @return A List of BrowserSetupElement + * @throws IOException */ - protected List getBrowserSettings(String fileName) throws FixtureException { + protected List getBrowserSettings(String fileName) throws FixtureException, IOException { BrowserSetupReader reader = new BrowserSetupReader(); List elements = reader.readElements(fileName); Platform currentPlattform = getCurrentPlatform(); diff --git a/src/main/java/org/testeditor/fixture/web/json/BrowserSetupReader.java b/src/main/java/org/testeditor/fixture/web/json/BrowserSetupReader.java index cd1df43..bc0be32 100644 --- a/src/main/java/org/testeditor/fixture/web/json/BrowserSetupReader.java +++ b/src/main/java/org/testeditor/fixture/web/json/BrowserSetupReader.java @@ -13,6 +13,7 @@ package org.testeditor.fixture.web.json; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -40,8 +41,9 @@ public class BrowserSetupReader { * @param fileName of the BrowserElements * @return All Browser or OS specific settings for starting a browser in a test environment. * @throws FixtureException + * @throws IOException */ - public List readElements(String fileName) throws FixtureException { + public List readElements(String fileName) throws FixtureException, IOException { FileReader reader = new FileReader(); List allSetupElements = new ArrayList<>(); String jsonString = reader.getFileContentAsString(fileName); diff --git a/src/test/java/org/testeditor/fixture/web/CapabilityIntegrationtest.java b/src/test/java/org/testeditor/fixture/web/CapabilityIntegrationtest.java index d302043..caf9924 100644 --- a/src/test/java/org/testeditor/fixture/web/CapabilityIntegrationtest.java +++ b/src/test/java/org/testeditor/fixture/web/CapabilityIntegrationtest.java @@ -17,7 +17,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; @@ -25,26 +30,40 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testeditor.fixture.core.FixtureException; +import org.testeditor.fixture.web.io.FileReader; +import org.testeditor.fixture.web.json.BrowserSetting; +import org.testeditor.fixture.web.json.BrowserSetupElement; +import org.testeditor.fixture.web.json.BrowserSetupReader; public class CapabilityIntegrationtest { + private static final String BROWSER_SETUP_JSON = "browserSetup.json"; + private static final Logger logger = LoggerFactory.getLogger(CapabilityIntegrationtest.class); - private static String proxyHttpExpected = "http://mysystem.proxy.server"; - private static String proxySslExpected = "http://mysystem_ssl.proxy.server"; - private static int proxyTypeExpected = 5; - private static int proxySslPortExpected = 108; - private static int proxyhttpPortExpected = 101; - private static String proxyHttp = ""; - private static String proxySsl = ""; - private static int proxyHttpPort = 0; - private static int proxySslPort = 0; - private static int proxyType = 0; + private static String proxyHttpExpected ; + private static String proxySslExpected ; + private static int proxyTypeExpected ; + private static int proxySslPortExpected ; + private static int proxyhttpPortExpected ; + private static String proxyHttp = null; + private static String proxySsl = null; + private static int proxyHttpPort ; + private static int proxySslPort ; + private static int proxyType ; + private static String PROXY_HTTP_KEY = "network.proxy.http"; + private static String PROXY_SSL_KEY = "network.proxy.ssl"; + private static String PROXY_TYPE = "network.proxy.type"; + private static String PROXY_SSL_PORT = "network.proxy.ssl_port"; + private static String PROXY_HTTP_PORT = "network.proxy.http_port"; + @Test public void readCapabilitySuccesful() throws Exception { // given + getBrowserSetupFile(); WebDriverFixture fixture = new WebDriverFixture(); // when @@ -61,6 +80,53 @@ public void readCapabilitySuccesful() throws Exception { logger.debug(" ######## End of Test readCapabilitySuccesful ########"); } + + private void getBrowserSetupFile() throws FixtureException, IOException { + List elements = null; + // check if environment variable exists + String browserSetupPath = System.getenv(FileReader.PATH_TO_BROWSER_JSON_FILE); + if (StringUtils.isNotBlank(browserSetupPath)) { + logger.debug("External browserSetup file : {} used !", browserSetupPath); + BrowserSetupReader reader = new BrowserSetupReader(); + elements = reader.readElements(browserSetupPath); + } + // check if browserSetup.json file exists on 'src/test/resources' path + ClassLoader classLoader = getClass().getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream(BROWSER_SETUP_JSON); + if (StringUtils.isBlank(browserSetupPath) && inputStream != null) { + logger.debug("Internal browserSetup file browserSetup.json at path src/test/resources is used !"); + BrowserSetupReader reader = new BrowserSetupReader(); + elements = reader.readElements(BROWSER_SETUP_JSON); + } else if (StringUtils.isBlank(browserSetupPath) && inputStream == null) { + // There is no file to proof please check the prerequisites ! + throw new IllegalArgumentException("There is no browserSetup file to execute the test"); + } + + for (BrowserSetupElement browserSetupElement : elements) { + if (browserSetupElement.getBrowserName().equalsIgnoreCase("firefox") + && (browserSetupElement.getOsName() == null || browserSetupElement.getOsName().equals("LINUX"))) { + List options = browserSetupElement.getOptions(); + for (BrowserSetting browserOption : options) { + String key = browserOption.getKey(); + if (key.equals(PROXY_HTTP_KEY)) { + proxyHttpExpected = (String) browserOption.getValue(); + } + if (key.equals(PROXY_SSL_KEY)) { + proxySslExpected = (String) browserOption.getValue(); + } + if (key.equals(PROXY_HTTP_PORT)) { + proxyhttpPortExpected = (int) browserOption.getValue(); + } + if (key.equals(PROXY_SSL_PORT)) { + proxySslPortExpected = (int) browserOption.getValue(); + } + if (key.equals(PROXY_TYPE)) { + proxyTypeExpected = (int) browserOption.getValue(); + } + } + } + } + } private void readCapabilitiesFromProfile(WebDriverFixture fixture) { WebDriver driver = fixture.getDriver(); @@ -78,7 +144,6 @@ private void readCapabilitiesFromProfile(WebDriverFixture fixture) { logger.debug("Firefox Preference proxy SSL port = {} read successfully", proxySslPort); proxyType = profile.getIntegerPreference("network.proxy.type", 0); logger.debug("Firefox Preference proxy type = {} read successfully", proxyType); - logger.debug(" ######## End of Test readCapabilitiesFromProfile ########"); } } diff --git a/src/test/java/org/testeditor/fixture/web/json/BrowserSettingsManagerTest.java b/src/test/java/org/testeditor/fixture/web/json/BrowserSettingsManagerTest.java index 4aecb22..e8eea5b 100644 --- a/src/test/java/org/testeditor/fixture/web/json/BrowserSettingsManagerTest.java +++ b/src/test/java/org/testeditor/fixture/web/json/BrowserSettingsManagerTest.java @@ -17,6 +17,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -40,7 +41,7 @@ public class BrowserSettingsManagerTest { public ExpectedException thrown = ExpectedException.none(); @Test - public void failureInDuplicateCapability() throws FixtureException { + public void failureInDuplicateCapability() throws FixtureException, IOException { // given + when thrown.expect(FixtureException.class); thrown.expectMessage("Duplicate entries existing in configuration file"); @@ -57,7 +58,7 @@ public void failureInDuplicateCapability() throws FixtureException { } @Test - public void failureInDuplicateOption() throws FixtureException { + public void failureInDuplicateOption() throws FixtureException, IOException { // given // when @@ -77,7 +78,7 @@ public void failureInDuplicateOption() throws FixtureException { } @Test - public void successfulWindowsTestForOptions() throws FixtureException { + public void successfulWindowsTestForOptions() throws FixtureException, IOException { // given BrowserSettingsManager manager = new BrowserSettingsManager(); @@ -97,7 +98,7 @@ public void successfulWindowsTestForOptions() throws FixtureException { } @Test - public void successfulLinuxTestForOptions() throws FixtureException { + public void successfulLinuxTestForOptions() throws FixtureException, IOException { // given BrowserSettingsManager manager = new BrowserSettingsManager(); @@ -117,7 +118,7 @@ public void successfulLinuxTestForOptions() throws FixtureException { } @Test - public void successfulMacTestForOptions() throws FixtureException { + public void successfulMacTestForOptions() throws FixtureException, IOException { // given BrowserSettingsManager manager = new BrowserSettingsManager(); diff --git a/src/test/java/org/testeditor/fixture/web/json/BrowserSetupReaderIntegrationTest.java b/src/test/java/org/testeditor/fixture/web/json/BrowserSetupReaderIntegrationTest.java index a6c737e..5d544cc 100644 --- a/src/test/java/org/testeditor/fixture/web/json/BrowserSetupReaderIntegrationTest.java +++ b/src/test/java/org/testeditor/fixture/web/json/BrowserSetupReaderIntegrationTest.java @@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; import java.util.List; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -28,7 +29,7 @@ public class BrowserSetupReaderIntegrationTest { private static final Logger logger = LoggerFactory.getLogger(BrowserSetupReaderIntegrationTest.class); @Test - public void integrationTest() throws FixtureException { + public void integrationTest() throws FixtureException, IOException { // given BrowserSetupReader browserSetupReader = new BrowserSetupReader();