Using Gradle to Build Java Selenium Projects

In a recent article, we showed you how to start working with [Selenium in IntelliJ Idea](http://localhost:4000/selenium-and-intellij-idea-part-two/). As we’ve often done in the past, we started our Idea project with a Maven build script (a pom file). Well, all that worked well enough, but as Jenniffer was beginning to make some really good progress on her Goalboost Testing project, I wanted to help her with a tool that would enable custom scripting of tasks such as generating HTML from Markdown for the test plan, while still allowing us to build and run Selenium tests in Java in a straightforward way as Maven does.

In recent years there’s been a lot of buzz around Gradle, the tool that lets you enjoy the easy dependency management that made Maven so popular while still having the flexibility to script whatever custom tasks you need to create. So being the language and tools junkie that I am, of course I had to try it out. My first task was to allow Jenniffer to write the Goalboost test plan in Markdown and generate the HTML as needed so she could preview it without pushing to Github. Using Andres Almiray’s markdown-gradle-plugin, this turned out to be pretty easy to do:

Now that we had a way to “build” the test plan as it was being written, we also wanted to use the same tool to build and run the Selenium tests that she’d be developing against the plan. So what was needed was a Gradle build script that build a Java Selenium project the same way my earlier Maven build script did. Taking as a starting point Juan Cavallotti’s fine article on converting a Maven project to Gradle, it turned out this was really to do. The first version was done in about twenty minutes, but here’s a version with a bit more polish:

Once you have Gradle installed (if you don’t, download it here), importing the Gradle project into IntelliJ Idea works pretty much the same way as importing a Maven project. That is to say, in Idea you use File / Import Project, select the directory where the build.gradle file lives, then click “Import project from external model” and select “Gradle” as the model type.

Unfortunately, Idea’s support for Gradle is not yet as polished as it is for Maven, but it’s still possible to use Gradle to import the dependencies you need to build from within Idea as well. To do this, go to Idea / View / Tools / Gradle to open the Gradle tool window, as shown below:

gradletoolsmenu

This will bring up the Gradle Tool Window, shown below. In that window, the button with the circular arrows right under “Gradle Tasks” is the “Refresh all Gradle projects” button. Clicking this will allow you to build inside of IntelliJ Idea as you normally would, or you can also select Gradle tasks to run from within this window as well. I’ve run the test task and the jar task already, so this shows up in the Gradle Tool Window under “Recent tasks”.

gradletoolwindow

The SeleniumStarterGradle directory of the CodeSolid tutorials contains a complete Idea project with a Java Selenium test to get you started. You may need to go into the project settings to point your gradle configuration to your local Gradle install (File / Settings / Gradle).

Leave a Reply