Java · TestNG
Utilities with Selenium
OTP login with a resend countdown, a usage chart with hover tooltips, meter readings with slab-tariff bill maths, full or partial payments, an autopay mandate and outage reports.
App under test
⚡ PowerGrid
- URL
- /apps/utilities
- Consumer no.
- 1234567890
- OTP
- 111222
- Tariff
- ₹3 / ₹5 / ₹7.5 + ₹50
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
OTP & resend timer
Intermediateresend-otp stays disabled while resend-countdown ticks from 30 — mock the clock or wait for enabled.
- 2
Hover tooltips
IntermediateHover usage-bar-* to show usage-tooltip with units and amount (also works on keyboard focus).
- 3
Reading validation
BeginnerA reading below last-reading or non-numeric shows error-new-reading.
- 4
High-usage confirm
IntermediateMore than 1,000 units reveals confirm-high, which must be ticked to continue.
- 5
Slab maths
Advanced350 units → 100×3 + 200×5 + 50×7.5 + 50 = ₹1,725.00 in estimated-bill; slab-row lists each band.
- 6
Partial payments
Intermediatepay-partial below 50% of the due shows pay-error; a valid amount leaves remaining-due.
- 7
Autopay switch
IntermediateTurning autopay-toggle on opens mandate-modal; cancelling leaves it off.
- 8
Conditional warning
BeginnerChoosing “Sparking wire” shows safety-warning before submitting an outage.
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 ×3 · By.name ×3 · By.linkText ×2 · By.xpath ×5 · By.cssSelector ×1 · data-testid ×9
public class PowerGridBillTest {
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 submitsAReadingSetsAutopayAndPays() {
driver.get(BASE + "/apps/utilities");
type(By.id("consumer-number"), "1234567890");
click(By.xpath("//button[normalize-space()='Send OTP']"));
expectVisible(tid("otp-sent"));
expectText(tid("resend-otp"), "Resend OTP in");
type(By.name("utility-otp"), "111222");
click(By.xpath("//button[normalize-space()='Verify & sign in']"));
wait.until(urlContains("/apps/utilities/dashboard"));
type(By.id("new-reading"), "4870");
click(By.xpath("//button[normalize-space()='Calculate bill']"));
expectText(tid("units-consumed"), "350 kWh");
expectText(tid("estimated-bill"), "₹1,725.00");
expectText(tid("amount-due"), "₹1,725.00");
click(By.cssSelector("[aria-label='Autopay']"));
select(By.name("mandate-bank"), "TrustBank");
type(By.id("mandate-max"), "3000");
click(By.xpath("//button[normalize-space()='Authorise']"));
expectText(tid("autopay-status"), "Active via TrustBank");
click(By.linkText("Pay now"));
click(tid("pay-full"));
select(By.name("utility-method"), "upi");
click(By.xpath("//button[normalize-space()='Pay']"));
expectText(tid("paid-amount"), "₹1,725.00");
click(By.linkText("Back to dashboard"));
expectText(tid("bill-status"), "Paid");
}
}