Skip to content

Commit

Permalink
Chrome preferences (#63)
Browse files Browse the repository at this point in the history
* 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 <balavengaiah.matam@dwp.gov.uk>
Co-authored-by: Steve Walton <steve.walton@evoco.co.uk>
  • Loading branch information
3 people authored Dec 16, 2020
1 parent 002a329 commit e90b3ae
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ script/dist
logs/*
src/main/resources/bank-holidays.json
node_modules
dist
dist
run-generated-files/*
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void beforeAll() {
* @throws IOException if results directory isn't created or config file cannot be found
*/
@BeforeEach
public void setUp() throws SauceLabsCredentialsException {
public void setUp() throws SauceLabsCredentialsException, IOException {
ConfiguredDriver sauceLabsDriver = new ConfiguredSauceLabsGridDriver();
this.webDriver = new EventFiringWebDriver(sauceLabsDriver.getRemoteDriver());
this.webDriver.register(new WebDriverListener());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package uk.co.evoco.webdriver.configuration.driver;

import com.fasterxml.jackson.databind.JsonNode;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import uk.co.evoco.webdriver.configuration.BrowserType;
import uk.co.evoco.webdriver.configuration.TestConfigHelper;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ConfiguredChromeDriver implements ConfiguredDriver {

/**
*
*
* @return WebDriver representing RemoteWebDriver grid
*/
public WebDriver getRemoteDriver() {
public WebDriver getRemoteDriver() throws IOException {
return new RemoteWebDriver(
TestConfigHelper.get().getGridConfig().getGridUrl(), this.getOptions());
}
Expand All @@ -36,8 +43,32 @@ public WebDriver getLocalDriver() throws IOException {
*
* @return configured options object for target browser driver
*/
public ChromeOptions getOptions() {
public ChromeOptions getOptions() throws IOException {
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Object> chromePrefs = new HashMap<>();
Iterator<Map.Entry<String, JsonNode>> browserPreferences = TestConfigHelper.get()
.getBrowserPreferences(BrowserType.CHROME)
.fields();
while (browserPreferences.hasNext()) {
Map.Entry<String, JsonNode> entry = browserPreferences.next();
JsonNode value = entry.getValue();
String key = entry.getKey();
switch (value.getNodeType()) {
case BOOLEAN:
chromePrefs.put(key, value.asBoolean());
break;
case NUMBER:
chromePrefs.put(key, value.asInt());
break;
default:
if (key.equals("download.default_directory")) {
chromePrefs.put(key, createFileDownloadDirectory(value.asText()));
} else {
chromePrefs.put(key, value.asText());
}
}
}
chromeOptions.setExperimentalOption("prefs", chromePrefs);
chromeOptions.setHeadless(TestConfigHelper.get().isHeadless());
return chromeOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public interface ConfiguredDriver {

WebDriver getLocalDriver() throws IOException;
WebDriver getRemoteDriver();
<T> T getOptions();
WebDriver getRemoteDriver() throws IOException;
<T> T getOptions() throws IOException;

/**
Expand All @@ -24,7 +24,7 @@ public interface ConfiguredDriver {
*/
default EventFiringWebDriver getDriver(File screenshotPath) throws IOException {
WebDriver webDriver;
switch(TestConfigHelper.get().getRunType()) {
switch (TestConfigHelper.get().getRunType()) {
case LOCAL:
webDriver = getLocalDriver();
break;
Expand Down Expand Up @@ -55,10 +55,22 @@ default EventFiringWebDriver configureEventFiringWebDriver(
}

/**
*
* @throws IOException if the log directory cannot be created
*/
default void createLogDirectory() throws IOException {
FileUtils.forceMkdir(new File("./logs"));
}

/**
*
* @param path runtime browser files download directory path
* @return Absolute file download path
* @throws IOException if the required directory cannot be created
*/
default String createFileDownloadDirectory(String path) throws IOException {
String canonicalPath = new File(path).getCanonicalPath();
FileUtils.forceMkdir(new File(canonicalPath));
return canonicalPath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
public class ConfiguredFirefoxDriver implements ConfiguredDriver {

/**
*
* @return WebDriver representing RemoteWebDriver grid
*/
public WebDriver getRemoteDriver() {
public WebDriver getRemoteDriver() throws IOException {
return new RemoteWebDriver(
TestConfigHelper.get().getGridConfig().getGridUrl(), this.getOptions());
}
Expand All @@ -40,7 +39,7 @@ public WebDriver getLocalDriver() throws IOException {
*
* @return configured options object for target browser driver
*/
public FirefoxOptions getOptions() {
public FirefoxOptions getOptions() throws IOException {
FirefoxOptions firefoxOptions = new FirefoxOptions();
Iterator<Map.Entry<String, JsonNode>> firefoxPreferences = TestConfigHelper.get()
.getBrowserPreferences(BrowserType.FIREFOX)
Expand All @@ -58,7 +57,11 @@ public FirefoxOptions getOptions() {
firefoxOptions.addPreference(key, value.asInt());
break;
default:
firefoxOptions.addPreference(key, value.asText());
if (key.equals("browser.download.dir")) {
firefoxOptions.addPreference(key, createFileDownloadDirectory(value.asText()));
} else {
firefoxOptions.addPreference(key, value.asText());
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class ConfiguredChromeDriverTests {
Expand All @@ -18,4 +22,19 @@ public void testReturnsLocalWebDriver() throws IOException {
WebDriver webDriver = configuredChromeDriver.getDriver(FileUtils.getTempDirectory());
assertThat(webDriver, instanceOf(EventFiringWebDriver.class));
}

@Test
public void testGetOptionsReturnsOptionsIncludedInChromeConfig() throws IOException {
ConfiguredChromeDriver configuredChromeDriver = new ConfiguredChromeDriver();
Map<String, Object> chromeOptions = getOptions(configuredChromeDriver.getOptions());
String expectedFileDownLoadPath = new File("run-generated-files/chrome/downloads").getCanonicalPath();
assertThat(chromeOptions.get("profile.default_content_settings.popups"), is(0));
assertThat(chromeOptions.get("download.default_directory"), is(expectedFileDownLoadPath));
assertThat(chromeOptions.get("safebrowsing.enabled"), is(true));
}

private Map<String, Object> getOptions(ChromeOptions options) {
Map<String, Object> googleChromeOptions = (Map<String, Object>) options.asMap().get("goog:chromeOptions");
return (Map<String, Object>) googleChromeOptions.get("prefs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.io.File;
import java.io.IOException;
import java.util.Map;

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

@Test
public void testGetOptionsReturnsOptionsIncludedInFireFoxConfig() {
public void testGetOptionsReturnsOptionsIncludedInFireFoxConfig() throws IOException {
ConfiguredDriver configuredFirefoxDriver = new ConfiguredFirefoxDriver();
Map<String, Object> firefoxPreferences = getPreferences(configuredFirefoxDriver.getOptions());
assertThat(firefoxPreferences.get("pdfjs.disabled"), is(true));
String expectedFileDownLoadPath = new File("run-generated-files/firefox/downloads").getCanonicalPath();
assertThat(firefoxPreferences.get("browser.download.folderList"), is(2));
assertThat(firefoxPreferences.get("browser.download.defaultFolder"), is("downloads/reports"));
assertThat(firefoxPreferences.get("browser.helperApps.alwaysAsk.force"), is(false));
assertThat(firefoxPreferences.get("browser.download.manager.showWhenStarting"), is(false));
assertThat(firefoxPreferences.get("browser.helperApps.neverAsk.saveToDisk"), is("application/pdf, application/octet-stream"));
assertThat(firefoxPreferences.get("pdfjs.disabled"), is(true));
assertThat(firefoxPreferences.get("unhandled_prompt_behaviour"), is(false));
assertThat(firefoxPreferences.get("browser.download.dir"), is(expectedFileDownLoadPath));
}

private Map<String, Object> getPreferences (FirefoxOptions options) {
Map<String, Object> moxOptions = (Map<String, Object>)options.asMap().get("moz:firefoxOptions");
return (Map<String, Object>)moxOptions.get("prefs");
private Map<String, Object> getPreferences(FirefoxOptions options) {
Map<String, Object> moxOptions = (Map<String, Object>) options.asMap().get("moz:firefoxOptions");
return (Map<String, Object>) moxOptions.get("prefs");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package uk.co.evoco.webdriver.configuration.driver;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import uk.co.evoco.exceptions.SauceLabsCredentialsException;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class ConfiguredSauceLabsDriverTests {

@Test
public void testCanRunTestOnSauceLabsUsingConfigiration() throws SauceLabsCredentialsException{
public void testCanRunTestOnSauceLabsUsingConfigiration() throws SauceLabsCredentialsException, IOException {
System.setProperty("config", "config-saucelabs.json");
ConfiguredDriver configuredSauceLabsGridDriver = new ConfiguredSauceLabsGridDriver();
WebDriver webDriver = configuredSauceLabsGridDriver.getRemoteDriver();
webDriver.get("https://www.evoco.co.uk");
assertThat("Didn't make it to evoco.co.uk", webDriver.getCurrentUrl(), containsString("evoco.co.uk"));
webDriver.quit();
}

@AfterAll
public static void tearDown() {
System.setProperty("config", "DEFAULT");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.co.evoco.webdriver.utils;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import uk.co.evoco.webdriver.configuration.driver.ConfiguredChromeDriver;

import java.io.File;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class ChromeDriverPreferenceTests {

private static String baseUrl;
private static EmbeddedJetty embeddedJetty;
private WebDriver webDriver;

@BeforeAll
public static void webDriverSetup() throws Exception {
embeddedJetty = new EmbeddedJetty();
embeddedJetty.start();
baseUrl = "http://localhost:" + embeddedJetty.getPort() + "/index.html";
}

@Test
public void testChromeBrowserPreferencesApplied() throws Exception {
System.setProperty("config", "fixtures/sample-config-with-chrome-preferences.json");
webDriver = new ConfiguredChromeDriver().getDriver(FileUtils.getTempDirectory());
webDriver.get(baseUrl);
String expectedFile = new File("run-generated-files/chrome/downloads").getCanonicalPath() + "/sampleFile.pdf";
webDriver.findElement(By.xpath("//a[text()='clickHereToDownLoadAFile']")).click();
Thread.sleep(2000);//need to wait until file download
assertThat(new File(expectedFile).exists(), is(true));
}

@AfterEach
public void tearDown() {
this.webDriver.quit();
}

@AfterAll
public static void webDriverTearDown() throws Exception {
System.setProperty("config", "DEFAULT");
embeddedJetty.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package uk.co.evoco.webdriver.utils;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import uk.co.evoco.webdriver.configuration.driver.ConfiguredFirefoxDriver;

import java.io.File;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class FirefoxDriverPreferenceTests {

private static String baseUrl;
private static EmbeddedJetty embeddedJetty;
private WebDriver webDriver;

@BeforeAll
public static void webDriverSetup() throws Exception {
embeddedJetty = new EmbeddedJetty();
embeddedJetty.start();
baseUrl = "http://localhost:" + embeddedJetty.getPort() + "/index.html";
}

@Test
public void testFirefoxBrowserPreferencesApplied() throws Exception {
System.setProperty("config", "fixtures/sample-config-with-firefox-preferences.json");
webDriver = new ConfiguredFirefoxDriver().getDriver(FileUtils.getTempDirectory());
webDriver.get(baseUrl);
String expectedFile = new File("run-generated-files/firefox/downloads").getCanonicalPath() + "/sampleFile.pdf";
webDriver.findElement(By.xpath("//a[text()='clickHereToDownLoadAFile']")).click();
Thread.sleep(2000);//need to wait until file download
assertThat(new File(expectedFile).exists(), is(true));
}

@AfterEach
public void tearDown() {
this.webDriver.quit();
}

@AfterAll
public static void webDriverTearDown() throws Exception {
System.setProperty("config", "DEFAULT");
embeddedJetty.stop();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.co.evoco.webdriver.utils.pageobjects;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -33,4 +34,9 @@ public void testCanUseBasePageObject() throws IOException {
samplePageObject.fillTextBox();
assertThat("Text incorrect", samplePageObject.getTextBoxContents(), is("Hello from sample page object"));
}

@AfterAll
public static void webDriverTearDown() throws Exception {
embeddedJetty.stop();
}
}
Loading

0 comments on commit e90b3ae

Please sign in to comment.