Java · TestNG
Insurance with Selenium
Vehicle details with dependent dropdowns, plan comparison with an IDV slider and add-on switches, a proposal with nominee, and payment inside an iframe gateway.
App under test
🛡️ SecureLife
- URL
- /apps/insurance
- Reg. format
- MH12AB1234
- Gateway card
- any 16 digits
- Owner age
- 18+ (DD/MM/YYYY)
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Dependent dropdowns
Beginnervehicle-model is disabled until vehicle-make is chosen, and resets when the make changes.
- 2
Conditional field
Beginnerclaim-no reveals ncb; claim-yes hides it and removes the bonus from the premium.
- 3
Pattern validation
Intermediatereg-number and owner-dob reject bad formats with error-* messages; under-18 owners fail.
- 4
Plan comparison
IntermediateAssert premium-* for all three plans; third-party disables idv-slider and add-on switches.
- 5
Range slider
AdvancedMove idv-slider (keyboard arrows or fill) and verify idv-value and od-premium change.
- 6
Iframe payment
AdvancedSwitch into payment-gateway, test gw-error validation, pay, and switch back to the parent.
- 7
Cancel in the iframe
Advancedgw-cancel closes the gateway and shows payment-cancelled in the parent page.
- 8
Download policy
Intermediatedownload-policy saves a .txt; assert the file name contains the policy number.
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 ×6 · By.name ×5 · By.xpath ×3 · By.cssSelector ×1 · data-testid ×10
public class SecureLifePolicyTest {
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));
}
void inFrame(String frameId, Runnable action) {
driver.switchTo().frame(ready(tid(frameId)));
try { action.run(); } finally { driver.switchTo().defaultContent(); }
}
@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 buysACarInsurancePolicy() {
driver.get(BASE + "/apps/insurance");
type(By.id("reg-number"), "MH12AB1234");
select(By.name("vehicle-make"), "maruti");
select(tid("vehicle-model"), "swift");
click(tid("fuel-petrol"));
select(By.id("vehicle-year"), "2021");
click(By.xpath("//button[normalize-space()='No']"));
select(By.name("ncb"), "20");
click(By.xpath("//button[normalize-space()='View plans']"));
wait.until(urlContains("/apps/insurance/quote"));
click(tid("plan-comprehensive"));
click(tid("addon-roadside"));
expectText(tid("total-premium"), "14,442.00");
click(By.xpath("//button[normalize-space()='Buy this plan']"));
// Proposal and nominee
type(By.id("owner-name"), "Amir Khan");
type(By.cssSelector("[placeholder='DD/MM/YYYY']"), "15/08/1990");
type(By.name("owner-email"), "amir@example.com");
type(By.id("owner-phone"), "9876543210");
type(By.name("owner-address"), "12 Palm Street, Pune 411001");
type(By.id("nominee-name"), "Sara Khan");
select(By.name("nominee-relation"), "spouse");
type(By.id("nominee-age"), "32");
check(tid("declaration"));
click(tid("pay-premium"));
// Pay inside the iframe gateway
inFrame("payment-gateway", () -> type(tid("gw-card"), "4111111111111111"));
inFrame("payment-gateway", () -> type(tid("gw-expiry"), "1230"));
inFrame("payment-gateway", () -> type(tid("gw-cvv"), "123"));
inFrame("payment-gateway", () -> click(tid("gw-pay")));
expectVisible(tid("policy-issued"));
expectText(tid("policy-number"), "SL/2026/");
expectText(tid("policy-card-last4"), "1111");
}
}