Skip to content

Commit

Permalink
Use Chrome for testing (#369)
Browse files Browse the repository at this point in the history
Run acceptance tests with Chrome for testing

Since Selenium 4.11.x is used we can leverage their driver manager taking care of finding a suitable Chrome and driver.
  • Loading branch information
alextu authored Nov 23, 2023
1 parent 6c6f584 commit da57983
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 83 deletions.
8 changes: 5 additions & 3 deletions acceptance-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.net.URL

plugins {
java
id("buildlogic.chromedriver")
id("de.undercouch.download") version "5.5.0"
}

Expand Down Expand Up @@ -79,8 +78,11 @@ jenkinsVersions
.withNormalizer(ClasspathNormalizer::class)

onlyIf {
// Do not run on Jenkins since acceptance tests don't work there for some reason, one of them:
// https://github.com/jenkinsci/acceptance-test-harness/issues/1170
//
// Do not run on Windows as written here: https://github.com/jenkinsci/acceptance-test-harness/blob/master/docs/EXTERNAL.md
!OperatingSystem.current().isWindows
!ciJenkinsBuild && !OperatingSystem.current().isWindows
}

javaLauncher.set(javaToolchains.launcherFor {
Expand All @@ -98,7 +100,7 @@ jenkinsVersions
mapOf(
"JENKINS_WAR" to downloadJenkinsTask.get().outputs.files.singleFile,
"LOCAL_JARS" to gradlePlugin.singleFile,
"BROWSER" to if (ciJenkinsBuild) "firefox-container" else "chrome"
"BROWSER" to "chrome"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import hudson.plugin.gradle.ath.updatecenter.VersionOverridesDecorator;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jenkinsci.test.acceptance.guice.TestScope;
import org.jenkinsci.test.acceptance.update_center.UpdateCenterMetadata;
import org.jenkinsci.test.acceptance.update_center.UpdateCenterMetadataProvider;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.util.function.Consumer;
Expand All @@ -31,6 +33,7 @@ protected void configure() {
Matchers.returns(Matchers.subclassesOf(UpdateCenterMetadata.class)),
new ResultDecoratingAdapter<>(new VersionOverridesDecorator())
);
bind(WebDriver.class).toProvider(WebDriverProvider.class).in(TestScope.class);
}

private static class ResultDecoratingAdapter<T> implements MethodInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package hudson.plugin.gradle.ath.config;

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.jenkinsci.test.acceptance.FallbackConfig;
import org.jenkinsci.test.acceptance.guice.TestCleaner;
import org.jenkinsci.test.acceptance.guice.TestName;
import org.jenkinsci.test.acceptance.selenium.Scroller;
import org.jenkinsci.test.acceptance.utils.ElasticTime;
import org.junit.runners.model.Statement;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.UnsupportedCommandException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.io.IOException;
import java.time.Duration;
import java.util.Locale;
import java.util.logging.Logger;

class WebDriverProvider implements Provider<WebDriver> {

private static final Logger LOGGER = Logger.getLogger(FallbackConfig.class.getName());
private final TestCleaner testCleaner;
private final FallbackConfig fallbackConfig;
private final TestName testName;
private final ElasticTime time;

@Inject
public WebDriverProvider(
TestCleaner testCleaner,
FallbackConfig fallbackConfig,
TestName testName,
ElasticTime time) {
this.testCleaner = testCleaner;
this.fallbackConfig = fallbackConfig;
this.testName = testName;
this.time = time;
}

private String getBrowser() {
String browser = System.getenv("BROWSER");
if (browser == null) browser = "chrome-container";
browser = browser.toLowerCase(Locale.ENGLISH);
return browser;
}

@Override
public WebDriver get() {
if ("chrome".equals(getBrowser())) {
return createChromeWebDriver();
}
try {
return fallbackConfig.createWebDriver(testCleaner, testName, time);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private WebDriver createChromeWebDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBrowserVersion("stable");
chromeOptions.addArguments("--window-position=0,0");
chromeOptions.addArguments("--window-size=1280,720");
chromeOptions.addArguments("--lang=en_US");
chromeOptions.setExperimentalOption("prefs", ImmutableMap.of("intl.accept_languages", "en_US"));
ChromeDriver d = new ChromeDriver(chromeOptions);
Dimension oldSize = d.manage().window().getSize();
if (oldSize.height < 1050 || oldSize.width < 1680) {
d.manage().window().setSize(new Dimension(1680, 1050));
}
final EventFiringWebDriver driver = new EventFiringWebDriver(d);
driver.register(new Scroller());
try {
driver.manage().timeouts().pageLoadTimeout(Duration.ofMillis(time.seconds(FallbackConfig.PAGE_LOAD_TIMEOUT)));
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(time.seconds(FallbackConfig.IMPLICIT_WAIT_TIMEOUT)));
} catch (UnsupportedCommandException e) {
// sauce labs RemoteWebDriver doesn't support this
LOGGER.info(d + " doesn't support page load timeout");
}
testCleaner.addTask(new Statement() {
@Override
public void evaluate() {
driver.quit();
}

@Override
public String toString() {
return "Close WebDriver after test";
}
});
return driver;
}
}
73 changes: 0 additions & 73 deletions build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ tasks.test {
maxRetries.set(2)
maxFailures.set(5)
}
failOnPassedAfterRetry.set(false)
}
useJUnitPlatform()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import spock.lang.Unroll
@Subject(BuildScanLogScanner)
class BuildScanLogScannerTest extends Specification {

def 'properly captures build scan url given #log'(List<String> log, List<String> expectedUrls) {
def 'properly captures build scan url given #label'(String label, List<String> log, List<String> expectedUrls) {
given:
def listener = new SimpleBuildScanPublishedListener()
def scanner = new BuildScanLogScanner(listener)
Expand All @@ -20,12 +20,12 @@ class BuildScanLogScannerTest extends Specification {
listener.buildScans == expectedUrls

where:
log || expectedUrls
logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"]) || ["https://scans.gradle.com/s/bzb4vn64kx3bc"]
logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc", "https://scans.gradle.com/s/asc9wm73ly1do"]) || ["https://scans.gradle.com/s/bzb4vn64kx3bc", "https://scans.gradle.com/s/asc9wm73ly1do"]
logWithBuildScans(["https://scans.gradle.com/bzb4vn64kx3bc"]) || []
logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"], 1010) || []
logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"], 900) || ["https://scans.gradle.com/s/bzb4vn64kx3bc"]
label | log || expectedUrls
'One build scan URL' | logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"]) || ["https://scans.gradle.com/s/bzb4vn64kx3bc"]
'Two build scan URLs' | logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc", "https://scans.gradle.com/s/asc9wm73ly1do"]) || ["https://scans.gradle.com/s/bzb4vn64kx3bc", "https://scans.gradle.com/s/asc9wm73ly1do"]
'Non build scan URL' | logWithBuildScans(["https://scans.gradle.com/bzb4vn64kx3bc"]) || []
'Too many lines before build scan URL' | logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"], 1010) || []
'Lot of lines before build scan URL' | logWithBuildScans(["https://scans.gradle.com/s/bzb4vn64kx3bc"], 900) || ["https://scans.gradle.com/s/bzb4vn64kx3bc"]
}

static List<String> logWithBuildScans(List<String> scanLinks, linesBetween = 10) {
Expand Down

0 comments on commit da57983

Please sign in to comment.