Skip to content

Commit

Permalink
Big changeset here:
Browse files Browse the repository at this point in the history
Features/fixes/changes:
- BaseAbstractTest - amended to as the config is now a singleton
- u.c.e.w.configuration.Configured* - added interface and implementations for each driver to allow us to use the grid configuration that comes from the config file and only to download the binaries if we're local
- ConfigurationLoader - removed some of the options for the builder so we can use it in the TestConfigManager singleton
- TestConfigManger - a singleton to make the configuration available anywhere and to only have one instance of it
- GridConfig - data model to represent the object in the config file
- RunType - enum for local or grid runs
- WebDriverConfig - some additiona fields for exception tolerance, grid, run type and generic test config
- ClickUtils - a tolerant click that handles the exceptions passed in the config file and retries once
- RadioButtonUtils - methods to select radio buttons by visible labels
- JavaScriptUtils - added method to handle running javascript on a specific WebElement
- WindowUtils - added scrollIntoView method (which uses new methods in JavaScriptUtils)
- SelectBoxUtils - making itemByIndex work not on 0 based numbers (so that the argument can be presented logically i.e. 1 instead of 0)
- WebDriverBuilder - refactored after the introduction of ConfgiuredDrivers and a TestConfigManager
- WebDriverListener - a few fixes for screenshots - found issue #13 here
- Dates - added a method for getting todays date

