Bird
Raised Fist0
Selenium Javatesting~15 mins

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

Choose your learning style10 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

Practice

(1/5)
1. What is the basic Maven command to run all Selenium tests in a project?
easy
A. mvn test
B. mvn clean
C. mvn compile
D. mvn install

Solution

  1. Step 1: Understand Maven lifecycle phases

    Maven has phases like clean, compile, test, package, install. The test phase runs tests.
  2. Step 2: Identify command for running tests

    The command mvn test runs all tests in the project, including Selenium tests.
  3. Final Answer:

    mvn test -> Option A
  4. Quick Check:

    Maven test phase = mvn test [OK]
Hint: Use mvn test to run all tests quickly [OK]
Common Mistakes:
  • Using mvn clean which only cleans files
  • Using mvn compile which only compiles code
  • Confusing mvn install with running tests
2. Which Maven command runs a specific Selenium test class named LoginTest?
easy
A. mvn run -Dtest=LoginTest
B. mvn test -Dtest=LoginTest
C. mvn test -Dclass=LoginTest
D. mvn compile -Dtest=LoginTest

Solution

  1. Step 1: Recall Maven syntax for running specific tests

    Maven uses -Dtest=ClassName to run a specific test class.
  2. Step 2: Match correct command format

    mvn test -Dtest=LoginTest runs only the LoginTest class.
  3. Final Answer:

    mvn test -Dtest=LoginTest -> Option B
  4. Quick Check:

    Run single test = -Dtest=ClassName [OK]
Hint: Use -Dtest=ClassName to run one test class [OK]
Common Mistakes:
  • Using -Dclass instead of -Dtest
  • Using mvn run which is not a valid phase
  • Trying to run tests during compile phase
3. Given the Maven command mvn test -Dtest=LoginTest, what will be the output if LoginTest has 3 passing Selenium tests?
medium
A. No tests run because -Dtest is ignored
B. All tests run, but only 3 tests from LoginTest pass
C. Only 3 tests from LoginTest run and all pass
D. Tests fail because mvn test can't run specific tests

Solution

  1. Step 1: Understand -Dtest effect

    The -Dtest=LoginTest option runs only tests in the LoginTest class.
  2. Step 2: Interpret test results

    If LoginTest has 3 tests and all pass, only those 3 tests run and pass.
  3. Final Answer:

    Only 3 tests from LoginTest run and all pass -> Option C
  4. Quick Check:

    -Dtest runs specified tests only [OK]
Hint: Specific test runs only those tests, others skipped [OK]
Common Mistakes:
  • Thinking all tests run despite -Dtest
  • Assuming -Dtest is ignored
  • Believing mvn test can't run specific tests
4. You run mvn test -Dtest=LoginTest but get an error: Could not find or load main class LoginTest. What is the likely cause?
medium
A. Maven does not support running specific tests
B. The pom.xml file is missing
C. You forgot to compile the project before testing
D. The test class name is misspelled or not in the correct package

Solution

  1. Step 1: Analyze error message

    The error says it cannot find the LoginTest class, meaning the name or location is wrong.
  2. 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 missing pom.xml cause different errors.
  3. Final Answer:

    The test class name is misspelled or not in the correct package -> Option D
  4. Quick Check:

    Class not found = wrong name or package [OK]
Hint: Check test class spelling and package path first [OK]
Common Mistakes:
  • Assuming Maven can't run specific tests
  • Ignoring compilation errors
  • Thinking missing pom.xml causes this error
5. You want to run all Selenium tests but exclude the SlowTests class using Maven. Which command achieves this?
hard
A. mvn test -Dtest=!SlowTests
B. mvn test -DexcludedTests=SlowTests
C. mvn test -Dtest='*' -DexcludedTests=SlowTests
D. mvn test -Dtest='*' -DexcludeTests=SlowTests

Solution

  1. 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.
  2. Step 2: Match correct command

    mvn test -Dtest=!SlowTests runs all tests except SlowTests. Other options use invalid or unsupported properties.
  3. Final Answer:

    mvn test -Dtest=!SlowTests -> Option A
  4. Quick Check:

    Exclude test with -Dtest=!ClassName [OK]
Hint: Use -Dtest=!ClassName to exclude tests [OK]
Common Mistakes:
  • Using -DexcludedTests which is unsupported
  • Using wrong property names like excludeTests
  • Trying to exclude tests without exclamation mark
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard