import org.apache.tools.ant.taskdefs.condition.Os

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

  ext {
    groovyVersion = '2.4.1'
    gebVersion = '0.12.2'
    seleniumVersion = '2.46.0'
    chromeDriverVersion = '2.19'
    phantomJsVersion = '1.9.7'
  }
}

apply plugin: "groovy"
apply from: "gradle/idea.gradle"
apply from: "gradle/osSpecificDownloads.gradle"

repositories {
  mavenCentral()
}

dependencies {
  // If using Spock, need to depend on geb-spock
  testCompile "org.gebish:geb-spock:$gebVersion"
  testCompile("org.spockframework:spock-core:1.0-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"
  testCompile "org.seleniumhq.selenium:selenium-support:$seleniumVersion"

  // Drivers
  testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
  testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
  // using a custom version of phantomjs driver for now as the original one does not support WebDriver > 2.43.1
  testCompile("com.codeborne:phantomjsdriver:1.2.1") {
    // phantomjs driver pulls in a different selenium version
    transitive = false
  }
}

drivers.each { driver ->
  task "${driver}Test"(type: Test) {
    reports {
      html.destination = reporting.file("$name/tests")
      junitXml.destination = file("$buildDir/test-results/$name")
    }

    outputs.upToDateWhen { false }  // Always run tests

    systemProperty "geb.build.reportsDir", reporting.file("$name/geb")
    systemProperty "geb.env", driver
    systemProperty "profile", System.getProperty("profile", "production")

    // If you wanted to set the baseUrl in your build…
    // systemProperty "geb.build.baseUrl", "http://myapp.com"
  }
}

chromeTest {
  dependsOn unzipChromeDriver

  def chromedriverFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "chromedriver.exe" : "chromedriver"
  systemProperty "webdriver.chrome.driver", new File(unzipChromeDriver.outputs.files.singleFile, chromedriverFilename).absolutePath
}

phantomJsTest {
  dependsOn unzipPhantomJs

  def phantomJsFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "phantomjs.exe" : "bin/phantomjs"
  systemProperty "phantomjs.binary.path", new File(unzipPhantomJs.outputs.files.singleFile, phantomJsFilename).absolutePath
}

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

apply from: "gradle/ci.gradle"
