Skip to content

Commit 1a6231b

Browse files
Add driver specific config (#37)
* Allow the WebDriverConfig class handle browserPreferencess We want to be able to include browserPreferences as a key in the root of the config file and have the resulting WebDriverConfig object expose methods to fetch specific properties based on the provided browser type. This can then be picked up when the drivers are created. This commit only lets the WebDriverCongfig class support this extra field. Following commits will have the code that includes the configs for the supported driver types. In terms of testing, we have not included fixture files for the various test cases (as is the case in the other tests) So we should probably have a chat about that and refactor the code to either use the fixture files or leave as-is. The reason we choose not to crerate the extra fixture files is that it would require at least 3 extra files for the 4 test cases wich can be a bit excessive especially as defining the input inline does not make the code less readable? Will be happy to change this approach if others think otherwise. We also handled the scenario where the case of the browser type does not matter. This can also be removed if deemed unnecessary. * Include firefox preferences from config into driver We want to be able to load user-specified preferences from config (if it exists) onto the driver. This has to be browser specific as the techniques for each of the browsers are different. This commit adds the necessary code for including firefox preferences when creating a firefox driver.
1 parent b68d023 commit 1a6231b

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/main/java/uk/co/evoco/webdriver/configuration/WebDriverConfig.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
57

68
import java.net.MalformedURLException;
79
import java.net.URL;
810
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Optional;
913

1014
/**
1115
* A simple representation object for the "./src/test/resources/config.json" file
@@ -20,6 +24,7 @@ public class WebDriverConfig {
2024
private GridConfig gridConfig;
2125
private RunType runType;
2226
private List<String> exceptionsToHandleOnTolerantActions;
27+
private Map<String, ObjectNode> browserPreferences;
2328

2429
/**
2530
*
@@ -138,6 +143,29 @@ public void setGridConfig(GridConfig gridConfig) {
138143
this.gridConfig = gridConfig;
139144
}
140145

146+
/**
147+
*
148+
* @return the BrowserPreferences configuration
149+
*/
150+
public ObjectNode getBrowserPreferences(BrowserType browserType) {
151+
return Optional.ofNullable(browserPreferences)
152+
.flatMap(options -> options.entrySet().stream()
153+
.filter(entry -> entry.getKey().equalsIgnoreCase((browserType.toString())))
154+
.findFirst()
155+
.map(Map.Entry::getValue)
156+
)
157+
.orElse(JsonNodeFactory.instance.objectNode());
158+
}
159+
160+
/**
161+
*
162+
* @param browserPreferences the configuration properties for the various browsers supported by the webdriver
163+
*/
164+
@JsonProperty("browserPreferences")
165+
public void setBrowserPreferences(Map<String, ObjectNode> browserPreferences) {
166+
this.browserPreferences = browserPreferences;
167+
}
168+
141169
/**
142170
*
143171
* @return the run type

src/main/java/uk/co/evoco/webdriver/configuration/driver/ConfiguredFirefoxDriver.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package uk.co.evoco.webdriver.configuration.driver;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import io.github.bonigarcia.wdm.WebDriverManager;
45
import org.openqa.selenium.WebDriver;
56
import org.openqa.selenium.firefox.FirefoxDriver;
67
import org.openqa.selenium.firefox.FirefoxOptions;
78
import org.openqa.selenium.remote.RemoteWebDriver;
9+
import uk.co.evoco.webdriver.configuration.BrowserType;
810
import uk.co.evoco.webdriver.configuration.TestConfigManager;
911

1012
import java.io.IOException;
13+
import java.util.Iterator;
14+
import java.util.Map;
1115

1216
public class ConfiguredFirefoxDriver implements ConfiguredDriver {
1317

@@ -38,6 +42,26 @@ public WebDriver getLocalDriver() throws IOException {
3842
*/
3943
public FirefoxOptions getOptions() {
4044
FirefoxOptions firefoxOptions = new FirefoxOptions();
45+
Iterator<Map.Entry<String, JsonNode>> firefoxPreferences = TestConfigManager.get()
46+
.getBrowserPreferences(BrowserType.FIREFOX)
47+
.fields();
48+
49+
while (firefoxPreferences.hasNext()) {
50+
Map.Entry<String, JsonNode> entry = firefoxPreferences.next();
51+
JsonNode value = entry.getValue();
52+
String key = entry.getKey();
53+
switch (value.getNodeType()) {
54+
case BOOLEAN:
55+
firefoxOptions.addPreference(key, value.asBoolean());
56+
break;
57+
case NUMBER:
58+
firefoxOptions.addPreference(key, value.asInt());
59+
break;
60+
default:
61+
firefoxOptions.addPreference(key, value.asText());
62+
}
63+
}
64+
4165
firefoxOptions.setHeadless(TestConfigManager.get().isHeadless());
4266
return firefoxOptions;
4367
}

src/test/java/uk/co/evoco/webdriver/configuration/WebDriverConfigTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package uk.co.evoco.webdriver.configuration;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
7+
import com.fasterxml.jackson.databind.node.ObjectNode;
48
import org.junit.jupiter.api.Test;
59
import uk.co.evoco.webdriver.utils.JsonUtils;
610

@@ -25,6 +29,52 @@ public void testCanCreateInstanceFromJsonFileAndTestGetters() throws IOException
2529
assertThat(webDriverConfig.getGridConfig().getGridUrl().toString(), is("http://localhost:4444/wd/hub"));
2630
}
2731

