Dependency between tests in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class DependencyBetweenTests { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testLogin() { driver.get("https://example.com/login"); WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameField.sendKeys("testuser"); WebElement passwordField = driver.findElement(By.id("password")); passwordField.sendKeys("Test@1234"); WebElement loginButton = driver.findElement(By.id("loginBtn")); loginButton.click(); wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard")); Assert.assertEquals(driver.getCurrentUrl(), "https://example.com/dashboard", "URL after login should be dashboard"); } @Test(dependsOnMethods = {"testLogin"}) public void testProfileUpdate() { WebElement profileLink = wait.until(ExpectedConditions.elementToBeClickable(By.id("profileLink"))); profileLink.click(); WebElement profileNameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("profileName"))); profileNameField.clear(); profileNameField.sendKeys("Test User Updated"); WebElement saveButton = driver.findElement(By.id("saveProfileBtn")); saveButton.click(); WebElement successMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("successMsg"))); String successText = successMsg.getText(); Assert.assertTrue(successText.contains("Profile updated successfully"), "Success message should confirm profile update"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
The setUp method initializes the Chrome WebDriver and sets an explicit wait for 10 seconds.
The testLogin method opens the login page, enters username and password, clicks login, and waits until the URL changes to the dashboard URL. It asserts the URL to confirm successful login.
The testProfileUpdate method depends on testLogin using @Test(dependsOnMethods = {"testLogin"}). This ensures it runs only if login passes. It clicks the profile link, updates the profile name, clicks save, and asserts the success message is displayed.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
The tearDown method closes the browser after tests finish.
This structure shows how to create test dependencies in TestNG and use Selenium best practices.
Now add data-driven testing with 3 different user credentials for login and profile names
Practice
dependsOnMethods in Selenium Java tests?Solution
Step 1: Understand test dependencies
ThedependsOnMethodsattribute is used to specify that a test should run only after another test method has successfully completed.Step 2: Recognize the effect on test execution order
This ensures that dependent tests do not run if the test they depend on fails, maintaining logical test flow.Final Answer:
To make one test run only after another test has passed -> Option DQuick Check:
dependsOnMethods controls test order and dependency [OK]
- Thinking dependsOnMethods runs tests in parallel
- Assuming it retries failed tests automatically
- Believing it skips tests without assertions
testB depend on testA in TestNG?Solution
Step 1: Recall TestNG annotation syntax
The correct attribute name isdependsOnMethodsand it expects an array of method names as strings.Step 2: Check the syntax correctness
@Test(dependsOnMethods = {"testA"}) public void testB() {} correctly uses@Test(dependsOnMethods = {"testA"})with the method name in quotes inside braces.Final Answer:
@Test(dependsOnMethods = {"testA"}) public void testB() {} -> Option BQuick Check:
dependsOnMethods requires string array syntax [OK]
- Using dependsOn instead of dependsOnMethods
- Omitting quotes around method name
- Not using braces for the array
@Test
public void login() { Assert.assertTrue(false); }
@Test(dependsOnMethods = {"login"})
public void dashboard() { System.out.println("Dashboard loaded"); }What will happen when these tests run?
Solution
Step 1: Analyze login test result
Thelogintest asserts false, so it will fail.Step 2: Understand effect on dependent test
Thedashboardtest depends onlogin. Sinceloginfailed,dashboardwill be skipped and not run.Final Answer:
login fails, dashboard is skipped -> Option AQuick Check:
Dependent tests skip if dependency fails [OK]
- Assuming dependent test runs despite failure
- Thinking both tests pass
- Confusing skip with fail
@Test(dependsOnMethods = "setup")
public void testFeature() {}
@Test
public void setup() {}Solution
Step 1: Check dependsOnMethods syntax
The attributedependsOnMethodsrequires an array of method names, even if only one method is listed.Step 2: Identify the syntax mistake
Here,dependsOnMethods = "setup"is a single string, missing braces to make it an array.Final Answer:
dependsOnMethods should be an array of strings, not a single string -> Option CQuick Check:
dependsOnMethods needs braces for method names [OK]
- Using single string instead of string array
- Making dependent method private
- Adding parameters to setup method unnecessarily
init(), login(), and purchase(). login() depends on init(), and purchase() depends on login(). If init() fails, what happens when you run all tests together?Solution
Step 1: Understand the dependency chain
login depends on init, and purchase depends on login, forming a chain: init -> login -> purchase.Step 2: Analyze failure impact
If init fails, login will be skipped because it depends on init. Since login is skipped, purchase will also be skipped because it depends on login.Final Answer:
init fails, login and purchase are skipped -> Option AQuick Check:
Failure cascades down dependent tests [OK]
- Assuming dependent tests run despite failure
- Thinking only immediate dependent test skips
- Believing tests run independently
Ready to prove it?
Pass the test and this pattern moves unknown
