Search code examples
gradleautomated-testsspockgeb

Running specific tests using gradle over multiple browsers


I'm using Geb/Spock for automated testing. I'm using Gradle as my build tool.

I'd like to call different gradle tasks to build and run a specific spec(test) or a suite of specs.

I dont know enough about the gradle build lifecycle to completely understand what is going on here: https://github.com/geb/geb-example-gradle/blob/master/build.gradle

plugins {
    id "idea"
    id "groovy"
    id "com.energizedwork.webdriver-binaries" version "1.4"
    id "com.energizedwork.idea-base" version "1.4"
}

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

    ext {
        groovyVersion = '2.4.12'
        gebVersion = '2.2'
        seleniumVersion = '3.6.0'
        chromeDriverVersion = '2.32'
        geckoDriverVersion = '0.18.0'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // If using Spock, need to depend on geb-spock
    testCompile "org.gebish:geb-spock:$gebVersion"
    testCompile("org.spockframework:spock-core:1.1-groovy-2.4") {
        exclude group: "org.codehaus.groovy"
    }
    testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"

    // If using JUnit, need to depend on geb-junit (3 or 4)
    testCompile "org.gebish:geb-junit4:$gebVersion"

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

webdriverBinaries {
    chromedriver chromeDriverVersion
    geckodriver geckoDriverVersion
}

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

        outputs.upToDateWhen { false }  // Always run tests

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

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

I've tried inserting the following into build.gradle:

task dataGen {
    include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}

task sanity {
    include '**com.company.project.spec.sanity.*'
}

But calling these tasks (gradle sanity) results in a build failure:

Could not find method include() for arguments [**com.company.project.spec.util/DataGenerationUtilSpec.groovy] on task ':dataGen' of type org.gradle.api.DefaultTask

Obviously there's existing build instructions since I can call gradle build and all the specs run on Chrome, I'm just not sure how to add more tasks


Solution

  • You can use Spock annotation to control the test or the Spec, see example here.

    You will have to define annotation classes and define the Spock config file to use that annotation. You then annotate the specific Specification (or test).
    Now you will have to define the Spock config file in the task or from a parameter.