Skip to content

Commit

Permalink
Try chrome for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
alextu committed Nov 17, 2023
1 parent 895034d commit 89d0ab9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion acceptance-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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 @@ -2,16 +2,31 @@

import com.cloudbees.sdk.extensibility.Extension;
import com.cloudbees.sdk.extensibility.ExtensionModule;
import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Names;
import hudson.plugin.gradle.ath.updatecenter.VersionOverridesDecorator;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jenkinsci.test.acceptance.guice.TestCleaner;
import org.jenkinsci.test.acceptance.guice.TestScope;
import org.jenkinsci.test.acceptance.selenium.Scroller;
import org.jenkinsci.test.acceptance.update_center.UpdateCenterMetadata;
import org.jenkinsci.test.acceptance.update_center.UpdateCenterMetadataProvider;
import org.junit.runners.model.Statement;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Dimension;
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 org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.File;
import java.util.Locale;
import java.util.function.Consumer;

/**
Expand All @@ -31,6 +46,63 @@ 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 WebDriverProvider implements Provider<WebDriver> {

private final TestCleaner testCleaner;

@Inject
public WebDriverProvider(TestCleaner testCleaner) {
this.testCleaner = testCleaner;
}

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

@Override
public WebDriver get() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/usr/bin/google-chrome-for-testing");
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());
testCleaner.addTask(new Statement() {
@Override
public void evaluate() {
if (getBrowser().equals("firefox")) {
//https://github.com/mozilla/geckodriver/issues/1151
//https://bugzilla.mozilla.org/show_bug.cgi?id=1264259
//https://bugzilla.mozilla.org/show_bug.cgi?id=1434872
driver.navigate().to("about:mozilla");
Alert alert = ExpectedConditions.alertIsPresent().apply(driver);
if (alert != null) {
alert.accept();
driver.navigate().refresh();
}
}
driver.quit();
}

@Override public String toString() {
return "Close WebDriver after test";
}
});
return driver;
}
}

private static class ResultDecoratingAdapter<T> implements MethodInterceptor {
Expand Down

0 comments on commit 89d0ab9

Please sign in to comment.