Bird
Raised Fist0
Selenium Javatesting~10 mins

Maven build lifecycle in Selenium Java - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test runs a Selenium WebDriver test using Maven's build lifecycle. It verifies that the Maven phases compile the code, run tests, and package the application successfully.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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.assertTrue;

public class MavenLifecycleTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        // Assuming chromedriver is in system PATH
        driver = new ChromeDriver();
    }

    @Test
    public void testGoogleSearch() {
        driver.get("https://www.google.com");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("Maven build lifecycle");
        searchBox.submit();

        // Wait for title to contain search term
        for (int i = 0; i < 10; i++) {
            if (driver.getTitle().toLowerCase().contains("maven build lifecycle")) {
                break;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // ignore
            }
        }

        assertTrue(driver.getTitle().toLowerCase().contains("maven build lifecycle"));
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 11 Steps
StepActionSystem StateAssertionResult
1Maven starts build lifecycle: validate phaseProject structure and POM file are checked for correctness—PASS
2Maven runs compile phase to compile Java source codeJava source files compiled into target/classes directoryNo compilation errorsPASS
3Maven runs test-compile phase to compile test source codeTest Java files compiled into target/test-classes directoryNo compilation errors in test codePASS
4Maven runs test phase to execute testsJUnit test MavenLifecycleTest starts executionTest method testGoogleSearch() runsPASS
5Test opens Chrome browser using Selenium WebDriverChrome browser window opensBrowser is launched successfullyPASS
6Test navigates to https://www.google.comGoogle homepage is loadedPage title contains 'Google'PASS
7Test finds search input box by name 'q'Search box element is locatedElement is present and interactablePASS
8Test enters text 'Maven build lifecycle' and submits searchSearch results page loadsPage title contains 'maven build lifecycle'PASS
9Test asserts page title contains search termTitle verifiedassertTrue(title contains 'maven build lifecycle')PASS
10Test closes browserBrowser window closedDriver quit successfullyPASS
11Maven runs package phase to create JARTarget directory contains packaged JAR fileJAR file created without errorsPASS
Failure Scenario
Failing Condition: Search input element with name 'q' is not found on Google homepage
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after submitting the search?
AThat the page title contains 'maven build lifecycle'
BThat the search box is empty
CThat the browser closes immediately
DThat the URL changes to a different domain
Key Result
Use Maven's lifecycle phases to manage build steps clearly: compile, test-compile, test, and package. This ensures tests run on compiled code and packaging happens only after successful tests.

Practice

(1/5)
1. Which Maven build lifecycle phase is responsible for compiling the source code of a Java project?
easy
A. clean
B. test
C. package
D. compile

Solution

  1. Step 1: Understand Maven lifecycle phases

    Maven has phases like clean, compile, test, and package, each with a specific role.
  2. Step 2: Identify the phase for compiling source code

    The compile phase compiles the Java source code into bytecode.
  3. Final Answer:

    compile -> Option D
  4. Quick Check:

    Compile phase compiles code = compile [OK]
Hint: Compile phase always compiles source code [OK]
Common Mistakes:
  • Confusing 'test' phase with compiling code
  • Thinking 'package' compiles code
  • Assuming 'clean' compiles code
2. Which Maven command cleans the project by deleting the target directory?
easy
A. mvn clean
B. mvn compile
C. mvn package
D. mvn test

Solution

  1. Step 1: Recall Maven commands and their purposes

    mvn clean deletes the target directory, removing previous build files.
  2. Step 2: Match command to cleaning action

    Only mvn clean removes old build artifacts before a fresh build.
  3. Final Answer:

    mvn clean -> Option A
  4. Quick Check:

    Clean command deletes target folder = mvn clean [OK]
Hint: Clean command always removes old build files [OK]
Common Mistakes:
  • Using mvn package to clean project
  • Confusing mvn test with cleaning
  • Thinking mvn compile cleans files
3. What will happen if you run mvn test on a Maven project that has not been compiled yet?
medium
A. Tests will fail because code is not compiled
B. Maven will compile the code first, then run tests
C. Maven will skip compilation and run tests directly
D. Maven will only clean the project without running tests

Solution

  1. Step 1: Understand Maven lifecycle phase dependencies

    Running mvn test triggers all previous phases including compile automatically.
  2. Step 2: Predict behavior when code is not compiled

    Maven compiles code before running tests to ensure tests run on compiled classes.
  3. Final Answer:

    Maven will compile the code first, then run tests -> Option B
  4. Quick Check:

    Test phase runs compile first = compile then test [OK]
Hint: Test phase always runs compile first [OK]
Common Mistakes:
  • Assuming tests run without compiling
  • Thinking Maven skips compile phase
  • Confusing clean with test phase
4. You run mvn package but get an error saying tests failed. What is the best way to fix this without skipping tests?
medium
A. Run mvn package -DskipTests to skip tests
B. Run mvn compile only to compile code
C. Run mvn clean package to clean and rebuild
D. Delete the target folder manually and rerun mvn package

Solution

  1. Step 1: Understand the error cause

    Tests failed during package phase, so build stopped.
  2. Step 2: Choose a fix that cleans and rebuilds including tests

    Running mvn clean package removes old files and rebuilds fresh, running tests again.
  3. Final Answer:

    Run mvn clean package to clean and rebuild -> Option C
  4. Quick Check:

    Clean then package reruns tests = mvn clean package [OK]
Hint: Clean before package to fix test errors without skipping [OK]
Common Mistakes:
  • Skipping tests instead of fixing them
  • Only compiling without packaging
  • Manually deleting target folder is error-prone
5. You want to create a Maven build that cleans, compiles, runs tests, and packages your project in one command. Which command should you use?
hard
A. mvn clean package
B. mvn clean compile test package
C. mvn compile test package
D. mvn test package

Solution

  1. Step 1: Understand Maven lifecycle chaining

    Running a later phase runs all previous phases automatically.
  2. Step 2: Identify the shortest command that runs all phases

    mvn clean package runs clean, compile, test, and package in order.
  3. Final Answer:

    mvn clean package -> Option A
  4. Quick Check:

    Package phase runs all before it = mvn clean package [OK]
Hint: Run clean then package to do all steps in one [OK]
Common Mistakes:
  • Listing all phases explicitly is unnecessary
  • Skipping clean phase
  • Running test package without clean
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