32+
@Test
33+
public void testGetBrowserPreferencesReturnsAnEmptyNodeIfNoBrowserOptionsArePresentInConfigJson() throws JsonProcessingException {
34+
String configJson = "{}";
35+
WebDriverConfig webDriverConfig = JsonUtils.fromString(configJson, WebDriverConfig.class);
36+
37+
JsonNode actualPreferences = webDriverConfig.getBrowserPreferences(BrowserType.CHROME);
38+
assertThat(actualPreferences, is(JsonNodeFactory.instance.objectNode()));
39+
}
40+
41+
@Test
42+
public void testGetBrowserPreferencesReturnsAnEmptyNodeIfSpecifiedBrowserTypeIsNotPresentInConfigJson() throws JsonProcessingException {
43+
String configJson = "{ \"browserPreferences\": {}}";
44+
WebDriverConfig webDriverConfig = JsonUtils.fromString(configJson, WebDriverConfig.class);
45+
46+
JsonNode actualPreferences = webDriverConfig.getBrowserPreferences(BrowserType.CHROME);
47+
assertThat(actualPreferences, is(JsonNodeFactory.instance.objectNode()));
48+
}
49+
50+
@Test
51+
public void testGetBrowserPreferencesReturnsTheCorrectBrowserOptions() throws JsonProcessingException {
52+
String preferenceKey = "browser.download.dir";
53+
String preferenceValue = "docs/chrome/";
54+
String inputConfigJson = String.format("{ \"browserPreferences\": { \"chrome\": {\"%s\": \"%s\"}}}", preferenceKey, preferenceValue);
55+
56+
WebDriverConfig webDriverConfig = JsonUtils.fromString(inputConfigJson, WebDriverConfig.class);
57+
JsonNode actualPreferences = webDriverConfig.getBrowserPreferences(BrowserType.CHROME);
58+
59+
ObjectNode expectedPreferences = JsonNodeFactory.instance.objectNode()
60+
.put(preferenceKey, preferenceValue);
61+
assertThat(actualPreferences, is(expectedPreferences));
62+
}
63+
64+
@Test
65+
public void testGetBrowserPropertiesReturnsTheCorrectBrowserOptionsIrrespectiveOfCase() throws JsonProcessingException {
66+
String preferenceKey = "browser.download.dir";
67+
String preferenceValue = "docs/chrome/";
68+
String inputConfigJson = String.format("{ \"browserPreferences\": { \"CHROME\": {\"%s\": \"%s\"}}}", preferenceKey, preferenceValue);
69+
70+
WebDriverConfig webDriverConfig = JsonUtils.fromString(inputConfigJson, WebDriverConfig.class);
71+
JsonNode actualPreferences = webDriverConfig.getBrowserPreferences(BrowserType.CHROME);
72+
73+
ObjectNode expectedPreferences = JsonNodeFactory.instance.objectNode()
74+
.put(preferenceKey, preferenceValue);
75+
assertThat(actualPreferences, is(expectedPreferences));
76+
}
77+
2878
@Test
2979
public void testConstructionFromJsonFileWithBadBaseUrlFails() {
3080
assertThrows(JsonMappingException.class, () -> {

src/test/java/uk/co/evoco/webdriver/configuration/driver/ConfiguredFirefoxDriverIT.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import org.apache.commons.io.FileUtils;
44
import org.junit.jupiter.api.Test;
55
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.firefox.FirefoxOptions;
67
import org.openqa.selenium.support.events.EventFiringWebDriver;
78

89
import java.io.IOException;
10+
import java.util.Map;
911

1012
import static org.hamcrest.CoreMatchers.instanceOf;
13+
import static org.hamcrest.CoreMatchers.is;
1114
import static org.hamcrest.MatcherAssert.assertThat;
1215

1316
public class ConfiguredFirefoxDriverIT {
@@ -18,4 +21,18 @@ public void testReturnsLocalWebDriver() throws IOException {
1821
WebDriver webDriver = configuredFirefoxDriver.getDriver(FileUtils.getTempDirectory());
1922
assertThat(webDriver, instanceOf(EventFiringWebDriver.class));
2023
}
24+
25+
@Test
26+
public void testGetOptionsReturnsOptionsIncludedInFireFoxConfig() {
27+
ConfiguredFirefoxDriver configuredFirefoxDriver = new ConfiguredFirefoxDriver();
28+
Map<String, Object> firefoxPreferences = getPreferences(configuredFirefoxDriver.getOptions());
29+
assertThat(firefoxPreferences.get("pdfjs.disabled"), is(true));
30+
assertThat(firefoxPreferences.get("browser.download.folderList"), is(2));
31+
assertThat(firefoxPreferences.get("browser.download.defaultFolder"), is("downloads/reports"));
32+
}
33+
34+
private Map<String, Object> getPreferences (FirefoxOptions options) {
35+
Map<String, Object> moxOptions = (Map<String, Object>)options.asMap().get("moz:firefoxOptions");
36+
return (Map<String, Object>)moxOptions.get("prefs");
37+
}
2138
}

src/test/resources/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
"gridConfig": {
1111
"url": "http://localhost:4444/wd/hub"
1212
},
13+
"browserPreferences" : {
14+
"firefox": {
15+
"browser.download.defaultFolder": "downloads/reports",
16+
"pdfjs.disabled": true,
17+
"browser.download.folderList": 2
18+
}
19+
},
1320
"exceptionsToHandleOnTolerantActions": [
1421
"StaleElementReferenceException",
1522
"ElementClickInterceptedException",

0 commit comments

Comments
 (0)