plugins {
    id "groovy"
}

ext {
    // The drivers we want to use
    drivers = ["firefox", "chrome", "chromeHeadless"]

    gebVersion = '8.0.0'
    seleniumVersion = '4.27.0'
    chromeDriverVersion = '79.0.3945.36'
    geckoDriverVersion = '0.31.0'
}

repositories {
    mavenCentral()
    maven {
        url 'https://repository.apache.org/content/repositories/releases/'
    }
}

dependencies {
    implementation platform('org.junit:junit-bom:5.12.0')
    testImplementation "org.apache.groovy:groovy:4.0.28"
    // If using Spock, need to depend on geb-spock
    testImplementation "org.apache.groovy.geb:geb-spock:$gebVersion"

    // If using JUnit 5, need to depend on geb-junit5
    testImplementation "org.apache.groovy.geb:geb-junit5:$gebVersion"
    testImplementation 'org.junit.jupiter:junit-jupiter'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    // Drivers
    testImplementation "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
    testImplementation "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

drivers.each { driver ->
    task "${driver}Test"(type: Test) {
        group JavaBasePlugin.VERIFICATION_GROUP

        outputs.upToDateWhen { false }  // Always run tests

        useJUnitPlatform()

        systemProperty "geb.build.reportsDir", reporting.file("geb/$name")
        systemProperty "geb.env", driver
    }
}

test {
    dependsOn drivers.collect { tasks["${it}Test"] }
    enabled = false
}

tasks.withType(Test) {
    maxHeapSize = "1g"
    jvmArgs '-XX:MaxMetaspaceSize=128m'
    testLogging {
        exceptionFormat = 'full'
    }
    useJUnitPlatform()
}

tasks.withType(GroovyCompile) {
    groovyOptions.forkOptions.memoryMaximumSize = '256m'
}
