Skip to content

Commit

Permalink
Selenium grid support (#118)
Browse files Browse the repository at this point in the history
* grid first attempt

* selenium grid support

* selenium grid support - file upload fix; some browserstack support
  • Loading branch information
jerzykrlkroche authored Oct 23, 2024
1 parent 5c89df1 commit b59ef19
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build/*
*.iws
*.iml
*.ipr
**/application-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GebHomepageSpec extends SpockGebQuickstartSpec {

def "can access The Book of Geb via homepage"() {
given:
driver.manage().window().maximize()
to GebHomePage

when:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
com.roche.spock.geb:
base-url: http://gebish.org
# change to "docker" to enable Docker-based browser execution
# browser-type: docker
browser-type: browserstack
browser:
browser-stack:
username: [TODO]
access-key: [TODO]
browser-name: Chrome
options:
os: Windows
osVersion: 10
browserVersion: 125
resolution: 1920x1080
projectName: Geb Spock
debug: true
networkLogs: true
telemetryLogs: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
com.roche.spock.geb:
base-url: http://gebish.org
# change to "docker" to enable Docker-based browser execution
# browser-type: docker
browser-type: grid
browser:
grid-address: http://127.0.0.1:5555
arguments:
- --headless
2 changes: 2 additions & 0 deletions spock-geb-quickstart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ dependencies {

api "org.gebish:geb-spock:7.0"

api 'com.browserstack:browserstack-local-java:1.1.1'

api group: 'org.spockframework', name: 'spock-core', version: '2.4-M4-groovy-4.0'
api group: 'org.spockframework', name: 'spock-spring', version: '2.4-M4-groovy-4.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ reportingListener = new GebReportingListener()
reportsDir = 'build/geb-spock-reports'
reportOnTestFailureOnly = false

quitDriverOnBrowserReset = true

// defaults
driver = { BrowserConfiguration.getChromeDriver() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</layout>
</appender>

<root level="INFO">
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ abstract class SpockGebQuickstartSpec extends GebReportingSpec {
@Autowired
SpockGebQuickstartConfiguration spockGebQuickstartConfiguration

def setupSpec() {
// browser.setDriver(null)
}

def setup() {
browser.baseUrl = spockGebQuickstartConfiguration.baseUrl
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.roche.spock.geb.config;

import geb.driver.BrowserStackDriverFactory;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.chrome.ChromeDriver;
Expand All @@ -35,9 +36,13 @@
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static java.time.temporal.ChronoUnit.SECONDS;
Expand Down Expand Up @@ -70,11 +75,27 @@ public RemoteWebDriver getRemoteWebDriver(WebDriverManager webDriverManager,
SpockGebQuickstartConfiguration spockGebQuickstartConfiguration) {
if (spockGebQuickstartConfiguration.getBrowserType() == BrowserType.docker) {
return testContainersChromeDriver(webDriverManager, spockGebQuickstartConfiguration);
} else if (spockGebQuickstartConfiguration.getBrowserType() == BrowserType.grid) {
return gridChromeDriver(spockGebQuickstartConfiguration);
} else if (spockGebQuickstartConfiguration.getBrowserType() == BrowserType.browserstack) {
return browserStackChromeDriver(spockGebQuickstartConfiguration);
} else {
return chromeDriver(webDriverManager, spockGebQuickstartConfiguration);
}
}

private RemoteWebDriver browserStackChromeDriver(SpockGebQuickstartConfiguration spockGebQuickstartConfiguration) {
BrowserStackConfiguration browserStack = spockGebQuickstartConfiguration.getBrowser().getBrowserStack();
Map<String, Object> capabilities = new HashMap<>();
capabilities.put("browserName", browserStack.getBrowser());
Map<String, Object> browserStackOptions = browserStack.getOptions();
if (browserStackOptions != null) {
capabilities.put("bstack:options", browserStackOptions);
}
return (RemoteWebDriver) new BrowserStackDriverFactory()
.create(browserStack.getUsername(), browserStack.getAccessKey(), capabilities);
}

public RemoteWebDriver chromeDriver(WebDriverManager webDriverManager,
SpockGebQuickstartConfiguration spockGebQuickstartConfiguration) {

Expand Down Expand Up @@ -146,4 +167,15 @@ public static RemoteWebDriver getChromeDriver() {
return applicationContext.getBean(RemoteWebDriver.class);
}

}
private RemoteWebDriver gridChromeDriver(SpockGebQuickstartConfiguration spockGebQuickstartConfiguration) {
ChromeOptions chromeOptions = new ChromeOptions();

if (spockGebQuickstartConfiguration.getBrowser() != null) {
chromeOptions.addArguments(spockGebQuickstartConfiguration.getBrowser().getArguments());
}

RemoteWebDriver remoteWebDriver = new RemoteWebDriver(spockGebQuickstartConfiguration.getBrowser().getGridAddress(), chromeOptions);
remoteWebDriver.setFileDetector(new LocalFileDetector());
return remoteWebDriver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.roche.spock.geb.config;

import java.util.Map;

public class BrowserStackConfiguration {

private final String username;
private final String accessKey;
private final String local;
private final String localIdentifier;
private final String build;
private final String name;
private final String browser = "chrome";
private final String os = "Windows";
private final String osVersion = "10";
private final String browserVersion = "125";
private final Map<String, Object> options;
private final String browserName;


public BrowserStackConfiguration(String username, String accessKey, String local, String localIdentifier, String build, String name, Map<String, Object> options, String browserName) {
this.username = username;
this.accessKey = accessKey;
this.local = local;
this.localIdentifier = localIdentifier;
this.build = build;
this.name = name;
this.options = options;
this.browserName = browserName;
}

public String getUsername() {
return username;
}

public String getAccessKey() {
return accessKey;
}

public String isLocal() {
return local;
}

public String getLocalIdentifier() {
return localIdentifier;
}

public String getBuild() {
return build;
}

public String getName() {
return name;
}

public String getBrowser() {
return browser;
}

public String getOs() {
return os;
}

public String getOsVersion() {
return osVersion;
}

public String getBrowserVersion() {
return browserVersion;
}

public Map<String, Object> getOptions() {
return options;
}

public String getBrowserName() {
return browserName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.roche.spock.geb.config;

public enum BrowserType {
local, docker
local, docker, grid, browserstack
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -16,7 +17,13 @@ public class SpockGebQuickstartBrowserConfiguration {

private final String binary;

public SpockGebQuickstartBrowserConfiguration(List<String> arguments, String dockerImageName, String binary) {
private final URL gridAddress;

private final BrowserStackConfiguration browserStack;

public SpockGebQuickstartBrowserConfiguration(List<String> arguments, String dockerImageName, String binary, URL gridAddress, BrowserStackConfiguration browserStack) {
this.gridAddress = gridAddress;
this.browserStack = browserStack;

if (arguments != null) {
this.arguments = Collections.unmodifiableList(new ArrayList<>(arguments));
Expand Down Expand Up @@ -44,4 +51,13 @@ public String getDockerImageName() {
public String getBinary() {
return binary;
}

public URL getGridAddress() {
return gridAddress;
}

public BrowserStackConfiguration getBrowserStack() {
return browserStack;
}

}

0 comments on commit b59ef19

Please # to comment.