Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically download chrome driver when running on CI #253

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions acceptance-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -86,8 +86,6 @@ jenkinsVersions
languageVersion.set(jenkinsVersion.javaVersion)
})

jvmArgumentProviders.add(ChromeDriverProvider(ciTeamCityBuild))

doFirst {
environment(
mapOf(
Expand Down Expand Up @@ -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<String> =
// 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()
}
}
74 changes: 74 additions & 0 deletions build-logic/src/main/kotlin/buildlogic.chromedriver.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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")
}

// 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
}

tasks.withType(Test::class).configureEach {
inputs.files(chromedriver)
jvmArgumentProviders += ChromeDriverProvider(chromedriver)

finalizedBy(killRunningChromedriverInstances)
}
}

class ChromeDriverProvider(@Internal val chromedriverDirectory: FileCollection) : CommandLineArgumentProvider {

override fun asArguments(): Iterable<String> {
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")
}
}