Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Jenkins 
3.
Maven
4.
Maven Installation
4.1.
Configure Eclipse with Maven
5.
Jenkins Installation
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Selenium Integration with Maven and Jenkins

Author Toohina Barua
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction 

Selenium WebDriver is an excellent tool for automating browsers. However, it feels underpowered when used for testing and building a test framework. Integrating Maven with Selenium provides many benefits. Apache Maven provides support for managing the full lifecycle of a test project.
In this article, we will learn about Selenium Integration with Maven and Jenkins. So let us dive in!

Also See, Locators in Selenium

Jenkins 

Hudson Lab developed Jenkins, the leading open-source continuous integration tool. It is cross-platform, meaning it can run on Windows, Linux, Mac OS, and Solaris. Jenkins is a Java-based application. Jenkin's main function is to track any job, such as SVN checkouts, cron jobs, or application states. When a particular step in a job occurs, it triggers pre-configured actions.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Maven

Maven is a powerful project / build management tool based on the concept of a POM (Project Object Model) that contains project information and Maven configuration information such as the construction directory, source directory, dependency, and test source directory, Goals, and plugins, among other things.

Maven Installation

To set up the Selenium WebDriver project, we'll use the Eclipse (Juno) IDE for Java Developers. We also need to install the m2eclipse plugin in Eclipse to make the build process easier and create the pom.xml file.

Let's add the m2eclipse plugin to Eclipse using the steps below:

Step1: From the Eclipse Main Menu, select Help | Install New Software.

Step 2: In the Install dialogue, type http://download.eclipse.org/technology/m2e/releases/ in the URL field. As shown in the image, select Work with and m2e plugin:

Source

Step 3: Click Next to complete the installation.

Configure Eclipse with Maven

Now that the m2e plugin has been installed, we must create a Maven project.

Step 1: In Eclipse IDE, select File | New | Other from the Eclipse menu to start a new project.

Step 2: Select Maven | Maven Project from the New dialogue and click Next.

Source

Step 3: Select Create a simple project from the New Maven Project dialogue and click Next.

Source

Step 4: Click Finish after entering WebdriverTest in the Group Id: and Artifact Id: fields.

Source

Step 5: Eclipse will create a WebdriverTest with the structure shown below:

Source

Step 6: Select the Properties option from the menu when right-clicking on JRE System Library.

Source

Make sure Workspace default JRE is selected in the Properties for JRE System Library dialogue box, then click OK.

Source

Step 7: From Project Explorer, choose pom.xml.

Source

In the Editor section, the pom.xml file will open.

Source

Step 8: In the <project> node, add the Selenium, Maven, TestNG, and Junit dependencies to pom.xml:

<dependencies> 
        <dependency> 
             <groupId>junit</groupId> 
             <artifactId>junit</artifactId> 
             <version>3.8.1</version> 
             <scope>test</scope> 
        </dependency> 
        <dependency> 
            <groupId>org.seleniumhq.selenium</groupId> 
            <artifactId>selenium-java</artifactId> 
            <version>2.45.0</version> 
</dependency> 
        <dependency> 
            <groupId>org.testng</groupId> 
            <artifactId>testng</artifactId> 
            <version>6.8.8</version> 
            <scope>test</scope>    
       </dependency> 
</dependencies>

 

Create a new TestNG class. In the Name: textbox, type "example" and "NewTest" and click the Finish button, as shown in the following image:

Source

Step 10: As shown in the following screenshot, Eclipse will create the NewTest class:

Source

Step 11: In the NewTest class, add the following code:

This code will verify the title of a Selenium Page

package example; 


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest; 
public class NewTest { 
    private WebDriver driver; 
@Test 
public void testEasy() {
driver.get("http://demo.website.com/test/home/");  
String title = driver.getTitle();  
Assert.assertTrue(title.contains("Demo Guru99 Page"));  
}
@BeforeTest
public void beforeTest() {
    driver = new FirefoxDriver();  
} 
@AfterTest
public void afterTest() {
driver.quit(); 
} 
}

 

Step 12: Select TestNG | Convert to TestNG from the context menu of the WebdriverTest.

As shown in the following screenshot, Eclipse will create testng.xml, which states that you only need to run one test named NewTest.

Source

