0
0
Selenium Javatesting~15 mins

Dependency between tests in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify user profile update depends on successful login
Preconditions (2)
Step 1: Open the login page at 'https://example.com/login'
Step 2: Enter 'testuser' in the username field with id 'username'
Step 3: Enter 'Test@1234' in the password field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Verify that the URL changes to 'https://example.com/dashboard'
Step 6: Navigate to the profile page by clicking the profile link with id 'profileLink'
Step 7: Update the profile name field with id 'profileName' to 'Test User Updated'
Step 8: Click the save button with id 'saveProfileBtn'
Step 9: Verify that a success message with id 'successMsg' is displayed containing text 'Profile updated successfully'
✅ Expected Result: User can only update profile after successful login. Profile update shows success message.
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Assert URL after login is 'https://example.com/dashboard'
Assert success message text after profile update
Best Practices:
Use @Test(dependsOnMethods = ...) to create dependency between login and profile update tests
Use explicit waits to wait for elements to be visible or clickable
Use Page Object Model to separate page interactions
Use assertions from TestNG Assert class
Automated Solution
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.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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not using dependsOnMethods and running dependent tests independently', 'why_bad': 'Profile update test may run without login, causing failures and false negatives.', 'correct_approach': 'Use @Test(dependsOnMethods = {"testLogin"}) to ensure profile update runs only after successful login.'}
Using Thread.sleep instead of explicit waits
Hardcoding URLs or element locators without validation
Bonus Challenge

Now add data-driven testing with 3 different user credentials for login and profile names

Show Hint