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.
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.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test suite setup: Launch Chrome browser and maximize window | Chrome browser opened, blank page | — | PASS |
| 2 | Navigate to login page at https://example.com/login | Login page loaded with username, password fields and login button | — | PASS |
| 3 | Find username, password input fields and login button | Elements located by id: username, password, loginBtn | — | PASS |
| 4 | Enter username 'testuser' and password 'password123', then click login button | Login form submitted | — | PASS |
| 5 | Find welcome message element by id 'welcomeMsg' | Welcome message displayed on page | Verify welcome message is visible | PASS |
| 6 | Navigate to user profile page at https://example.com/profile | Profile page loaded with header | — | PASS |
| 7 | Find profile page header element by tag name 'h1' | Header element found with text 'User Profile' | Verify header text equals 'User Profile' | PASS |
| 8 | Test suite teardown: Close browser | Browser closed | — | PASS |
dependsOnMethods in Selenium Java tests?dependsOnMethods attribute is used to specify that a test should run only after another test method has successfully completed.testB depend on testA in TestNG?dependsOnMethods and it expects an array of method names as strings.@Test(dependsOnMethods = {"testA"}) with the method name in quotes inside braces.@Test
public void login() { Assert.assertTrue(false); }
@Test(dependsOnMethods = {"login"})
public void dashboard() { System.out.println("Dashboard loaded"); }login test asserts false, so it will fail.dashboard test depends on login. Since login failed, dashboard will be skipped and not run.@Test(dependsOnMethods = "setup")
public void testFeature() {}
@Test
public void setup() {}dependsOnMethods requires an array of method names, even if only one method is listed.dependsOnMethods = "setup" is a single string, missing braces to make it an array.init(), login(), and purchase(). login() depends on init(), and purchase() depends on login(). If init() fails, what happens when you run all tests together?Pass the test and this pattern moves unknown