0
0
Selenium Javatesting~15 mins

Running tests via Maven in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Run Selenium tests using Maven command
Preconditions (3)
Step 1: Open a terminal or command prompt
Step 2: Navigate to the root folder of the Selenium project where pom.xml is located
Step 3: Run the command 'mvn test' to start test execution
Step 4: Wait for Maven to compile and run the tests
Step 5: Observe the console output for test execution results
✅ Expected Result: Maven runs the Selenium tests and shows test results summary in the console, indicating which tests passed or failed
Automation Requirements - JUnit 5 with Selenium WebDriver
Assertions Needed:
Verify the test method runs successfully
Verify the test output includes 'BUILD SUCCESS' after tests pass
Best Practices:
Use Maven Surefire plugin for running tests
Keep test code and pom.xml configuration clean and minimal
Use descriptive test method names
Run tests from project root directory
Automated Solution
Selenium Java
package tests;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class LoginTest {

    @Test
    public void testLoginPageTitle() {
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/login");
            String title = driver.getTitle();
            assertTrue(title.contains("Login"), "Page title should contain 'Login'");
        } finally {
            driver.quit();
        }
    }
}

/*
To run this test via Maven:
1. Open terminal in project root (where pom.xml is)
2. Run: mvn test

Expected console output includes:
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running tests.LoginTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: X.XXX s
[INFO] 
[INFO] BUILD SUCCESS
*/

The test class LoginTest uses JUnit 5 and Selenium WebDriver.

We set the ChromeDriver system property to locate the driver executable.

The test opens the login page URL and checks the page title contains 'Login'.

Finally, the driver quits to close the browser.

Running mvn test in the project root triggers Maven Surefire plugin to compile and run this test.

The console output shows test progress and ends with BUILD SUCCESS if tests pass.

Common Mistakes - 4 Pitfalls
{'mistake': "Running 'mvn test' from a directory other than the project root", 'why_bad': 'Maven cannot find the pom.xml file and will fail to run tests', 'correct_approach': 'Always navigate to the project root folder where pom.xml is before running Maven commands'}
{'mistake': 'Not setting the WebDriver system property for the browser driver', 'why_bad': 'Selenium will throw an error because it cannot find the browser driver executable', 'correct_approach': "Set the system property 'webdriver.chrome.driver' (or appropriate) with the correct path before creating WebDriver instance"}
Hardcoding absolute paths for drivers or project files
Not including the surefire plugin or test dependencies in pom.xml
Bonus Challenge

Now add data-driven testing with 3 different login page URLs to verify their titles

Show Hint