How to integrate Selenium WebDriver Java, GitHub, Jenkins and Ant

In this tutorial, we are going to be walking through the process of setting up Selenium WebDriver Java with source control and continuous integration. To do this, firstly we are going to need a few things.

Prerequisites:

  • Eclipse downloaded and installed
  • WebDriver libraries downloaded
  • Jenkins installed with default plugins
  • GitHub account setup

Step 1 – Create a simple WebDriver script

To integrate these tools, we are first going to need a simple WebDriver script, as something for our Jenkins server to run.

  • Click Finish to create the project
  • Create a simple junit test (you can place this in a new package inside the src package)

  • Here is my test that simply, launches Chrome and performs a basic assertion (i also added in the import statements)
  • Run it and make sure it executes successfully

Step 2 – Ant

  • Right click on your project->Export..
  • Export your project as an Ant buildfile, you should see the option in the dialog that appears

  • Click Next
  • And finish
  • You should now have an ant build file in your project workspace, if it is not there, simply try refreshing the workspace
  • Open the ant build file and locate the “path” section
  • You will need to modify each path in here to use an absolute path rather than a relative path

  • I will now change my remaining 6 path elements
  • Save and close your ant build file
  • Right click build file in the workspace->Run As->2 Ant Build… (You must run the second one)

  • In the dialog, you can specify an order to run these tests by clicking the order button, but this is only when running from eclipse
  • Run it and make sure the ant build file runs the test correctly

Step 3 – GitHub

  • Go the GitHub homepage and create a new project (I named mine WebDriverRepo and made it a public repository)
  • Grab the link to the repository from the top of the page, or by simply clicking the button highlighted below:

  • Go back to your eclipse workspace
  • From the toolbar->Window->Show View->Other
  • Now in here you want to search for and open two windows; Git Staging and Git Repositories
  • Open one window and then repeat the steps for the other window
  • Once open, go to the Git Repositories window and click ‘Create a new repository’

  • Now set the repository directory to your projects file path

  • Make sure ‘Create as bare repository’ is not ticked!
  • Click finish
  • This should create the repository and it should appear in the Git repositories window
  • Click on it and in the Git Staging window, you should see a bunch of files under the ‘Unstaged Changes’ section
  • Select them all and drag them into ‘Staged Changes’

  • Once all changes are stages, you can go ahead and add a Commit message
  • Finally click Commit and Push to commit to a local repository (the one in your project is now your local repo) and push the changes up to the GitHub repo
  • In the URI section, in the new dialog that appears, paste in the path to your GitHub repository and it should autocomplete the other fields with your credentials. (If not, simply fill them in manually)
  • Next->Next->Finish wait a few seconds for it to push the changes.
  • Check the files are in your GitHub repo by going back to your GitHub account and refreshing the repository page
  • You should now have a bunch of files in there

Step 4 – Jenkins Configuration & Node Setup

  • Now you will want to head over to your Jenkins server
  • Manage Jenkins (if you do not have the default plugins installed you will have to grab the; Git, GitHub, and the Ant plugin from the Manage Plugins section)
  • Click ‘Global Tool Configuration’
  • Add JDK, give it a name and pass in the path to the JDK folder

  • Add GIT, here in the path we simply entered ‘git’

  • And finally add the installation for Ant
  • Don’t worry about it saying that Ant is not a directory, remember the Jenkins server will be running on another machine which probably won’t have all of these things installed on, as long as it is on the machine on which your tests will be running, you should be ok
  • Save the configuration
  • Back in the Manage Jenkins section, go to Manage Nodes
  • Create a New Node, give it a name and make it a Permanent Agent
  • In the Node settings, enter a root directory path to a folder (we simply created an empty folder on our C drive named; Jenkins Workspace)

  • And give it a label, this can be the same as the name
  • Click Save
  • Click the new node from the list and click the orange Launch button

  • This should launch the Jenkins agent on the specified machine, after the file has downloaded

Step 5 – Jenkins Job

  • Head back to the homepage of Jenkins and create a new job
  • General:
    • Under the General section you should see an option to ‘restrict where this project can be run’
    • Tick this and pass in the label of your node

  • Source Code Management:
    • Git
    • Pass in the path to your GitHub repository in the Repository URL field
  • Build Environment
    • Select ‘With Ant’
    • Specify the names of your Ant version and the JDK (mine were named: JDK_OD and ANT_OD)
  • Build
    • Click ‘Add Build Step’->’Invoke Ant’
    • Add 2 of these
      • First one to run your build.xml file (in the Build File field, under the Advanced section)
      • Second one to run your specified test in the Targets field

  • You can now save and Build Now to see if it is working as expected

Extra – Polling and JUnit Test Results

Polling:

  • If it runs successfully, you can set up polling of your GitHub repository, so that if a change is pushed to the repo, Jenkins will see that change and re-build and re-run your test
  • Simply go back to the job configuration page
  • Under the Build Triggers section, you should see a ‘Poll SCM’ checkbox. Tick this
  • You can find more information on the format of the schedule by clicking the question mark icon highlighted below, we simply set this up to poll every 5 minutes for any changes

  • Now when I save this, Jenkins will be checking for any changes within my GitHub repository

Publish JUnit Results:

  • To publish the JUnit results to Jenkins we head back into the job configuration
  • Down to the ‘Post build actions’ section, add a step for ‘Publish junit test results report’
  • Now in here you will need to pass in the name of the test results file from your jenkins/eclipse workspace, the name should be the same, unless you have specified otherwise
  • This will be inside a junit folder aswell so the path must represent that:

  • Finally if you want to test it and make sure it all works correctly, simply go over to eclipse, make some changes within your test and save, commit and push them up to your GitHub repo
  • After 5 minutes (depending on your schedule) you should see Jenkins:
    • Execute your test, completely automatically
    • And now when you navigate into the Successful build within Jenkins, you should have a ‘Test Result’ section

  • Open this to get your Junit Results