Skip to content

Commit

Permalink
selenium grid support - file upload fix; some browserstack support
Browse files Browse the repository at this point in the history
  • Loading branch information
jerzykrlkroche committed Oct 23, 2024
1 parent 1b30f9a commit 368d22e
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 3 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
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 @@ -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 @@ -40,6 +41,8 @@
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 @@ -74,11 +77,25 @@ public RemoteWebDriver getRemoteWebDriver(WebDriverManager webDriverManager,
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 @@ -157,6 +174,8 @@ private RemoteWebDriver gridChromeDriver(SpockGebQuickstartConfiguration spockGe
chromeOptions.addArguments(spockGebQuickstartConfiguration.getBrowser().getArguments());
}

return new RemoteWebDriver(spockGebQuickstartConfiguration.getBrowser().getGridAddress(), chromeOptions);
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, grid
local, docker, grid, browserstack
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class SpockGebQuickstartBrowserConfiguration {

private final URL gridAddress;

public SpockGebQuickstartBrowserConfiguration(List<String> arguments, String dockerImageName, String binary, 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 @@ -51,4 +55,9 @@ public String getBinary() {
public URL getGridAddress() {
return gridAddress;
}

public BrowserStackConfiguration getBrowserStack() {
return browserStack;
}

}

0 comments on commit 368d22e

Please # to comment.