Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebFixture can now read external browserSetup files #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
20 changes: 18 additions & 2 deletions docs/BROWSERSETUP.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -497,7 +498,12 @@ private void populateWithBrowserSpecificSettings(String browserName, List<Browse
throws FixtureException {
StringBuilder builder = new StringBuilder();
BrowserSettingsManager manager = new BrowserSettingsManager();
List<BrowserSetupElement> browserSettings = manager.getBrowserSettings();
List<BrowserSetupElement> 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<BrowserSetting> separateOptions = setting.getOptions();
Expand Down
71 changes: 54 additions & 17 deletions src/main/java/org/testeditor/fixture/web/io/FileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be trace

} 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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trace and then (re)throw the exception. the caller should then wrap this in a fixture exception

+ " 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trace and then (re)throw the exception. the caller should then wrap this in a fixture exception

result = "";
}
}
return result;
}


/**
* For reading content of a JSON-File where the path is defined through the environment variable
* <code>TEST_EDITOR_BROWSER_SETUP_PATH</code>.<br>
* Usage under Linux : Set TEST_EDITOR_BROWSER_SETUP_PATH=~/your_prefered_folder/fileName.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no set, rather export

* 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 <code>TEST_EDITOR_BROWSER_SETUP_PATH</code>
* @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 "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trace

+ "settings JSON file, trying local resource folder src/test/resources", PATH_TO_BROWSER_JSON_FILE);
}
return fileContent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,8 +42,9 @@ public class BrowserSettingsManager {
}

* </code>
* @throws IOException
*/
public List<BrowserSetupElement> getBrowserSettings() throws FixtureException {
public List<BrowserSetupElement> getBrowserSettings() throws FixtureException, IOException {
BrowserSetupReader reader = new BrowserSetupReader();
List<BrowserSetupElement> elements = reader.readElements(FILE_NAME);
Platform currentPlattform = getCurrentPlatform();
Expand All @@ -54,8 +56,9 @@ public List<BrowserSetupElement> getBrowserSettings() throws FixtureException {
*
* @param fileName of JSON test file for browserSetup.json.
* @return A List of BrowserSetupElement
* @throws IOException
*/
protected List<BrowserSetupElement> getBrowserSettings(String fileName) throws FixtureException {
protected List<BrowserSetupElement> getBrowserSettings(String fileName) throws FixtureException, IOException {
BrowserSetupReader reader = new BrowserSetupReader();
List<BrowserSetupElement> elements = reader.readElements(fileName);
Platform currentPlattform = getCurrentPlatform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BrowserSetupElement> readElements(String fileName) throws FixtureException {
public List<BrowserSetupElement> readElements(String fileName) throws FixtureException, IOException {
FileReader reader = new FileReader();
List<BrowserSetupElement> allSetupElements = new ArrayList<>();
String jsonString = reader.getFileContentAsString(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,53 @@
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;
import org.openqa.selenium.firefox.FirefoxProfile;
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
Expand All @@ -61,6 +80,53 @@ public void readCapabilitySuccesful() throws Exception {
logger.debug(" ######## End of Test readCapabilitySuccesful ########");

}

private void getBrowserSetupFile() throws FixtureException, IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split test in two parts

List<BrowserSetupElement> 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<BrowserSetting> 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();
Expand All @@ -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 ########");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand All @@ -57,7 +58,7 @@ public void failureInDuplicateCapability() throws FixtureException {
}

@Test
public void failureInDuplicateOption() throws FixtureException {
public void failureInDuplicateOption() throws FixtureException, IOException {

// given
// when
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down