Java · TestNG
Banking with Selenium
Two-step login with lockout, account dashboard, add a beneficiary, transfer funds with OTP, manager approval for large transfers, session timeout, a paginated statement and a buggy build.
App under test
🏦 TrustBank
- URL
- /apps/bank
- Customer ID
- 100200300
- Manager ID
- 900800700
- Password
- Bank@123
- Security answer
- greenwood
- OTP
- 246810
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Invalid login & lockout
BeginnerWrong password shows attempts left in login-error; the 3rd failure locks the form (reset-lock).
- 2
Two-step authentication
BeginnerAfter step 1, security-question appears; a wrong answer keeps you on step 2.
- 3
Route guard
BeginnerOpening /apps/bank/dashboard signed out redirects to the login page.
- 4
Add a payee
IntermediateValidate IFSC pattern and matching account numbers; pasting into the confirm field is blocked.
- 5
Transfer rules
IntermediateBalance, daily limit (₹50,000) and RTGS minimum each raise their own error-* message.
- 6
OTP step
IntermediateWrong OTP shows otp-error; 246810 completes the transfer after a processing overlay.
- 7
Balance reconciliation
Advancednew-balance on the receipt must equal the old balance minus the amount, and match the dashboard.
- 8
Statement filters & paging
AdvancedCombine history-* filters, walk page-next until disabled, export download-statement CSV.
- 9
Session timeout
AdvancedMock the clock 10 minutes ahead: you're logged out and see session-expired.
- 10
Maker and checker
Advanced₹25,000 or more shows approval-notice and ends on transfer-pending with the balance unchanged; manager 900800700 approves or rejects (10+ character reason) in approvals-table.
- 11
Role-based access
IntermediateThe customer sees access-denied on /apps/bank/approvals; the manager sees it on /apps/bank/transfer.
- 12
Bug hunt
AdvancedCustomer ID 100200399 has 5 seeded defects (daily limit, receipt balance, debit filter, CSV, OTP) — catch each with a test.
- 13
UI vs API
AdvancedPOST /api/bank/transfer-check returns the same error text as the form; GET /api/bank/rules lists the limits.
Sample Selenium suite
A working starting point that finds elements every way Selenium can — by role, label, text, id, name, CSS, XPath and data-testid — so you practice them all. Pick your language, switch to data-testid only to compare, or try your own in the Locator Playground.
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.Map;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
// Locators in this suite: By.id ×5 · By.name ×5 · By.xpath ×6 · By.cssSelector ×1 · data-testid ×9
public class TrustBankTransferTest {
static final String BASE = "https://www.practiceqaautomation.com";
WebDriver driver;
WebDriverWait wait;
// Small helpers: every action takes a By and waits for its element first (no Thread.sleep).
By tid(String id) { return By.cssSelector("[data-testid='" + id + "']"); }
WebElement ready(By by) { return centre(wait.until(elementToBeClickable(by))); }
void click(By by) { ready(by).click(); }
WebElement nth(String id, int n) { return centre(wait.until(numberOfElementsToBeMoreThan(tid(id), n)).get(n)); }
void clickNth(String id, int n) { nth(id, n).click(); }
// Scroll to the middle of the screen first: Selenium only scrolls an element to the edge,
// where a sticky header or a fixed bottom bar can cover it (ElementClickInterceptedException).
WebElement centre(WebElement e) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center', inline: 'nearest'})", e);
return e;
}
void type(By by, String text) { ready(by).sendKeys(text); }
void select(By by, String value) { new Select(ready(by)).selectByValue(value); }
void check(By by) { WebElement e = ready(by); if (!e.isSelected()) e.click(); }
void expectText(By by, String text) { wait.until(textToBePresentInElementLocated(by, text)); }
void expectVisible(By by) { wait.until(visibilityOfElementLocated(by)); }
void expectNthText(String id, int n, String text) {
wait.until(d -> d.findElements(tid(id)).size() > n && d.findElements(tid(id)).get(n).getText().contains(text));
}
@BeforeMethod
public void setUp() {
ChromeOptions options = new ChromeOptions();
// Turn off Chrome's "Save password?" and "password found in a data breach" pop-ups: after a
// login they take the keyboard and mouse away from the page, so later typing and clicks are lost.
options.setExperimentalOption("prefs", Map.of(
"credentials_enable_service", false,
"profile.password_manager_enabled", false,
"profile.password_manager_leak_detection", false));
driver = new ChromeDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@AfterMethod(alwaysRun = true)
public void tearDown() { driver.quit(); }
@Test
public void transfersMoneyToANewPayee() {
driver.get(BASE + "/apps/bank");
type(By.id("customer-id"), "100200300");
type(By.name("bank-password"), "Bank@123");
click(By.xpath("//button[normalize-space()='Continue']"));
expectVisible(tid("security-question"));
type(By.id("security-answer"), "greenwood");
click(By.xpath("//button[normalize-space()='Sign in']"));
wait.until(urlContains("/apps/bank/dashboard"));
expectText(tid("customer-name"), "Amir Khan");
click(tid("action-transfer"));
wait.until(urlContains("/apps/bank/transfer"));
select(By.name("from-account"), "savings");
// Add a beneficiary in the modal
click(tid("add-payee"));
type(By.id("payee-name"), "Ravi Kumar");
type(By.name("payee-account"), "123456789012");
type(By.id("payee-confirm-account"), "123456789012");
type(By.name("payee-ifsc"), "TRST0001234");
click(By.xpath("//button[normalize-space()='Save payee']"));
type(By.id("amount"), "2500");
click(tid("mode-imps"));
type(By.name("remarks"), "Rent");
click(By.xpath("//button[normalize-space()='Review transfer']"));
expectText(tid("review-payee"), "Ravi Kumar");
click(By.xpath("//button[normalize-space()='Confirm']"));
type(By.cssSelector("[aria-label='One-time password']"), "246810");
click(By.xpath("//button[normalize-space()='Verify & transfer']"));
expectVisible(tid("transfer-success"));
expectText(tid("transfer-amount"), "2,500.00");
expectText(tid("new-balance"), "1,22,930.50");
}
}