Java · TestNG
Government services with Selenium
A six-section application with autosaved drafts you can resume, split date-of-birth dropdowns, conditional fields, “same as present address”, document uploads, a verification code, appointment booking and ARN tracking.
App under test
🏛️ GovServe
- URL
- /apps/gov
- Login
- citizen@govserve.in / Gov@123
- Login OTP
- 5821
- Form verification code
- 7439
- Uploads
- PDF/JPG ≤ 2 MB · photo JPG ≤ 1 MB
- Offices
- weekdays only
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Citizen login + OTP
BeginnerEmail + password, then OTP 5821. Opening a deep link such as /apps/gov/status signed out shows the login in place and returns to that page after sign-in.
- 2
Section validation
Beginnersection-next on an empty section shows section-errors plus an error-* per field.
- 3
Date dropdowns
Intermediate31 Feb or a future date in dob-day/month/year shows error-dob.
- 4
Conditional field
Beginnermarital-status “Married” reveals spouse-name, which becomes required.
- 5
Copy address
Intermediatesame-address copies present into permanent fields and disables that fieldset.
- 6
Upload validation
IntermediateUpload a .txt or >2 MB file to address-proof-file to see the type/size error; valid files show *-name.
- 7
Autosave & resume
Advanceddraft-status goes “Saving draft…” → “✓ Draft saved”; save-exit, then resume-<id> restores the section and values.
- 8
Review & edit
IntermediateOn the last step, edit-section-N jumps back; review-* rows reflect your answers.
- 9
Appointment & tracking
AdvancedPick an office, a weekday and a free appointment-slot; then track the ARN on the status page.
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 ×11 · By.name ×10 · By.xpath ×5 · By.cssSelector ×2 · data-testid ×16
public class GovServeApplicationTest {
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));
}
// File inputs take an absolute path via sendKeys (fixtures live in src/test/resources).
void upload(By by, String file) {
driver.findElement(by).sendKeys(new java.io.File("src/test/resources/" + file).getAbsolutePath());
}
@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 appliesForAPassportAndBooksAnAppointment() {
driver.get(BASE + "/apps/gov");
// Sign in first
type(By.id("gs-login-id"), "citizen@govserve.in");
type(By.name("gs-password"), "Gov@123");
click(By.xpath("//button[normalize-space()='Continue']"));
type(tid("gs-code"), "5821");
click(By.xpath("//button[normalize-space()='Verify & continue']"));
click(tid("start-application"));
wait.until(urlContains("/apps/gov/apply/"));
// 1. Personal
type(By.id("given-name"), "Amir");
type(By.name("surname"), "Khan");
select(By.cssSelector("[aria-label='Day']"), "15");
select(By.cssSelector("[aria-label='Month']"), "8");
select(By.id("dob-year"), "1990");
click(By.xpath("//button[normalize-space()='Male']"));
type(By.name("birth-place"), "Pune");
select(tid("marital-status"), "married");
type(By.id("spouse-name"), "Sara Khan");
click(tid("section-next"));
// 2. Address
type(By.name("house"), "12B");
type(By.id("street"), "Palm Residency, Baner");
type(By.name("city"), "Pune");
select(tid("state"), "Maharashtra");
type(By.id("pincode"), "411045");
check(tid("same-address"));
click(tid("section-next"));
// 3. Family, 4. Emergency contact
type(By.name("father-name"), "Imran Khan");
type(By.id("mother-name"), "Farah Khan");
click(tid("section-next"));
type(By.name("emergency-name"), "Sara Khan");
type(By.id("emergency-phone"), "9812345678");
select(By.name("emergency-relation"), "spouse");
click(tid("section-next"));
// 5. Documents
select(By.id("proof-type"), "aadhaar");
upload(By.id("address-proof-file"), "address-proof.pdf");
upload(tid("photo-file"), "photo.jpg");
expectText(tid("photo-file-name"), "photo.jpg");
click(tid("section-next"));
// 6. Review & submit
expectText(tid("review-name"), "Amir Khan");
check(By.id("declaration"));
type(By.name("captcha-input"), "7439");
click(By.xpath("//button[normalize-space()='Submit application']"));
expectText(tid("arn"), "ARN-26");
select(By.name("office"), "psk-pune");
click(By.cssSelector("[data-testid=\"appointment-day\"]:not([disabled])"));
click(By.cssSelector("[data-testid=\"appointment-slot\"][data-available=\"true\"]"));
click(By.xpath("//button[normalize-space()='Confirm appointment']"));
expectVisible(tid("appointment-receipt"));
expectText(tid("receipt-name"), "Amir Khan");
}
}