Skip to content

Commit

Permalink
Add browser profiles to Gradle project (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbabcoc authored Jan 15, 2023
1 parent f5987c5 commit 1dcfd17
Show file tree
Hide file tree
Showing 27 changed files with 393 additions and 141 deletions.
34 changes: 33 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.gradle.internal.os.OperatingSystem
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
Expand All @@ -20,6 +21,15 @@ if (!project.hasProperty('profile')) {

apply from: "${profile}Deps.gradle"

if (project.hasProperty('browsers')) {
ext.browsers.split(',').each {
def browser = it.trim()
if (browser) {
apply from: "${browser}Deps.gradle"
}
}
}

def archiveVer = null
def verBits = scmVersion.version.split('-')
def seleniumApi = 's' + profile.charAt(8)
Expand Down Expand Up @@ -112,12 +122,26 @@ task testNG(type: Test) {
useTestNG()
reports.html.destination = file("${buildDir}/reports/testng")
testLogging.showStandardStreams = true

systemProperty 'selenium.grid.plugins', System.getProperty('selenium.grid.plugins')
systemProperty 'selenium.browser.name', System.getProperty('selenium.browser.name')
systemProperty 'selenium.browser.caps', getBrowserCaps()
systemProperty 'selenium.context.platform', System.getProperty('selenium.context.platform', 'support')
systemProperty 'selenium.grid.examples', System.getProperty('selenium.grid.examples', 'true')
systemProperty 'appium.with.pm2', System.getProperty('appium.with.pm2', 'false')
}

test {
dependsOn testNG
reports.html.destination = file("${buildDir}/reports/junit")
testLogging.showStandardStreams = true

systemProperty 'selenium.grid.plugins', System.getProperty('selenium.grid.plugins')
systemProperty 'selenium.browser.name', System.getProperty('selenium.browser.name')
systemProperty 'selenium.browser.caps', getBrowserCaps()
systemProperty 'selenium.context.platform', System.getProperty('selenium.context.platform', 'support')
systemProperty 'selenium.grid.examples', System.getProperty('selenium.grid.examples', 'true')
systemProperty 'appium.with.pm2', System.getProperty('appium.with.pm2', 'false')
}

scmVersion {
Expand Down Expand Up @@ -213,7 +237,7 @@ dependencies {
api 'com.nordstrom.tools:junit-foundation:17.0.2'
api 'com.github.sbabcoc:logback-testng:2.0.0'
api 'org.hamcrest:hamcrest-core:2.2'
api 'org.yaml:snakeyaml:1.32'
api 'org.yaml:snakeyaml:1.33'
}
api 'com.nordstrom.tools:java-utils'
api 'com.nordstrom.tools:settings'
Expand All @@ -228,3 +252,11 @@ dependencies {
test {
jvmArgs "-javaagent:${classpath.find { it.name.contains('junit-foundation') }.absolutePath}"
}

def getBrowserCaps() {
def browserCaps = System.getProperty('selenium.browser.caps')
if (browserCaps && OperatingSystem.current().isWindows()) {
return browserCaps.replaceAll('"', '\\\\"')
}
return browserCaps
}
9 changes: 9 additions & 0 deletions chromeDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.ChromePlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'chrome')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:selenium-chrome-driver') {
exclude module: 'selenium-remote-driver'
}
}
9 changes: 9 additions & 0 deletions edgeDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.EdgePlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'MicrosoftEdge')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:selenium-edge-driver') {
exclude module: 'selenium-remote-driver'
}
}
12 changes: 12 additions & 0 deletions espressoDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.EspressoPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.caps', '{"platformName":"Android","appium:automationName":"Espresso","appium:forceEspressoRebuild":true,"appium:showGradleLog":true,"appium:app":"https://github.com/appium/appium/raw/master/packages/appium/sample-code/apps/ApiDemos-debug.apk"}')
System.setProperty('selenium.context.platform', 'android')
System.setProperty('selenium.grid.examples', 'false')
System.setProperty('appium.with.pm2', 'true')
dependencies {
testImplementation('io.appium:java-client') {
exclude group: 'org.seleniumhq.selenium', module: 'selenium-java'
exclude group: 'org.seleniumhq.selenium', module: 'selenium-support'
}
}
9 changes: 9 additions & 0 deletions firefoxDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.FirefoxPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'firefox')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:selenium-firefox-driver') {
exclude module: 'selenium-remote-driver'
}
}
3 changes: 0 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
profile=selenium3
version=26.7.0-SNAPSHOT
org.gradle.java.installations.auto-detect=false
org.gradle.java.installations.auto-download=false
org.gradle.java.installations.fromEnv=JDK8_HOME,JDK11_HOME
9 changes: 9 additions & 0 deletions htmlunitDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.HtmlUnitPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'htmlunit')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:htmlunit-driver') {
exclude module: 'selenium-support'
}
}
12 changes: 12 additions & 0 deletions mac2Deps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.Mac2Plugin' + File.pathSeparator)
System.setProperty('selenium.browser.caps', '{"platformName":"Mac","appium:automationName":"Mac2","appium:bundleId":"com.apple.TextEdit"}')
System.setProperty('selenium.context.platform', 'mac-app')
System.setProperty('selenium.grid.examples', 'false')
System.setProperty('appium.with.pm2', 'true')
dependencies {
testImplementation('io.appium:java-client') {
exclude group: 'org.seleniumhq.selenium', module: 'selenium-java'
exclude group: 'org.seleniumhq.selenium', module: 'selenium-support'
}
}
9 changes: 9 additions & 0 deletions operaDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.OperaPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'opera')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:selenium-opera-driver') {
exclude module: 'selenium-remote-driver'
}
}
9 changes: 9 additions & 0 deletions phantomjsDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.PhantomJsPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'phantomjs')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('com.codeborne:phantomjsdriver') {
exclude group: 'org.seleniumhq.selenium', module: 'selenium-remote-driver'
}
}
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<clean-plugin.version>3.2.0</clean-plugin.version>
<hamcrest.version>2.2</hamcrest.version>
<!-- managed to resolve identified threat -->
<snakeyaml.version>1.32</snakeyaml.version>
<snakeyaml.version>1.33</snakeyaml.version>
<chromePlugin />
<edgePlugin />
<espressoPlugin />
Expand Down Expand Up @@ -408,6 +408,12 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
Expand Down
9 changes: 9 additions & 0 deletions safariDeps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def driverPlugins = System.getProperty('selenium.grid.plugins', '')
System.setProperty('selenium.grid.plugins', driverPlugins + 'com.nordstrom.automation.selenium.plugins.SafariPlugin' + File.pathSeparator)
System.setProperty('selenium.browser.name', 'safari')
System.setProperty('selenium.context.platform', 'web-app')
dependencies {
testImplementation('org.seleniumhq.selenium:selenium-safari-driver') {
exclude module: 'selenium-remote-driver'
}
}
7 changes: 7 additions & 0 deletions selenium3Deps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ dependencies {
api 'org.eclipse.jetty.websocket:websocket-client:9.4.46.v20220331'
api 'org.jetbrains.kotlin:kotlin-stdlib:1.7.0'
api 'org.jetbrains.kotlin:kotlin-stdlib-common:1.7.0'
testImplementation 'org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:selenium-edge-driver:3.141.59'
testImplementation 'io.appium:java-client:7.6.0'
testImplementation 'org.seleniumhq.selenium:selenium-firefox-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:selenium-opera-driver:3.141.59'
testImplementation 'com.codeborne:phantomjsdriver:1.4.4'
testImplementation 'org.seleniumhq.selenium:selenium-safari-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:htmlunit-driver:2.67.0'
testImplementation 'org.mockito:mockito-core:4.6.1'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ public abstract class AbstractSeleniumConfig extends
SettingsCore<AbstractSeleniumConfig.SeleniumSettings> {

private static final String SETTINGS_FILE = "settings.properties";
private static final String CAPS_PATTERN = "{\"browserName\":\"%s\"}";

/** value: <b>{"browserName":"htmlunit"}</b> */
private static final String DEFAULT_CAPS = String.format(CAPS_PATTERN, "htmlunit");

protected static final String NODE_MODS_SUFFIX = ".node.mods";
private static final String CAPS_MODS_SUFFIX = ".caps.mods";

Expand Down Expand Up @@ -222,9 +218,9 @@ public enum SeleniumSettings implements SettingsCore.SettingsAPI {
* requests. This can be either a file path (absolute, relative, or simple filename) or a direct value.
* <p>
* name: <b>selenium.browser.caps</b><br>
* default: {@link #DEFAULT_CAPS}
* default: {@code null}
*/
BROWSER_CAPS("selenium.browser.caps", DEFAULT_CAPS),
BROWSER_CAPS("selenium.browser.caps", null),

/**
* This setting specifies the maximum allowed interval for a page to finish loading.
Expand Down Expand Up @@ -735,9 +731,9 @@ public Capabilities getCurrentCapabilities() {
Capabilities capabilities = null;
String browserName = getString(SeleniumSettings.BROWSER_NAME.key());
String browserCaps = resolveString(SeleniumSettings.BROWSER_CAPS.key());
if (browserName != null) {
if (!Strings.isNullOrEmpty(browserName)) {
capabilities = getSeleniumGrid().getPersonality(getConfig(), browserName);
} else if (browserCaps != null) {
} else if (!Strings.isNullOrEmpty(browserCaps)) {
capabilities = getCapabilitiesForJson(browserCaps)[0];
} else {
throw new IllegalStateException("Neither browser name nor capabilities are specified");
Expand Down Expand Up @@ -799,7 +795,7 @@ protected Capabilities getModifications(final Capabilities capabilities, final S
* @return list of {@link Capabilities} objects
*/
public Capabilities[] getCapabilitiesForName(final String browserName) {
return getCapabilitiesForJson(String.format(CAPS_PATTERN, browserName));
return getCapabilitiesForJson(String.format("{\"browserName\":\"%s\"}", browserName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import com.nordstrom.common.base.UncheckedThrow;

/**
* <h1>The {@code SeleniumGrid} Object</h1>
* <b>The {@code SeleniumGrid} Object</b>
* <p>
* The <b>{@code SeleniumGrid}</b> object provides an interface to
* <a href='https://github.com/SeleniumHQ/selenium/wiki/Grid2'>Selenium Grid</a> collections - both local and remote.
* A standard grid object is available through the configuration, and independent instances can be created as needed.
*
* <h2>Using the standard {@code SeleniumGrid} object</h2>
* <p>
* <b>Using the standard {@code SeleniumGrid} object</b>
* <p>
* By default, <b>Selenium Foundation</b> acquires its browser sessions from an instance of the
* <a href='https://seleniumhq.github.io/docs/grid.html'>Selenium Grid</a>. If no remote Grid instance is specified in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.nordstrom.automation.selenium.SeleniumConfig;
import com.nordstrom.automation.selenium.annotations.PageUrl;
import com.nordstrom.automation.selenium.core.ByType;
import com.nordstrom.automation.selenium.core.JsUtility;
import com.nordstrom.automation.selenium.model.Page;
import com.nordstrom.automation.selenium.model.RobustWebElement;

Expand Down Expand Up @@ -201,6 +202,10 @@ public boolean setInputValue(String value) {
return updateValue(findElement(Using.INPUT), value);
}

public boolean setInputValue(boolean value) {
return updateValue(findElement(Using.INPUT), value);
}

public String getInputValue() {
return findElement(Using.INPUT).getAttribute("value");
}
Expand All @@ -217,6 +222,14 @@ public boolean setCheckValue(boolean value) {
return updateValue(findElement(Using.CHECK), value);
}

public boolean setCheckValue(String value) {
return updateValue(findElement(Using.CHECK), value);
}

public void resetForm() {
JsUtility.run(driver, "document.getElementById('form').reset()");
}

public boolean hasCssOptional() {
return findOptional(By.cssSelector(ByType.cssLocatorFor(Using.FORM))).hasReference();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ public static boolean updateValue(final WebElement element, final String value)
if ("checkbox".equals(element.getAttribute("type"))) {
return updateValue(element, Boolean.parseBoolean(value));
} else if (!valueEquals(element, value)) {
if (value == null) {
element.clear();
} else {
WebDriverUtils.getExecutor(element).executeScript("arguments[0].select();", element);
element.clear();
if (value != null) {
element.sendKeys(value);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nordstrom.automation.selenium.platform;

interface TargetTypeName extends PlatformEnum {
public interface TargetTypeName extends PlatformEnum {
String SUPPORT_NAME = "support";
String WEB_APP_NAME = "web-app";
String ANDROID_NAME = "android";
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/ExamplePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
var div_a = document.createElement('div');
div_a.id = 'form-div-a';
var form_a = document.createElement('form');
form_a.id = 'form_a';
var label_1_a = document.createElement('label');
label_1_a.for = 'input-field-a';
label_1_a.textContent = '[A] Input: ';
var input_field_a = document.createElement('input');
input_field_a.type = 'text';
input_field_a.id = 'input-field-a';
input_field_a.name = 'input-field-a';
input_field_a.value = 'Poshmark';
var label_2_a = document.createElement('label');
label_2_a.for = 'checkbox-a';
label_2_a.textContent = '[A] Check: ';
Expand Down Expand Up @@ -87,13 +89,15 @@
var div_b = document.createElement('div');
div_b.id = 'form-div-b';
var form_b = document.createElement('form');
form_b.id = 'form_b';
var label_1_b = document.createElement('label');
label_1_b.for = 'input-field-b';
label_1_b.textContent = '[B] Input: ';
var input_field_b = document.createElement('input');
input_field_b.type = 'text';
input_field_b.id = 'input-field-b';
input_field_b.name = 'input-field-b';
input_field_b.value = 'RealReal';
var label_2_b = document.createElement('label');
label_2_b.for = 'checkbox-b';
label_2_b.textContent = '[B] Check: ';
Expand Down Expand Up @@ -145,9 +149,9 @@
</td>
<td>
<div id="form-div">
<form action="/action_page.php">
<form id="form" action="/action_page.php">
<label for="input-field">Input: </label>
<input type="text" name="input-field" id="input-field"><br>
<input type="text" name="input-field" id="input-field" value="Nordstrom"><br>
<label for="checkbox">Check: </label>
<input type="checkbox" name="checkbox" id="checkbox" value="checkbox"><br>
<input type="submit" value="Submit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static com.nordstrom.automation.selenium.platform.TargetTypeName.SUPPORT_NAME;

import java.net.URI;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.SearchContext;
import org.testng.SkipException;
import org.testng.annotations.Test;

import com.nordstrom.automation.selenium.AbstractSeleniumConfig.SeleniumSettings;
Expand Down Expand Up @@ -59,6 +61,11 @@ public void testTargetUri() {
@Test
public void testBrowserCaps() {
SeleniumConfig config = SeleniumConfig.getConfig();
String contextPlatform = config.getContextPlatform();
if ((contextPlatform == null) || contextPlatform.equals(SUPPORT_NAME)) {
throw new SkipException("Current target platform doesn't provide browser capabilities");
}

Capabilities browserCaps = config.getCurrentCapabilities();
assertNotNull(browserCaps.getBrowserName());
}
Expand Down
Loading

0 comments on commit 1dcfd17

Please sign in to comment.