Note: This is not a guide to run selenium tests locally, for that there are lots of guides out there. To run Selenium tests on Travis CI, as a part of a CI lifecycle, a few things need to be done. First you must understand that Selenium utilises a browser, and browsers require a Desktop environment. Travis runs tests in an environment similar to your Advania QStack servers, which have no Desktop environment. This is where Xvfb comes in.
Xvfb creates a "virtual" Desktop environment, and before running an application that requires a Desktop environment, you can export a display (see below) so that when the application is started, it will be started in that virtual environment. Sounds simple, right?
For Selenium tests, that's it, Selenium will do the rest for you. So let's set this up!
Travis supports Xvfb and Firefox by default. One of the ways to do this (as done in https://github.com/arnlaugsson/chuck_joke/tree/with-selenium-xvfb), is as follows:
-
Create a
bin/xvfb
script for convenience:#!/bin/bash
if [ -z "$1" ]; then
echo "basename $0
{start|stop}"
exit
fi
case "$1" in start) /usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 & ;; stop) killall Xvfb ;; esac ``` 2. Define new Gradle source set (to have separate imports and tasks) in build.gradle:
```groovy
sourceSets {
selenium
seleniumXvfb
}
```
-
Add Selenium and SeleniumXvfb dependencies
dependencies { ... seleniumCompile 'junit:junit:4.11' seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.48.2' seleniumXvfbCompile 'junit:junit:4.11' seleniumXvfbCompile 'org.seleniumhq.selenium:selenium-java:2.48.2' }
-
Create a SeleniumXvfb gradle task. This task will set the Display needed (:99, same as the bin/xvfb file), start Xvfb and start the server before running tests.
task seleniumXvfb(type: Test, dependsOn: installDist) { testClassesDir = sourceSets.selenium.output.classesDir classpath = sourceSets.selenium.runtimeClasspath environment "DISPLAY", ":99" doFirst { 'bin/xvfb start'.execute() 'build/install/<name-of-project>/bin/<name-of-project> &'.execute() } doLast { 'sh -e /etc/init.d/xvfb stop'.execute() } }
5. Add some tests!
6. Git add your changes, commit and push to a Travis CI monitored Github repository.