Run Selenium tests using Maven command
Preconditions (3)
✅ Expected Result: Maven runs the Selenium tests and shows test results summary in the console, indicating which tests passed or failed
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.
Now add data-driven testing with 3 different login page URLs to verify their titles
test phase runs tests.mvn test runs all tests in the project, including Selenium tests.mvn test [OK]mvn test to run all tests quickly [OK]mvn clean which only cleans filesmvn compile which only compiles codemvn install with running testsLoginTest?-Dtest=ClassName to run a specific test class.mvn test -Dtest=LoginTest runs only the LoginTest class.-Dtest=ClassName [OK]-Dtest=ClassName to run one test class [OK]-Dclass instead of -Dtestmvn run which is not a valid phasemvn test -Dtest=LoginTest, what will be the output if LoginTest has 3 passing Selenium tests?-Dtest effect-Dtest=LoginTest option runs only tests in the LoginTest class.LoginTest has 3 tests and all pass, only those 3 tests run and pass.-Dtest runs specified tests only [OK]-Dtest-Dtest is ignoredmvn test can't run specific testsmvn test -Dtest=LoginTest but get an error: Could not find or load main class LoginTest. What is the likely cause?LoginTest class, meaning the name or location is wrong.pom.xml cause different errors.pom.xml causes this errorSlowTests class using Maven. Which command achieves this?-Dtest, like -Dtest=!ClassName.mvn test -Dtest=!SlowTests runs all tests except SlowTests. Other options use invalid or unsupported properties.-Dtest=!ClassName [OK]-Dtest=!ClassName to exclude tests [OK]-DexcludedTests which is unsupportedexcludeTestsPass the test and this pattern moves unknown