0
0
Selenium Javatesting~5 mins

Running tests via Maven in Selenium Java

Choose your learning style9 modes available
Introduction

We use Maven to easily run and manage Selenium tests. It helps run tests with one command and handles dependencies automatically.

You want to run all your Selenium tests in one go without opening each test file.
You need to run tests on different machines or servers in the same way.
You want to automate tests as part of your project build process.
You want to share your test project with others and make running tests simple.
You want to generate reports after running tests automatically.
Syntax
Selenium Java
mvn test

# To run tests with a specific test class:
mvn -Dtest=TestClassName test

# To run tests with a specific method:
mvn -Dtest=TestClassName#testMethodName test

mvn test runs all tests in the project.

You can specify which tests to run using the -Dtest option.

Examples
Runs all Selenium tests in the project.
Selenium Java
mvn test
Runs only the LoginTest class tests.
Selenium Java
mvn -Dtest=LoginTest test
Runs only the testValidLogin method inside LoginTest.
Selenium Java
mvn -Dtest=LoginTest#testValidLogin test
Sample Program

This is a simple Selenium test using JUnit 5. It opens Google and checks the page title.

Running mvn test will execute this test and show results in the console.

Selenium Java
package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SimpleTest {
    WebDriver driver;

    @BeforeEach
    public void setup() {
        driver = new ChromeDriver();
    }

    @Test
    public void testGoogleTitle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        assertEquals("Google", title);
    }

    @AfterEach
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

// To run this test via Maven, use the command:
// mvn test

// Expected output snippet:
// [INFO] -------------------------------------------------------
// [INFO]  T E S T S
// [INFO] -------------------------------------------------------
// [INFO] Running tests.SimpleTest
// [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: X.XXX s - in tests.SimpleTest
// [INFO] 
// [INFO] Results:
// [INFO] 
// [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
// [INFO] 
// [INFO] ------------------------------------------------------------------------
// [INFO] BUILD SUCCESS
// [INFO] ------------------------------------------------------------------------
OutputSuccess
Important Notes

Make sure your pom.xml includes the maven-surefire-plugin to run tests.

Ensure your Selenium WebDriver binaries (like chromedriver) are set up correctly in your system PATH.

You can add test reports plugins in Maven to get detailed test reports after running tests.

Summary

Maven lets you run Selenium tests easily with one command.

You can run all tests or select specific tests using command options.

Test results show in the console and can be extended with reports.