0
0
Testing Fundamentalstesting~15 mins

Behavior-driven development (BDD) concept in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality using BDD style
Preconditions (1)
Step 1: Given the user is on the login page
Step 2: When the user enters username 'testuser' and password 'Test@1234'
Step 3: And clicks the login button
Step 4: Then the user should be redirected to the dashboard page
Step 5: And the welcome message 'Welcome testuser!' should be displayed
✅ Expected Result: User successfully logs in and sees the dashboard with the welcome message
Automation Requirements - Cucumber with Selenium WebDriver
Assertions Needed:
Verify current URL is dashboard URL
Verify welcome message text matches 'Welcome testuser!'
Best Practices:
Use Given-When-Then structure for test steps
Use Page Object Model for page interactions
Use explicit waits for elements to be visible before interacting
Keep test steps readable and reusable
Automated Solution
Testing Fundamentals
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.cucumber.java.en.*;
import static org.junit.Assert.*;

public class LoginSteps {
    private WebDriver driver;
    private WebDriverWait wait;

    public LoginSteps() {
        // Assume driver is initialized elsewhere and passed here
        this.driver = DriverManager.getDriver();
        this.wait = new WebDriverWait(driver, 10);
    }

    @Given("the user is on the login page")
    public void user_on_login_page() {
        driver.get("https://example.com/login");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-form")));
    }

    @When("the user enters username {string} and password {string}")
    public void user_enters_credentials(String username, String password) {
        WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        WebElement passwordField = driver.findElement(By.id("password"));
        usernameField.clear();
        usernameField.sendKeys(username);
        passwordField.clear();
        passwordField.sendKeys(password);
    }

    @When("clicks the login button")
    public void user_clicks_login() {
        WebElement loginButton = driver.findElement(By.id("login-button"));
        loginButton.click();
    }

    @Then("the user should be redirected to the dashboard page")
    public void verify_dashboard_url() {
        wait.until(ExpectedConditions.urlContains("/dashboard"));
        String currentUrl = driver.getCurrentUrl();
        assertTrue("URL should contain /dashboard", currentUrl.contains("/dashboard"));
    }

    @Then("the welcome message {string} should be displayed")
    public void verify_welcome_message(String expectedMessage) {
        WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcome-message")));
        assertEquals(expectedMessage, welcomeMsg.getText());
    }
}

This code uses Cucumber annotations to map BDD steps to Java methods.

@Given opens the login page and waits for the form to appear.

@When enters username and password with explicit waits to ensure fields are ready.

@When clicks the login button.

@Then verifies the URL contains '/dashboard' to confirm redirection.

@Then checks the welcome message text matches the expected string.

Explicit waits ensure elements are ready before interaction, avoiding flaky tests.

Assertions confirm the expected outcomes clearly.

This structure follows BDD style with readable steps matching the manual test case.

Common Mistakes - 4 Pitfalls
Not using explicit waits before interacting with elements
Hardcoding URLs or messages inside test methods
Mixing test logic and UI interaction code in step definitions
Skipping assertions or verifying only partial outcomes
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations

Show Hint