We use Maven to easily run and manage Selenium tests. It helps run tests with one command and handles dependencies automatically.
Running tests via Maven in 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.
mvn test
LoginTest class tests.mvn -Dtest=LoginTest test
testValidLogin method inside LoginTest.mvn -Dtest=LoginTest#testValidLogin testThis 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.
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] ------------------------------------------------------------------------
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.
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.
Practice
Solution
Step 1: Understand Maven lifecycle phases
Maven has phases like clean, compile, test, package, install. Thetestphase runs tests.Step 2: Identify command for running tests
The commandmvn testruns all tests in the project, including Selenium tests.Final Answer:
mvn test -> Option AQuick Check:
Maven test phase =mvn test[OK]
mvn test to run all tests quickly [OK]- Using
mvn cleanwhich only cleans files - Using
mvn compilewhich only compiles code - Confusing
mvn installwith running tests
LoginTest?Solution
Step 1: Recall Maven syntax for running specific tests
Maven uses-Dtest=ClassNameto run a specific test class.Step 2: Match correct command format
mvn test -Dtest=LoginTestruns only theLoginTestclass.Final Answer:
mvn test -Dtest=LoginTest -> Option BQuick Check:
Run single test =-Dtest=ClassName[OK]
-Dtest=ClassName to run one test class [OK]- Using
-Dclassinstead of-Dtest - Using
mvn runwhich is not a valid phase - Trying to run tests during compile phase
mvn test -Dtest=LoginTest, what will be the output if LoginTest has 3 passing Selenium tests?Solution
Step 1: Understand
The-Dtesteffect-Dtest=LoginTestoption runs only tests in theLoginTestclass.Step 2: Interpret test results
IfLoginTesthas 3 tests and all pass, only those 3 tests run and pass.Final Answer:
Only 3 tests from LoginTest run and all pass -> Option CQuick Check:
-Dtestruns specified tests only [OK]
- Thinking all tests run despite
-Dtest - Assuming
-Dtestis ignored - Believing
mvn testcan't run specific tests
mvn test -Dtest=LoginTest but get an error: Could not find or load main class LoginTest. What is the likely cause?Solution
Step 1: Analyze error message
The error says it cannot find theLoginTestclass, meaning the name or location is wrong.Step 2: Check common causes
Misspelling the class name or incorrect package path causes this error. Maven supports running specific tests, so A is wrong. Compile errors or missingpom.xmlcause different errors.Final Answer:
The test class name is misspelled or not in the correct package -> Option DQuick Check:
Class not found = wrong name or package [OK]
- Assuming Maven can't run specific tests
- Ignoring compilation errors
- Thinking missing
pom.xmlcauses this error
SlowTests class using Maven. Which command achieves this?Solution
Step 1: Recall Maven syntax for excluding tests
Maven allows excluding tests by prefixing the class name with an exclamation mark in-Dtest, like-Dtest=!ClassName.Step 2: Match correct command
mvn test -Dtest=!SlowTestsruns all tests exceptSlowTests. Other options use invalid or unsupported properties.Final Answer:
mvn test -Dtest=!SlowTests -> Option AQuick Check:
Exclude test with-Dtest=!ClassName[OK]
-Dtest=!ClassName to exclude tests [OK]- Using
-DexcludedTestswhich is unsupported - Using wrong property names like
excludeTests - Trying to exclude tests without exclamation mark
Ready to prove it?
Pass the test and this pattern moves unknown