Test (see issue #7):
- Added more unit tests
- Added integration tests for browser driver loading locally - these tests are platorm dependant (mac or windows) because of the browsers. They depend on the browsers being installed
- Added integration test for the helper utilities
- Added a small sample web app that is embedded with jetty to allow us to have a sample HTML page to test the helper utilites in bad situations, this is still WIP, and is dependant on port 4442 being open.

Dependencies:
- Added jetty-server and jetty-servlet (probably need to remove jetty-servlet (see issue #14)
- Added surefire plugin to run integration tests - these can now be ran on `mvn verify`
  • Loading branch information
stevewalton28 committed Jan 21, 2020
1 parent 81350a3 commit 588b4ac
Show file tree
Hide file tree
Showing 44 changed files with 1,029 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ bin

*.csv

script/dist
script/dist
logs/*
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist: trusty

language: java

jdk:
Expand All @@ -7,8 +9,18 @@ cache:
directories:
- $HOME/.m2

before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start

before_install:
- "cp .travis.settings.xml $HOME/.m2/settings.xml"
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16"
- google-chrome-stable --remote-debugging-port=9222 http://localhost &

addons:
firefox: latest

install:
- mvn clean install -Dmaven.javadoc.skip=true -B -V
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# selenium-pom-example
# selenium-pom-framework

[![Build Status](https://travis-ci.com/digital-delivery-academy/selenium-pom-framework.svg?branch=master)](https://travis-ci.com/digital-delivery-academy/selenium-pom-framework)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/122f56e1b6284b319b8c23a58ab2c664)](https://www.codacy.com/gh/digital-delivery-academy/selenium-pom-example?utm_source=github.com&utm_medium=referral&utm_content=digital-delivery-academy/selenium-pom-example&utm_campaign=Badge_Grade)
Expand Down
9 changes: 8 additions & 1 deletion config-fs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"browser": "chrome",
"baseUrl": "https://www.yahoo.com",
"timeout": "30"
"timeout": "30",
"headless": true,
"testConfig": {
"sample": "sample text"
},
"gridConfig": {
"url": "http://localhost:4444/wd/hub"
}
}
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<maven.surefire.version>2.22.2</maven.surefire.version>
<maven.failsafe.version>2.22.2</maven.failsafe.version>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<jetty.version>9.4.26.v20200117</jetty.version>
<selenium.version>3.141.59</selenium.version>
<webdriver.manager.version>3.7.1</webdriver.manager.version>
<slf4j.version>1.6.4</slf4j.version>
Expand Down Expand Up @@ -125,6 +127,18 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -136,6 +150,22 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/uk/co/evoco/tests/BaseAbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import uk.co.evoco.webdriver.WebDriverBuilder;
import uk.co.evoco.webdriver.configuration.ConfigurationLoader;
import uk.co.evoco.webdriver.configuration.WebDriverConfig;
import uk.co.evoco.webdriver.configuration.TestConfigManager;
import uk.co.evoco.webdriver.results.ResultsManager;

import java.io.IOException;
Expand All @@ -17,7 +16,6 @@
*/
public abstract class BaseAbstractTest {
protected EventFiringWebDriver webDriver;
protected static WebDriverConfig webDriverConfig;
protected static ResultsManager resultsManager;

/**
Expand All @@ -27,10 +25,7 @@ public abstract class BaseAbstractTest {
* @throws IOException
*/
@BeforeAll
public static void beforeAll() throws IOException {
webDriverConfig = new ConfigurationLoader()
.decideWhichConfigurationToUse()
.build();
public static void beforeAll() {
resultsManager = new ResultsManager();
resultsManager.createScreenshotDirectory();
}
Expand All @@ -41,12 +36,11 @@ public static void beforeAll() throws IOException {
* This ensures we always have a fresh browser window and a guaranteed starting point
*/
@BeforeEach
public void setUp() {
public void setUp() throws IOException {
this.webDriver = new WebDriverBuilder()
.setConfiguration(webDriverConfig)
.setResultsDirectory(this.resultsManager.getScreenshotDirectory())
.build();
this.webDriver.get(webDriverConfig.getBaseUrl());
this.webDriver.get(TestConfigManager.getInstance().getWebDriverConfig().getBaseUrl());
this.webDriver.manage().window().maximize();
}

Expand Down
61 changes: 15 additions & 46 deletions src/main/java/uk/co/evoco/webdriver/WebDriverBuilder.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
package uk.co.evoco.webdriver;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import uk.co.evoco.webdriver.configuration.WebDriverConfig;
import uk.co.evoco.webdriver.configuration.TestConfigManager;
import uk.co.evoco.webdriver.configuration.driver.*;

import java.io.File;
import java.io.IOException;

/**
* This class uses the Builder Pattern to construct its options. Calling .build() will
* result in all of the configuration options being assembled and a valid WebDriver object being
* supplied to the caller
*/
public class WebDriverBuilder {

private WebDriverConfig webDriverConfig;

private File screenshotDirectory;

/**
* Allows caller to provide a WebDriverConfig object. Returns reference to the class instance of WebDriverBuilder
* to maintain the builder pattern.
* @param webDriverConfig
* @return WebDriverBuilder
*
* @param screenshotDirectory
* @return
*/
public WebDriverBuilder setConfiguration(WebDriverConfig webDriverConfig) {
this.webDriverConfig = webDriverConfig;
return this;
}

public WebDriverBuilder setResultsDirectory(File screenshotDirectory) {
this.screenshotDirectory = screenshotDirectory;
return this;
Expand All @@ -43,39 +31,20 @@ public WebDriverBuilder setResultsDirectory(File screenshotDirectory) {
* construct an EventFiringWebDriver with the correct configuration for the browser type
* @return EventFiringWebDriver
*/
public EventFiringWebDriver build() {
WebDriver webDriver;

switch(this.webDriverConfig.getBrowserType()) {
public EventFiringWebDriver build() throws IOException {
switch(TestConfigManager.getInstance().getWebDriverConfig().getBrowserType()) {
case CHROME:
WebDriverManager.chromedriver().setup();
webDriver = new ChromeDriver();
break;
return new ConfiguredChromeDriver(TestConfigManager.getInstance().getWebDriverConfig()).getDriver(this.screenshotDirectory);
case FIREFOX:
WebDriverManager.firefoxdriver().setup();
webDriver = new FirefoxDriver();
break;
return new ConfiguredFirefoxDriver(TestConfigManager.getInstance().getWebDriverConfig()).getDriver(this.screenshotDirectory);
case IE:
WebDriverManager.iedriver().setup();
webDriver = new InternetExplorerDriver();
break;
return new ConfiguredInternetExplorerDriver(TestConfigManager.getInstance().getWebDriverConfig()).getDriver(this.screenshotDirectory);
case EDGE:
WebDriverManager.edgedriver().setup();
webDriver = new EdgeDriver();
break;
return new ConfiguredEdgeDriver(TestConfigManager.getInstance().getWebDriverConfig()).getDriver(this.screenshotDirectory);
case SAFARI:
webDriver = new SafariDriver();
break;
return new ConfiguredSafariDriver(TestConfigManager.getInstance().getWebDriverConfig()).getDriver(this.screenshotDirectory);
default:
throw new RuntimeException("WebDriverBuilder has no valid target browser set in WebDriverConfig");
}

EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(webDriver);
WebDriverListener eventListener = new WebDriverListener();
eventListener.setWebdriverWaitTimeout(this.webDriverConfig.getWebDriverWaitTimeout());
eventListener.setScreenshotDirectory(this.screenshotDirectory);
eventFiringWebDriver.register(eventListener);

return eventFiringWebDriver;
}
}
8 changes: 8 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/WebDriverListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ public class WebDriverListener implements WebDriverEventListener {
private static final Logger logger = LoggerFactory.getLogger(WebDriverListener.class);
private File screenshotDirectory;

/**
* Sets the wait for all of the ExpectedConditions calls that we make here
* @param webDriverWaitTimeout
*/
public void setWebdriverWaitTimeout(long webDriverWaitTimeout) {
this.WEBDRIVER_WAIT_TIMEOUT = webDriverWaitTimeout;
}

/**
* Sets the screenshot target directory that will be used for screenshots generated inside onException()
* @param screenshotDirectory
*/
public void setScreenshotDirectory(File screenshotDirectory) {
this.screenshotDirectory = screenshotDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public ConfigurationLoader decideWhichConfigurationToUse() {
* @return WebDriverConfig
* @throws IOException
*/
public WebDriverConfig build() throws IOException {
return JsonUtils.fromFile(FileLoaderUtils.loadFromClasspathOrFileSystem(targetConfigurationFile), WebDriverConfig.class);
public WebDriverConfig build() {
try {
return JsonUtils.fromFile(FileLoaderUtils.loadFromClasspathOrFileSystem(targetConfigurationFile), WebDriverConfig.class);
} catch (IOException e) {
throw new RuntimeException("Unable to load configuration from " + targetConfigurationFile);
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/configuration/GridConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.co.evoco.webdriver.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.net.MalformedURLException;
import java.net.URL;

public class GridConfig {
private URL gridUrl;

public URL getGridUrl() {
return gridUrl;
}

@JsonProperty("url")
public void setGridUrl(String gridUrl) throws MalformedURLException {
this.gridUrl = new URL(gridUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.co.evoco.webdriver.configuration;

public enum RunType {
LOCAL,
GRID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.co.evoco.webdriver.configuration;

public class TestConfigManager {

private static TestConfigManager testConfigManager;
private WebDriverConfig webDriverConfig;

private TestConfigManager() {
this.webDriverConfig = new ConfigurationLoader()
.decideWhichConfigurationToUse()
.build();
}

public static TestConfigManager getInstance() {
if(null == testConfigManager) {
testConfigManager = new TestConfigManager();
}
return testConfigManager;
}

public WebDriverConfig getWebDriverConfig() {
return webDriverConfig;
}
}
Loading

0 comments on commit 588b4ac

Please sign in to comment.