-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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 | ||
|
@@ -61,6 +80,53 @@ public void readCapabilitySuccesful() throws Exception { | |
logger.debug(" ######## End of Test readCapabilitySuccesful ########"); | ||
|
||
} | ||
|
||
private void getBrowserSetupFile() throws FixtureException, IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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 ########"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be trace