0
0
Selenium Javatesting~10 mins

Running tests via Maven in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a Selenium WebDriver test using Maven. It opens a browser, navigates to a website, clicks a button, and verifies the page title. The test verifies that Maven can successfully execute Selenium tests.

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.assertEquals;

public class MavenSeleniumTest {
    private WebDriver driver;

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

    @Test
    public void testButtonClickChangesTitle() {
        driver.get("https://example.com");
        WebElement button = driver.findElement(By.id("start-button"));
        button.click();
        String expectedTitle = "Welcome Page";
        assertEquals(expectedTitle, driver.getTitle());
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

// To run this test via Maven, use the command:
// mvn test
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Maven starts test execution with 'mvn test' commandMaven initializes and compiles the test code-PASS
2JUnit launches and runs the test method 'testButtonClickChangesTitle'Test environment ready, ChromeDriver instance created-PASS
3WebDriver opens Chrome browserChrome browser window is open and ready-PASS
4WebDriver navigates to 'https://example.com'Browser displays the example.com homepage-PASS
5WebDriver finds the button with id 'start-button'Button element is located on the page-PASS
6WebDriver clicks the 'start-button'Page updates after button click-PASS
7Test asserts that the page title is 'Welcome Page'Browser title is checkedassertEquals('Welcome Page', driver.getTitle())PASS
8WebDriver closes the browserBrowser window is closed-PASS
9JUnit finishes test and Maven reports successTest suite completes with no errors-PASS
Failure Scenario
Failing Condition: The element with id 'start-button' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What command starts the test execution in this Maven setup?
Amvn compile
Bjava -jar selenium.jar
Cmvn test
Dselenium-server start
Key Result
Always verify that the elements your test interacts with exist on the page and are accessible before performing actions. Using Maven to run tests ensures consistent and repeatable test execution.