Behavior-driven development (BDD) concept in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different username and password combinations
Practice
Solution
Step 1: Understand BDD's communication goal
BDD uses simple language to describe how software should behave, making it easier for everyone to understand.Step 2: Compare options with BDD purpose
Only To improve communication by using plain language to describe software behavior matches this goal; others focus on unrelated aspects like speed or UI only.Final Answer:
To improve communication by using plain language to describe software behavior -> Option BQuick Check:
BDD = plain language communication [OK]
- Thinking BDD is only about automation
- Confusing BDD with UI design
- Believing BDD skips manual testing
Solution
Step 1: Recall BDD scenario structure
BDD scenarios use the "Given-When-Then" format to describe context, action, and outcome.Step 2: Match options with BDD syntax
Only Given-When-Then matches this format; others are programming constructs or unrelated terms.Final Answer:
Given-When-Then -> Option DQuick Check:
BDD syntax = Given-When-Then [OK]
- Confusing BDD syntax with programming keywords
- Using If-Else as BDD steps
- Mixing error handling terms with BDD
Given the user is logged in When the user clicks the logout button Then the user should be logged out
What is the expected outcome of this scenario?
Solution
Step 1: Analyze the 'When' action
The user clicks the logout button, which triggers logout behavior.Step 2: Check the 'Then' expected result
The user should be logged out, meaning the session ends successfully.Final Answer:
The user is logged out after clicking logout -> Option AQuick Check:
When logout clicked, Then user logged out [OK]
- Ignoring the 'Then' step result
- Assuming logout button disables instead
- Confusing error messages with normal flow
Given the user is on the homepage When the user clicks the login button And the user enters valid credentials Then the user should see the dashboard But the user sees an error message instead
Solution
Step 1: Review 'Then' and 'But' steps
'Then' states the user should see the dashboard (expected), but 'But' says the user sees an error (unexpected).Step 2: Understand BDD scenario clarity
BDD scenarios should describe one clear expected outcome; mixing expected and unexpected causes confusion.Final Answer:
The scenario mixes expected and unexpected outcomes in 'Then' and 'But' -> Option AQuick Check:
BDD expects clear outcomes, not conflicting ones [OK]
- Thinking multiple actions in 'When' are invalid
- Believing 'Background' is mandatory
- Assuming 'Given' must include URLs
Solution
Step 1: Identify proper BDD format
Given the cart total is $120 When the user applies a discount code Then the discount should be applied uses Given-When-Then steps clearly describing context, action, and expected result.Step 2: Evaluate other options for BDD style
Options B, C, and D use programming logic or vague steps, not BDD's plain language format.Final Answer:
Given the cart total is $120 When the user applies a discount code Then the discount should be applied -> Option CQuick Check:
BDD scenario = Given-When-Then clear steps [OK]
- Writing code logic instead of plain language
- Skipping the 'Then' expected outcome
- Using vague or incomplete steps
Ready to prove it?
Pass the test and this pattern moves unknown
