Maven for Selenium

FREE Online Courses: Knowledge Awaits – Click for Free Access!

Popular open-source software called Selenium is used to automate web browsers. It allows testers and developers to automate web-based applications to perform functional testing. One of the popular build tools for Selenium automation is Maven. Selenium projects can be managed and built using Maven, a potent build automation tool.

In this tutorial, we will cover a Selenium Maven tutorial, which will provide a step-by-step guide on how to set up Selenium projects using Maven.

Prerequisites for

The following criteria should be met before beginning this tutorial:

  • Java Development Kit (JDK) installed
  • Maven installed
  • Selenium WebDriver Java client library installed

Setting up a Maven Project for Selenium

Follow the below steps to set up a Maven project for Selenium:

1. Open a terminal or command prompt and navigate to the directory where you want to create the Maven project.

2. Type the following command to create a new Maven project:

‘mvn archetype:generate -DgroupId=com.example.selenium -DartifactId=selenium-maven-tutorial -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false’

The above command creates a new Maven project with the group ID com.example.selenium and the artifact ID selenium-maven-tutorial.

3. Use the following command to navigate to the newly formed project directory:

cd selenium-maven-tutorial

4. Open the pom.xml file located in the project’s root directory in a text editor. Add the following dependencies to the pom.xml file:

<dependencies>
   <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>3.141.59</version>
   </dependency>
</dependencies>

The above dependencies are required to use Selenium in our Maven project.

5. Save the pom.xml file and close it.

Writing a Selenium Test

Now that we have set up a Maven project for Selenium, we can create a Selenium test.

1. The code below will create a new Java class in the src/main/java directory:

package com.example.selenium;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;


public class SeleniumTest {


   @Test
   public void test() {
       System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");


       WebDriver driver = new ChromeDriver();


       driver.get("https://www.google.com");


       if (driver.getTitle().contains("Google")) {
           System.out.println("Test Passed!");
       } else {
           System.out.println("Test Failed");
       }


       driver.quit();
   }
}

The above code opens a Chrome browser window, navigates to the Google homepage. It then verifies that the title of the page contains the word “Google”, and then closes the browser window.

2. Save the Java class with the name SeleniumTest.java.

Running the Selenium Test

To run the Selenium test, we can use the following command:

mvn test

The above command will execute the test() method in the SeleniumTest class.

Conclusion

In this tutorial, we have shown how to set up a Maven project for Selenium automation and how to create a simple Selenium test using Maven. With the knowledge gained from this tutorial, you should be able to create and run Selenium tests in a Maven project. Happy testing!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *