Jenkins Integration: Selenium Grid

Reading Time: 4 minutes
  • Selenium Grid is a tool that we can use for testing, automating web applications, and run them on remote machines. We can run our tests to integrate them with CI/CD tools such as Jenkins. In this blog, we will see Jenkins Integration with Selenium Grid.

Why is Jenkins integration required?

  • Test automation helps us with continuous defects, errors, and bugs as early as possible. If we catch the issue earlier then it is cheaper to fix it.
  • With CI/CD integration we can run our tests after every build or after a set interval of time which will help us identify any issues.
  • It is always faster to test the service manually or to run the regression manually. As it will run automatically without any human intervention. And a report will be generated at the end of it.
  • So, that we can see if any tests are failing or not. If failing they can be easily fixed.

Requirements

  • Jenkins
  • JDK
  • Maven
  • Selenium project

Setting up Jenkins

We will be using a maven project and pom.xml class to execute our test. To do that we will be required to configure JDK and Maven in Jenkins.

  • Log in into Jenkins and go to Manage Jenkins.
  • Go to Global Tool Configuration.
  • Go to JDK and click on Add JDK.
  • Here you have 2 options either install it automatically by selecting a checkbox (Install automatically), selecting the version and entering your oracle account details. You are required to have a valid oracle account.
  • Or you can unselect the checkbox and provide the name and path to your local JDK.
  • Go to maven and click on Maven installations.
  • Here you have 2 options either install it automatically by selecting a checkbox (Install automatically) and selecting the Maven version that you want.
  • Or you can unselect the checkbox and provide the name and path to your local MAVEN_HOME.
  • Save the configurations
  • Again go to Manage Jenkins and click on Manage Plugins.
  • Search and install: Selenium plugin for running Grid on Jenkins.

Setting-up Selenium Grid

  • Once we install the plugin we will be able to see it in the Jenkins dashboard. Go inside and you will see the Hub management page where the URL to the Hub is given. This will be used to execute our tests on Jenkins
  • Go to the configurations tab and click on New Configurations.
  • In this section, we will give the name of the configuration. And also we need to provide the path to the browser driver. We have given the path to our chromedriver. Save the configuration.
  • Now go to the Nodes matching configurations section and click on the newly created browser configuration to go inside and start the server.
  • Once you start the server you will all the configuration that you have saved.
  • Now go back to your Selenium Grid Hub management and you will see one new registered node in the Registered Remote Controls selection
  • Now we are ready to run our tests on Jenkins.

Configuring your Tests

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.logging.log4j.*;

public class JenkinsSeleniumGridTest {
    private static Logger log = LogManager.getLogger(JenkinsSeleniumGridTest.class);

    @Test
    public void JenkinsDemoFunc() throws MalformedURLException {
        ChromeOptions chromeOptions = new ChromeOptions();
        //initialize chromeOptions
        chromeOptions.setCapability("browserName", "chrome");
        //Define on which browser you want to execute your tests.
        chromeOptions.setCapability("platformName", "LINUX");
        //Define in which mode your tests will run.
        chromeOptions.addArguments("--headless");
        //Define the platform on which you will execute your tests
        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeOptions);
        //URL to the hub running on your local system
        driver.get("http://www.knoldus.com");
        //URL to hit
        log.debug(driver.getTitle());
        //Print the title of the webpage
        log.debug(driver.getCurrentUrl());
        //Print the URL of the current webpage
        driver.quit();
        //Close the browser
    }
}
  • In this code, we are providing the browser name and platform name on which our tests will run.
  • Using Jenkins, we usually execute our tests in the headless mode to do that will pass headless argument in our code.
  • In remote WebDriver URL, we are giving the URL which is present in the Jenkins Hub management.
  • We need to provide the valid URL which we will hit.
Output from Intellij
Output from Jenkins
  • So, this was a short blog on how we can achieve Jenkins Integration with Selenium Grid. See you in the next one.

References


Knoldus-blog-footer-image

Written by 

Ankur is a Sr. QA Consultant having experience of more than 3 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advanced concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading