Gradle Selenium (WebDriver) task

If you want to run your Selenium (WebDriver) tests from Gradle you can do it like this:

1. Add Jetty plugin to build.gradle:

apply plugin: 'jetty'

2. Create a src/selenium/java folder in you project root.

3. Add a SourceSet to build.gradle:

sourceSets {

4. Add Selenium dependencies to build.gradle:

seleniumCompile 'junit:junit:4.11'
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.30.0'

Add jettyDaemon and selenium tasks to build.gradle:

task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
    daemon = true

task selenium(type: Test, dependsOn: jettyDaemon) {
    testClassesDir = sourceSets.selenium.output.classesDir
    classpath = sourceSets.selenium.runtimeClasspath

5. If you are using Eclipse, add Selenium dependencies to the Eclipse classpath in build.gradle:

eclipse {
    classpath {
        plusConfigurations += configurations.seleniumCompile

You can now place you Selenium tests in src/selenium/java and run:

gradle clean selenium

7 Responses to Gradle Selenium (WebDriver) task

  1. Thanks for the post!

    Did u forget to declare the seleniumCompile configuration? Should that extend testCompile/testRuntime

    Also: what is the need for “clean”? Anything special with any tasks?

    • In old versions of Gradle you had to declare configurations for sourceSets. Since version 1.0 (or maybe later) it’s implicit when you declare a sourceSet.

      There is no need to add “clean” when you run gradle selenium the first time, but if you were to run it again, without any code change, the selenium tests would not run. Gradle does it this way so that it doesn’t have to recompile code if there has not been a code change. In any case, I do it out of habit, since I always want the selenium tests to run when I run the command gradle selenium.

  2. Cool, u learn something every day :-)

    I still declared a seleniumCompile just so I could extend the testCompile config (that way I avoided the straight cpy/paste, which always makes me feel like a thief)

Leave a Reply to Simon Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>