By Shivam Singh, founder of TesterRank · Updated September 2026
Almost every real test suite starts with login. If login breaks, nothing else can be tested, so teams run it first in every build. A login test is also small enough to finish in one sitting, yet it uses every basic skill: opening the browser, finding fields, typing, clicking and checking the result.
rahul@netbank.com, password Bank@123.[data-testid='userId'] and [data-testid='password'].[data-testid='loginBtn'].[data-testid='errorMsg'].Time to put it all together. You're going to write a complete login automation script from scratch. Open the Banking Portal, enter credentials, click Sign In, and verify the login worked. This is the exact kind of exercise you'll get in automation interviews.
Try the exercise yourself first. Then compare your code with this solution: it opens the login page, signs in with the demo account and checks the dashboard loaded.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BankingLoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
try {
// Navigate to login page
driver.get("https://www.testerrank.com/banking/login");
System.out.println("Opened: " + driver.getTitle());
// Enter User ID
WebElement userId = driver.findElement(
By.cssSelector("[data-testid='userId']"));
userId.clear();
userId.sendKeys("rahul@netbank.com");
// Enter Password
WebElement password = driver.findElement(
By.cssSelector("[data-testid='password']"));
password.clear();
password.sendKeys("Bank@123");
// Click Sign In
driver.findElement(
By.cssSelector("[data-testid='loginBtn']")).click();
// Wait for page to load (we'll learn proper waits later)
Thread.sleep(2000);
// Verify login
String url = driver.getCurrentUrl();
if (url.contains("/banking/dashboard")) {
System.out.println("PASS — Login successful!");
// Read welcome message
WebElement welcome = driver.findElement(
By.cssSelector("[data-testid='welcomeUser']"));
System.out.println("Welcome: " + welcome.getText());
// Read account balance
WebElement balance = driver.findElement(
By.cssSelector("[data-testid='accBal1']"));
System.out.println("Balance: " + balance.getText());
} else {
System.out.println("FAIL — Not on dashboard");
// Check for error message
java.util.List<WebElement> errors = driver.findElements(
By.cssSelector("[data-testid='errorMsg']"));
if (!errors.isEmpty()) {
System.out.println("Error: " + errors.get(0).getText());
}
}
} catch (Exception e) {
System.out.println("FAIL — Exception: " + e.getMessage());
} finally {
driver.quit();
}
}
}Now try this on your own: go to the Shopping Portal catalog (/shopping/catalog), search for a product, filter by category, and add an item to the cart. Use what you learned about findElement, sendKeys, click, and getText.
Notice how we used data-testid attributes for all locators. In real projects, ask your developers to add data-testid attributes to critical elements. They're the most stable locators — they don't change when CSS classes or HTML structure changes.
A test that "runs without errors" is not the same as a test that checks something. Ask yourself: if login were broken, would my test fail? If the answer is no, you are missing an assertion. A good login test checks one clear thing, such as the URL changing away from /login or the welcome text appearing on the dashboard.
| Problem you see | Most likely cause | Fix |
|---|---|---|
| NoSuchElementException on the email field | Page not loaded yet, or wrong locator | Check the locator in DevTools, then add an explicit wait |
| Test passes even with a wrong password | No assertion after clicking login | Assert the URL or the dashboard text |
| Browser closes before you see anything | driver.quit() runs straight away | This is normal; add a screenshot if you need proof |
Write the negative test too: wrong password should show the error message and keep you on the login page. Interviewers often ask "what else would you test on a login page?", and having both tests ready is a strong answer.
Key Point: You just automated your first login flow. This exact pattern — navigate, find elements, interact, verify — is the foundation of every test you'll ever write.