0
0
Selenium Javatesting~10 mins

Dependency between tests in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test suite demonstrates dependency between tests using Selenium in Java. The first test logs in a user, and the second test depends on the login test to verify the user profile page.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.junit.jupiter.api.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DependencyTest {
    static WebDriver driver;

    @BeforeAll
    public static void setup() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    @Order(1)
    public void testLogin() {
        driver.get("https://example.com/login");
        WebElement username = driver.findElement(By.id("username"));
        WebElement password = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        username.sendKeys("testuser");
        password.sendKeys("password123");
        loginButton.click();

        WebElement welcomeMessage = driver.findElement(By.id("welcomeMsg"));
        assertTrue(welcomeMessage.isDisplayed(), "Welcome message should be visible after login");
    }

    @Test
    @Order(2)
    public void testUserProfile() {
        // This test depends on successful login
        driver.get("https://example.com/profile");
        WebElement profileHeader = driver.findElement(By.tagName("h1"));
        assertEquals("User Profile", profileHeader.getText(), "Profile page header should be 'User Profile'");
    }

    @AfterAll
    public static void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test suite setup: Launch Chrome browser and maximize windowChrome browser opened, blank page-PASS
2Navigate to login page at https://example.com/loginLogin page loaded with username, password fields and login button-PASS
3Find username, password input fields and login buttonElements located by id: username, password, loginBtn-PASS
4Enter username 'testuser' and password 'password123', then click login buttonLogin form submitted-PASS
5Find welcome message element by id 'welcomeMsg'Welcome message displayed on pageVerify welcome message is visiblePASS
6Navigate to user profile page at https://example.com/profileProfile page loaded with header-PASS
7Find profile page header element by tag name 'h1'Header element found with text 'User Profile'Verify header text equals 'User Profile'PASS
8Test suite teardown: Close browserBrowser closed-PASS
Failure Scenario
Failing Condition: Login test fails causing user not to be logged in before profile test runs
Execution Trace Quiz - 3 Questions
Test your understanding
What ensures that the user profile test runs only after the login test?
AThe profile test calls the login test method explicitly
BThe use of WebDriverWait in the profile test
CThe @Order annotation specifying test execution order
DJUnit automatically runs tests in alphabetical order
Key Result
Use test ordering annotations to manage dependencies between tests, ensuring prerequisite tests run first to set up required state.