0
0
Selenium Javatesting~10 mins

Maven build lifecycle in Selenium Java - Test Execution Trace

Choose your learning style9 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.