Running Selenium Tests Using Jenkins
In this post you will get a quick introduction on running Selenium tests as part of your continuous integration workflow using the famous Jenkins CI server.
What is Jenkins?
Jenkins is an open source continuous integration server which supports countless building and deployment scenarios through its hundredths of plugins. You can read more about Jenkins here.
What is Selenium?
Selenium is a free and open source browser automation framework supported by all major browsers. It can be used to program test for web applications and websites in many different programming languages.
Run Selenium Tests using Jenkins
As mentioned above Selenium tests don’t require any sort of special runtime so normally you would use the Unit testing framework of your choice to execute the Selenium tests alongside your normal Unit tests (What are Unit Tests?).
In the example below we use C# and NUnit but the concept is similar regardless of the language and framework you are using.
Let take a look at our sample test first:
[Test]
public void TestWithFirefox()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(TestUrl);
Assert.IsFalse(string.IsNullOrEmpty(driver.Title));
driver.Quit();
}
This test will simply open Firefox, navigate to the url under test and check that the title of the page is not empty. Now we need to configure Jenkins to actually execute the tests as part of our build.
Now we are all set to execute this test as part of our build. But wait a second, where does the Firefox instance come from and on which machine does it run on? In this simple example Jenkins will run the Firefox browser directly on the machine where Jenkins is installed. Obviously there is something not quite right with this approach:
- Tests run directly on the Jenkins Server machine
- Not suitable for bigger test suites
- Testing with different operating systems is not possible
- Tests can not run in parallel
Using Selenium Grid
Instead of running the browsers directly on the Jenkins machine we can use a Selenium Grid to run the browsers on one or more designated machine(s). For this we need to setup a Selenium Grid and at least one Selenium Node (How?).
The good news is that we don’t need to change anything in the Jenkins configuration to execute the tests on a Selenium Grid, only the test case needs a small adjustment.
[TestMethod]
public void TestWithFirefox()
{
DesiredCapabilities capability = DesiredCapabilities.Firefox();
IWebDriver driver = new RemoteWebDriver(HubUrl, capability);
driver.Navigate().GoToUrl(TestUrl);
Assert.IsFalse(string.IsNullOrEmpty(driver.Title));
driver.Quit();
}
Running the build again will execute the tests against the Selenium Grid which, as mentioned, can run on a separate machine. This fixes all problems mentioned above:
- Tests now run on a separate machine
- Suitable for bigger test suites
- Nodes on different operating systems are possible
- Tests can be executed in parallel (if your unit test framework supports this)
Simpler Selenium Grid Setup
Of course the setup and maintenance of a Selenium Grid is quite the challenge in itself. Luckily our cross browser testing tool BrowseEmAll can help you solve this. To get a Selenium Grid running with BrowseEmAll just follow these 3 steps:
- Install BrowseEmAll on the machine where you want to run the Selenium Grid
- Start the application, switch to the Selenium tab and select the browsers your Grid should support:
3. Start the Grid and wait until it becomes available:
Now you can run your tests against a ready to go Selenium Grid without any further changes to the code or Jenkins configuration. BrowseEmAll will automatically maintain the Grid for you so you can focus on the important parts.