Skip to content
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

Selenium grid #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.opencredo.test.security.acceptance.test.config;
package com.opencredo.test.ui.acceptance.test.config;

import java.util.HashMap;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.opencredo.test.security.acceptance.test.config;
package com.opencredo.test.ui.acceptance.test.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
Expand All @@ -17,4 +17,16 @@ public String getApplicationBaseUrl() {
public int getSeleniumWaitTimeOutSeconds() {
return environment.getProperty("ui.acceptance.test.selenium.wait.timeout.seconds", Integer.class, 10);
}

public boolean isGridEnabled(){
return Boolean.valueOf(environment.getProperty("ui.acceptance.test.selenium.grid.enabled"));
}

public String getDefaultBrowser(){
return environment.getProperty("ui.acceptance.test.selenium.default.browser");
}

public String getDefaultHubUrl(){
return environment.getProperty("ui.acceptance.test.selenium.default.hub.url");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.opencredo.test.security.acceptance.test.config.spring;
package com.opencredo.test.ui.acceptance.test.config.spring;

import com.opencredo.test.security.acceptance.test.config.TestWorld;
import com.opencredo.test.security.acceptance.test.config.UiTestProperties;
import com.opencredo.test.security.acceptance.test.interaction.objects.GoogleSearchPageObject;
import com.opencredo.test.security.acceptance.test.config.webdriver.SharedDriver;
import com.opencredo.test.ui.acceptance.test.interaction.objects.GoogleSearchPageObject;
import com.opencredo.test.ui.acceptance.test.config.TestWorld;
import com.opencredo.test.ui.acceptance.test.config.UiTestProperties;
import com.opencredo.test.ui.acceptance.test.config.webdriver.SharedDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
Expand All @@ -19,6 +19,8 @@ public TestWorld testWorld() {

@Bean
public SharedDriver driver() {
if (properties().isGridEnabled())
return new SharedDriver(properties().getDefaultBrowser(), properties().getDefaultHubUrl());
return new SharedDriver();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.opencredo.test.ui.acceptance.test.config.webdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

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

/**
* Creates a browser session which can be used to interact with a web UI.
* This class allows for a single session to be re-used across multiple
* test cases for increased speed.
*/
public class SharedDriver extends EventFiringWebDriver {
private static WebDriver REAL_DRIVER;
private static final Thread CLOSE_THREAD = new Thread() {

@Override
public void run() {
quitGlobalInstance();
}
};
public WebDriver driver;

private static void quitGlobalInstance() {
WebDriver driver = REAL_DRIVER;
REAL_DRIVER = null;
if (driver != null) {
driver.quit();
}
}

private static WebDriver getRealDriver(String defaultBrowser,String defaultHubUrl) {
if (REAL_DRIVER == null) {
switch (defaultBrowser){
case "firefox":
try {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
REAL_DRIVER = new RemoteWebDriver(new URL(defaultHubUrl), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
break;
case "chrome":
try {
DesiredCapabilities capability = DesiredCapabilities.chrome();
REAL_DRIVER = new RemoteWebDriver(new URL(defaultHubUrl), capability);
} catch (MalformedURLException e) {
e.printStackTrace();
}
break;
default:REAL_DRIVER = new FirefoxDriver();
break;
}
}
return REAL_DRIVER;
}

private static WebDriver getRealDriver() {
if (REAL_DRIVER == null) {
REAL_DRIVER = new FirefoxDriver();
}
return REAL_DRIVER;
}

public SharedDriver(String defaultBrowser,String defaultHubUrl) {
super(getRealDriver(defaultBrowser,defaultHubUrl));
Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}


public SharedDriver() {
super(getRealDriver());
Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}

@Override
public void close() {
if (Thread.currentThread() != CLOSE_THREAD) {
throw new UnsupportedOperationException("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
}
try {
super.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.opencredo.test.security.acceptance.test.config.webdriver;
package com.opencredo.test.ui.acceptance.test.config.webdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.opencredo.test.security.acceptance.test.config.webdriver;
package com.opencredo.test.ui.acceptance.test.config.webdriver;

import com.opencredo.test.security.acceptance.test.config.spring.UiTestConfig;
import com.opencredo.test.ui.acceptance.test.config.spring.UiTestConfig;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.opencredo.test.security.acceptance.test.interaction.objects;
package com.opencredo.test.ui.acceptance.test.interaction.objects;

import com.opencredo.test.security.acceptance.test.config.webdriver.WaitConditions;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import com.opencredo.test.ui.acceptance.test.config.webdriver.WaitConditions;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
* Reusable methods for all page objects
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.opencredo.test.security.acceptance.test.interaction.objects;
package com.opencredo.test.ui.acceptance.test.interaction.objects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.opencredo.test.security.acceptance.test.step.definitions;
package com.opencredo.test.ui.acceptance.test.step.definitions;

import com.opencredo.test.security.acceptance.test.config.TestWorld;
import com.opencredo.test.security.acceptance.test.config.spring.UiTestConfig;
import com.opencredo.test.ui.acceptance.test.config.TestWorld;
import com.opencredo.test.ui.acceptance.test.config.spring.UiTestConfig;
import cucumber.api.Scenario;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.opencredo.test.security.acceptance.test.step.definitions;
package com.opencredo.test.ui.acceptance.test.step.definitions;

import com.opencredo.test.security.acceptance.test.interaction.objects.GoogleSearchPageObject;
import com.opencredo.test.ui.acceptance.test.interaction.objects.GoogleSearchPageObject;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Properties can be overwritten at runtime by passing in maven arguments using -D
# e.g. mvn clean install -Dui.acceptance.test.application.base.url=https:/www.google.com
ui.acceptance.test.application.base.url=https://www.google.co.uk
ui.acceptance.test.selenium.wait.timeout.seconds=10
ui.acceptance.test.selenium.wait.timeout.seconds=10
ui.acceptance.test.selenium.grid.enabled=false
ui.acceptance.test.selenium.default.browser=firefox
ui.acceptance.test.selenium.default.hub.url=http://127.0.0.1:4444/wd/hub