Update the project and double-check that the file is visible in the Package Explorer tree (right-click on the project and select Refresh).

Source

Step 13: Now you must run the test using this testng.xml file.

 

So, go to Run Configurations and make a new launch there. Select the project and field Suite as testng.xml in TestNG, then click Run.

Source

Ascertain that the construction was completed successfully.

Step 14: In addition, we must include

  1. maven-compiler-plugin
  2. maven-surefire-plugin
  3. testng.xml

to pom.xml.

To configure and run tests, use the maven-surefire-plugin. The testing.xml for TestNG tests is configured and test reports are generated using this plugin.

The maven-compiler-plugin is used to aid in the compilation of the code and the use of the appropriate JDK version. In the pom.xml in the plugin> node, add all dependencies in the following code snippet:

Source

Step 15: Right-click on the WebdriverTest and select Run As | Maven test to run the tests in the Maven lifecycle. Maven will run the project's tests.

Source

Ascertain that the construction was completed successfully.

Jenkins Installation

Step 1: Visit http://jenkins-ci.org/ and download the appropriate package for your operating system. Set up Jenkins.

Source

Step 2: Unzip Jenkins to the desired location. As shown in the screenshot, run the exe file:

Source

Step 3: In the Jenkins 1.607 Setup window, select Next.

Source

Step 4: Finally, click the Install button.

Source

Step 5: Once the installation is complete, open a browser window and navigate to the Jenkins Dashboard (http://localhost:8080 by default).

Source

Step 6: To create a CI job, click the New Item link.

Step 7: As shown in the screenshot, select the Maven project radio button:

Source

Jenkins can build and test Maven projects using the Build a Maven Project option.

Step 8: Press the OK button. In Jenkins Dashboard, a new job named "WebdriverTest" is created.

Source

Step 9: As shown in the screenshot, go to Manage Jenkins => Configure System.

Source

Select JDK installations and set up JDK as shown in the screenshot:

Source

Step 10: In the new job, navigate to the Build section.

  • Put the full path to pom.xml into the Root POM textbox.
  • Type "clean test" in the Goals and Options section.

Source

Step 11: Click on the Apply button.

Source

Step 12: Click the Build Now link on the WebdriverTest project page.

Source

The project will be built by Maven. The test cases will then be executed by TestNG.

Step 13: In Jenkins Dashboard, select the WebdriverTest project once the build process is complete.

Source

Step 14: As shown in the following screenshot, the WebdriverTest project page displays the build history as well as links to the results:

Source

Step 15: To view the test results, click the "Latest Test Result" link, as shown in the screenshot below:

Source

Step 16: By clicking on "console output," you can see the current status of a specific build.

Source

Frequently Asked Questions

  1. What is Selenium?
    Selenium is a well-known automated testing framework. It's a playback tool that allows you to perform functional testing without writing test scripts. Selenium is a web UI-based automation testing package that is open-source.
  2. What are the different components of Selenium?
    It has four major components:
    Selenium Integrated Development Environment (IDE) 
    Selenium Remote Control
    WebDriver
    Selenium Grid
  3. What are the limitations of Selenium?
    The limitations of Selenium are as follows:
    It only works for web-based applications; it cannot test Windows or Android applications.
    Selenium is dependent on third-party applications for reporting purposes.
    It can not handle captcha and barcodes.
    Selenium has issues with handling frames and pop-up windows.
  4. Why Jenkins and Selenium?
    When you run Selenium tests in Jenkins, you can run them every time your software changes, and when the tests pass, you can deploy the software to a new environment.
    Jenkins can run your tests at a predetermined time.
    The execution history as well as the Test Reports can be saved.
    Jenkins allows you to build and test a project in continuous integration using Maven.
  5. What are some of the benefits of Maven?
    - Maven is used to define the structure of a project, as well as its dependencies, build, and test management. 
    - You can configure dependencies for building, testing, and running code using pom.xml (Maven). 
    - While building the project, Maven automatically downloads the required files from the repository.

Conclusion

In this article, we saw the steps to install Maven and use it with TestNG Selenium. We also learned how to install Jenkins and configure it to Run Maven with TestNG Selenium.
We hope that this blog has helped you enhance your knowledge regarding Selenium integration with Maven and Jenkins and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass