Skip to content

Commit e90b3ae

Browse files
balu836balavengaiah.matamstevewalton28
authored
Chrome preferences (#63)
* added exception wait time out to handle tolerant methods * applied the changes as mentioned in the review comments for tolerant methods * Changed the method to package private to not expose this method * Added test to validate the tolerant method without passing wait time for method * changed the variable names for the TolerantAction object as mentioned * futureDataAvoidingWeekendsAndBankHolidays issue which need to avoid weekends when adding bank holidays count * added overload method for tolerantItemByIndex and tolerantItemByHtmlValueAttribute * resolved conflicts * changed from private package to public * Changed onException method to take screenshot only on Local run not on running on grid * Changed onException method to take screenshot only on Local run not on running on grid * Added boolean parameter in config file to handle taking snapshots * 1.Created tolerant methods for clear,getAttribute,getText and isDisplayed methods. 2. Modified interact methods to handle tolerant methods. * 1.Created tolerant methods for clear,getAttribute and getText methods to handle required exceptions * added chrome browser preferences to change the default file download path. * Added unit test for chrome preferences. * removed unnecessary imports * removed unnecessary imports * added chrome preferences saucelabs config file * updated chrome options variable name * updated download file directory for browser and added unit test for browser preference applied * removed unused imports * added chrome and firefox integration tests to test browser preferences * updated tear down method to reset system config value to default Co-authored-by: balavengaiah.matam <[email protected]> Co-authored-by: Steve Walton <[email protected]>
1 parent 002a329 commit e90b3ae

17 files changed

+318
-34
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ script/dist
5151
logs/*
5252
src/main/resources/bank-holidays.json
5353
node_modules
54-
dist
54+
dist
55+
run-generated-files/*

src/main/java/uk/co/evoco/tests/BaseAbstractSauceLabsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void beforeAll() {
3737
* @throws IOException if results directory isn't created or config file cannot be found
3838
*/
3939
@BeforeEach
40-
public void setUp() throws SauceLabsCredentialsException {
40+
public void setUp() throws SauceLabsCredentialsException, IOException {
4141
ConfiguredDriver sauceLabsDriver = new ConfiguredSauceLabsGridDriver();
4242
this.webDriver = new EventFiringWebDriver(sauceLabsDriver.getRemoteDriver());
4343
this.webDriver.register(new WebDriverListener());

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
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.chrome.ChromeDriver;
67
import org.openqa.selenium.chrome.ChromeOptions;
78
import org.openqa.selenium.remote.RemoteWebDriver;
9+
import uk.co.evoco.webdriver.configuration.BrowserType;
810
import uk.co.evoco.webdriver.configuration.TestConfigHelper;
911

12+
import java.io.File;
1013
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Iterator;
16+
import java.util.Map;
1117

1218
public class ConfiguredChromeDriver implements ConfiguredDriver {
1319

1420
/**
21+
*
1522
*
1623
* @return WebDriver representing RemoteWebDriver grid
1724
*/
18-
public WebDriver getRemoteDriver() {
25+
public WebDriver getRemoteDriver() throws IOException {
1926
return new RemoteWebDriver(
2027
TestConfigHelper.get().getGridConfig().getGridUrl(), this.getOptions());
2128
}
@@ -36,8 +43,32 @@ public WebDriver getLocalDriver() throws IOException {
3643
*
3744
* @return configured options object for target browser driver
3845
*/
39-
public ChromeOptions getOptions() {
46+
public ChromeOptions getOptions() throws IOException {
4047
ChromeOptions chromeOptions = new ChromeOptions();
48+
Map<String, Object> chromePrefs = new HashMap<>();
49+
Iterator<Map.Entry<String, JsonNode>> browserPreferences = TestConfigHelper.get()
50+
.getBrowserPreferences(BrowserType.CHROME)
51+
.fields();
52+
while (browserPreferences.hasNext()) {
53+
Map.Entry<String, JsonNode> entry = browserPreferences.next();
54+
JsonNode value = entry.getValue();
55+
String key = entry.getKey();
56+
switch (value.getNodeType()) {
57+
case BOOLEAN:
58+
chromePrefs.put(key, value.asBoolean());
59+
break;
60+
case NUMBER:
61+
chromePrefs.put(key, value.asInt());
62+
break;
63+
default:
64+
if (key.equals("download.default_directory")) {
65+
chromePrefs.put(key, createFileDownloadDirectory(value.asText()));
66+
} else {
67+
chromePrefs.put(key, value.asText());
68+
}
69+
}
70+
}
71+
chromeOptions.setExperimentalOption("prefs", chromePrefs);
4172
chromeOptions.setHeadless(TestConfigHelper.get().isHeadless());
4273
return chromeOptions;
4374
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
public interface ConfiguredDriver {
1414

1515
WebDriver getLocalDriver() throws IOException;
16-
WebDriver getRemoteDriver();
17-
<T> T getOptions();
16+
WebDriver getRemoteDriver() throws IOException;
17+
<T> T getOptions() throws IOException;
1818

1919
/**
2020
@@ -24,7 +24,7 @@ public interface ConfiguredDriver {
2424
*/
2525
default EventFiringWebDriver getDriver(File screenshotPath) throws IOException {
2626
WebDriver webDriver;
27-
switch(TestConfigHelper.get().getRunType()) {
27+
switch (TestConfigHelper.get().getRunType()) {
2828
case LOCAL:
2929
webDriver = getLocalDriver();
3030
break;
@@ -55,10 +55,22 @@ default EventFiringWebDriver configureEventFiringWebDriver(
5555
}
5656

5757
/**
58-
*
5958
* @throws IOException if the log directory cannot be created
6059
*/
6160
default void createLogDirectory() throws IOException {
6261
FileUtils.forceMkdir(new File("./logs"));
6362
}
63+
64+
/**
65+
*
66+
* @param path runtime browser files download directory path
67+
* @return Absolute file download path
68+
* @throws IOException if the required directory cannot be created
69+
*/
70+
default String createFileDownloadDirectory(String path) throws IOException {
71+
String canonicalPath = new File(path).getCanonicalPath();
72+
FileUtils.forceMkdir(new File(canonicalPath));
73+
return canonicalPath;
74+
}
75+
6476
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
public class ConfiguredFirefoxDriver implements ConfiguredDriver {
1717

1818
/**
19-
*
2019
* @return WebDriver representing RemoteWebDriver grid
2120
*/
22-
public WebDriver getRemoteDriver() {
21+
public WebDriver getRemoteDriver() throws IOException {
2322
return new RemoteWebDriver(
2423
TestConfigHelper.get().getGridConfig().getGridUrl(), this.getOptions());
2524
}
@@ -40,7 +39,7 @@ public WebDriver getLocalDriver() throws IOException {
4039
*
4140
* @return configured options object for target browser driver
4241
*/
43-
public FirefoxOptions getOptions() {
42+
public FirefoxOptions getOptions() throws IOException {
4443
FirefoxOptions firefoxOptions = new FirefoxOptions();
4544
Iterator<Map.Entry<String, JsonNode>> firefoxPreferences = TestConfigHelper.get()
4645
.getBrowserPreferences(BrowserType.FIREFOX)
@@ -58,7 +57,11 @@ public FirefoxOptions getOptions() {
5857
firefoxOptions.addPreference(key, value.asInt());
5958
break;
6059
default:
61-
firefoxOptions.addPreference(key, value.asText());
60+
if (key.equals("browser.download.dir")) {
61+
firefoxOptions.addPreference(key, createFileDownloadDirectory(value.asText()));
62+
} else {
63+
firefoxOptions.addPreference(key, value.asText());
64+
}
6265
break;
6366
}
6467
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
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.chrome.ChromeOptions;
67
import org.openqa.selenium.support.events.EventFiringWebDriver;
78

9+
import java.io.File;
810
import java.io.IOException;
11+
import java.util.Map;
912

1013
import static org.hamcrest.CoreMatchers.instanceOf;
14+
import static org.hamcrest.CoreMatchers.is;
1115
import static org.hamcrest.MatcherAssert.assertThat;
1216

1317
public class ConfiguredChromeDriverTests {
@@ -18,4 +22,19 @@ public void testReturnsLocalWebDriver() throws IOException {
1822
WebDriver webDriver = configuredChromeDriver.getDriver(FileUtils.getTempDirectory());
1923
assertThat(webDriver, instanceOf(EventFiringWebDriver.class));
2024
}
25+
26+
@Test
27+
public void testGetOptionsReturnsOptionsIncludedInChromeConfig() throws IOException {
28+
ConfiguredChromeDriver configuredChromeDriver = new ConfiguredChromeDriver();
29+
Map<String, Object> chromeOptions = getOptions(configuredChromeDriver.getOptions());
30+
String expectedFileDownLoadPath = new File("run-generated-files/chrome/downloads").getCanonicalPath();
31+
assertThat(chromeOptions.get("profile.default_content_settings.popups"), is(0));
32+
assertThat(chromeOptions.get("download.default_directory"), is(expectedFileDownLoadPath));
33+
assertThat(chromeOptions.get("safebrowsing.enabled"), is(true));
34+
}
35+
36+
private Map<String, Object> getOptions(ChromeOptions options) {
37+
Map<String, Object> googleChromeOptions = (Map<String, Object>) options.asMap().get("goog:chromeOptions");
38+
return (Map<String, Object>) googleChromeOptions.get("prefs");
39+
}
2140
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.openqa.selenium.firefox.FirefoxOptions;
77
import org.openqa.selenium.support.events.EventFiringWebDriver;
88

9+
import java.io.File;
910
import java.io.IOException;
1011
import java.util.Map;
1112

@@ -25,16 +26,21 @@ public void testReturnsLocalWebDriver() throws IOException {
2526
}
2627

2728
@Test
28-
public void testGetOptionsReturnsOptionsIncludedInFireFoxConfig() {
29+
public void testGetOptionsReturnsOptionsIncludedInFireFoxConfig() throws IOException {
2930
ConfiguredDriver configuredFirefoxDriver = new ConfiguredFirefoxDriver();
3031
Map<String, Object> firefoxPreferences = getPreferences(configuredFirefoxDriver.getOptions());
31-
assertThat(firefoxPreferences.get("pdfjs.disabled"), is(true));
32+
String expectedFileDownLoadPath = new File("run-generated-files/firefox/downloads").getCanonicalPath();
3233
assertThat(firefoxPreferences.get("browser.download.folderList"), is(2));
33-
assertThat(firefoxPreferences.get("browser.download.defaultFolder"), is("downloads/reports"));
34+
assertThat(firefoxPreferences.get("browser.helperApps.alwaysAsk.force"), is(false));
35+
assertThat(firefoxPreferences.get("browser.download.manager.showWhenStarting"), is(false));
36+
assertThat(firefoxPreferences.get("browser.helperApps.neverAsk.saveToDisk"), is("application/pdf, application/octet-stream"));
37+
assertThat(firefoxPreferences.get("pdfjs.disabled"), is(true));
38+
assertThat(firefoxPreferences.get("unhandled_prompt_behaviour"), is(false));
39+
assertThat(firefoxPreferences.get("browser.download.dir"), is(expectedFileDownLoadPath));
3440
}
3541

36-
private Map<String, Object> getPreferences (FirefoxOptions options) {
37-
Map<String, Object> moxOptions = (Map<String, Object>)options.asMap().get("moz:firefoxOptions");
38-
return (Map<String, Object>)moxOptions.get("prefs");
42+
private Map<String, Object> getPreferences(FirefoxOptions options) {
43+
Map<String, Object> moxOptions = (Map<String, Object>) options.asMap().get("moz:firefoxOptions");
44+
return (Map<String, Object>) moxOptions.get("prefs");
3945
}
4046
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package uk.co.evoco.webdriver.configuration.driver;
22

3+
import org.junit.jupiter.api.AfterAll;
34
import org.junit.jupiter.api.Test;
45
import org.openqa.selenium.WebDriver;
56
import uk.co.evoco.exceptions.SauceLabsCredentialsException;
67

8+
import java.io.IOException;
9+
710
import static org.hamcrest.MatcherAssert.assertThat;
811
import static org.hamcrest.Matchers.containsString;
912

1013
public class ConfiguredSauceLabsDriverTests {
1114

1215
@Test
13-
public void testCanRunTestOnSauceLabsUsingConfigiration() throws SauceLabsCredentialsException{
16+
public void testCanRunTestOnSauceLabsUsingConfigiration() throws SauceLabsCredentialsException, IOException {
1417
System.setProperty("config", "config-saucelabs.json");
1518
ConfiguredDriver configuredSauceLabsGridDriver = new ConfiguredSauceLabsGridDriver();
1619
WebDriver webDriver = configuredSauceLabsGridDriver.getRemoteDriver();
1720
webDriver.get("https://www.evoco.co.uk");
1821
assertThat("Didn't make it to evoco.co.uk", webDriver.getCurrentUrl(), containsString("evoco.co.uk"));
1922
webDriver.quit();
2023
}
24+
25+
@AfterAll
26+
public static void tearDown() {
27+
System.setProperty("config", "DEFAULT");
28+
}
2129
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.junit.jupiter.api.*;
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.WebDriver;
7+
import uk.co.evoco.webdriver.configuration.driver.ConfiguredChromeDriver;
8+
9+
import java.io.File;
10+
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
14+
public class ChromeDriverPreferenceTests {
15+
16+
private static String baseUrl;
17+
private static EmbeddedJetty embeddedJetty;
18+
private WebDriver webDriver;
19+
20+
@BeforeAll
21+
public static void webDriverSetup() throws Exception {
22+
embeddedJetty = new EmbeddedJetty();
23+
embeddedJetty.start();
24+
baseUrl = "http://localhost:" + embeddedJetty.getPort() + "/index.html";
25+
}
26+
27+
@Test
28+
public void testChromeBrowserPreferencesApplied() throws Exception {
29+
System.setProperty("config", "fixtures/sample-config-with-chrome-preferences.json");
30+
webDriver = new ConfiguredChromeDriver().getDriver(FileUtils.getTempDirectory());
31+
webDriver.get(baseUrl);
32+
String expectedFile = new File("run-generated-files/chrome/downloads").getCanonicalPath() + "/sampleFile.pdf";
33+
webDriver.findElement(By.xpath("//a[text()='clickHereToDownLoadAFile']")).click();
34+
Thread.sleep(2000);//need to wait until file download
35+
assertThat(new File(expectedFile).exists(), is(true));
36+
}
37+
38+
@AfterEach
39+
public void tearDown() {
40+
this.webDriver.quit();
41+
}
42+
43+
@AfterAll
44+
public static void webDriverTearDown() throws Exception {
45+
System.setProperty("config", "DEFAULT");
46+
embeddedJetty.stop();
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.junit.jupiter.api.*;
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.WebDriver;
7+
import uk.co.evoco.webdriver.configuration.driver.ConfiguredFirefoxDriver;
8+
9+
import java.io.File;
10+
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
14+
public class FirefoxDriverPreferenceTests {
15+
16+
private static String baseUrl;
17+
private static EmbeddedJetty embeddedJetty;
18+
private WebDriver webDriver;
19+
20+
@BeforeAll
21+
public static void webDriverSetup() throws Exception {
22+
embeddedJetty = new EmbeddedJetty();
23+
embeddedJetty.start();
24+
baseUrl = "http://localhost:" + embeddedJetty.getPort() + "/index.html";
25+
}
26+
27+
@Test
28+
public void testFirefoxBrowserPreferencesApplied() throws Exception {
29+
System.setProperty("config", "fixtures/sample-config-with-firefox-preferences.json");
30+
webDriver = new ConfiguredFirefoxDriver().getDriver(FileUtils.getTempDirectory());
31+
webDriver.get(baseUrl);
32+
String expectedFile = new File("run-generated-files/firefox/downloads").getCanonicalPath() + "/sampleFile.pdf";
33+
webDriver.findElement(By.xpath("//a[text()='clickHereToDownLoadAFile']")).click();
34+
Thread.sleep(2000);//need to wait until file download
35+
assertThat(new File(expectedFile).exists(), is(true));
36+
}
37+
38+
@AfterEach
39+
public void tearDown() {
40+
this.webDriver.quit();
41+
}
42+
43+
@AfterAll
44+
public static void webDriverTearDown() throws Exception {
45+
System.setProperty("config", "DEFAULT");
46+
embeddedJetty.stop();
47+
}
48+
49+
}

0 commit comments

Comments
 (0)