From 91dbb1ba99d7fa2175141355aa202f839e454e53 Mon Sep 17 00:00:00 2001 From: Alexey Venderov Date: Mon, 27 Feb 2023 18:38:46 +0100 Subject: [PATCH 1/2] Automatically download chrome driver when running on CI --- acceptance-tests/build.gradle.kts | 15 +--- .../kotlin/buildlogic.chromedriver.gradle.kts | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts diff --git a/acceptance-tests/build.gradle.kts b/acceptance-tests/build.gradle.kts index a7219cc2..b0f255e8 100644 --- a/acceptance-tests/build.gradle.kts +++ b/acceptance-tests/build.gradle.kts @@ -4,10 +4,10 @@ import java.net.URL plugins { java + id("buildlogic.chromedriver") id("de.undercouch.download") version "5.3.1" } -val ciTeamCityBuild: Boolean by (gradle as ExtensionAware).extra val ciJenkinsBuild: Boolean by (gradle as ExtensionAware).extra java { @@ -86,8 +86,6 @@ jenkinsVersions languageVersion.set(jenkinsVersion.javaVersion) }) - jvmArgumentProviders.add(ChromeDriverProvider(ciTeamCityBuild)) - doFirst { environment( mapOf( @@ -150,14 +148,3 @@ data class JenkinsVersion(val version: String, val downloadUrl: URL, val javaVer val requiredJavaVersion: JavaVersion get() = JavaVersion.toVersion(javaVersion.toString()) } - -class ChromeDriverProvider(private val teamCityBuild: Boolean) : CommandLineArgumentProvider { - - override fun asArguments(): Iterable = - // If executed on TeamCity, we need to set the Chromedriver path - if (teamCityBuild) { - listOf("-Dwebdriver.chrome.driver=${System.getenv("HOME")}/.gradle/webdriver/chromedriver/chromedriver") - } else { - emptyList() - } -} diff --git a/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts b/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts new file mode 100644 index 00000000..8ea1bee6 --- /dev/null +++ b/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts @@ -0,0 +1,77 @@ +import org.gradle.api.internal.artifacts.transform.UnzipTransform +import org.gradle.internal.os.OperatingSystem + +// Latest version: https://chromedriver.storage.googleapis.com/LATEST_RELEASE +val chromeDriverVersion = "110.0.5481.77" +val ciTeamCityBuild: Boolean by (gradle as ExtensionAware).extra + +val os: OperatingSystem = OperatingSystem.current() +val driverOsFilenamePart = when { + os.isWindows -> "win32" + os.isMacOsX && os.nativePrefix.contains("aarch64") -> "mac_arm64" + os.isMacOsX -> "mac64" + os.isLinux && os.nativePrefix.contains("64") -> "linux64" + else -> "linux32" +} + +repositories { + exclusiveContent { + forRepository { + ivy { + url = uri("https://chromedriver.storage.googleapis.com/") + patternLayout { + artifact("[revision]/[artifact]_[classifier].[ext]") + } + metadataSources { + artifact() + } + } + } + filter { + includeModule("chromedriver", "chromedriver") + } + } +} + +val chromedriver: Configuration by configurations.creating { + isCanBeResolved = true + isCanBeConsumed = false + attributes.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE) +} + +dependencies { + registerTransform(UnzipTransform::class) { + from.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.ZIP_TYPE) + to.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE) + } + chromedriver("chromedriver:chromedriver:${chromeDriverVersion}:${driverOsFilenamePart}@zip") +} + +val killRunningChromedriverInstances by tasks.registering(Exec::class) { + if (os.isWindows) { + didWork = false + } else { + val echoOption = if (os.isLinux) "e" else "l" + commandLine("bash", "-c", "pkill -9 -${echoOption} chrome") + isIgnoreExitValue = true + } +} + +if (ciTeamCityBuild) { + tasks.withType(Test::class).configureEach { + inputs.files(chromedriver) + jvmArgumentProviders += ChromeDriverProvider(chromedriver) + + finalizedBy(killRunningChromedriverInstances) + } +} + +class ChromeDriverProvider(@Internal val chromedriverDirectory: FileCollection) : CommandLineArgumentProvider { + + override fun asArguments(): Iterable { + val chromedriver = chromedriverDirectory.asFileTree.files.first { it.name == "chromedriver" } + // UnzipTransform does not preserve file permissions, so we're restoring it here + chromedriver.setExecutable(true) + return listOf("-Dwebdriver.chrome.driver=$chromedriver") + } +} From ec96d2c522b9956f9bb88b2e884cf91024857ab8 Mon Sep 17 00:00:00 2001 From: Alexey Venderov Date: Mon, 27 Feb 2023 19:35:51 +0100 Subject: [PATCH 2/2] Do not try to download chrome driver on Windows --- .../src/main/kotlin/buildlogic.chromedriver.gradle.kts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts b/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts index 8ea1bee6..5d4c6330 100644 --- a/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts +++ b/build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts @@ -47,17 +47,14 @@ dependencies { chromedriver("chromedriver:chromedriver:${chromeDriverVersion}:${driverOsFilenamePart}@zip") } -val killRunningChromedriverInstances by tasks.registering(Exec::class) { - if (os.isWindows) { - didWork = false - } else { +// We do not run acceptance-tests on Windows +if (ciTeamCityBuild && !os.isWindows) { + val killRunningChromedriverInstances by tasks.registering(Exec::class) { val echoOption = if (os.isLinux) "e" else "l" commandLine("bash", "-c", "pkill -9 -${echoOption} chrome") isIgnoreExitValue = true } -} -if (ciTeamCityBuild) { tasks.withType(Test::class).configureEach { inputs.files(chromedriver) jvmArgumentProviders += ChromeDriverProvider(chromedriver)