PracticeQAAutomation logoPracticeQAAutomation
Se

Java · TestNG

HR with Selenium

Clock in with a live timer, apply for leave with weekend-aware day counts and half days, approve as the HR manager, watch the balance drop, browse a paginated directory and download payslips.

App under test

👥 PeopleHub

URL
/apps/hr
Employee
emp / Hr@123
HR manager
hrmanager / Hr@123
Casual leave
8 days

Scenarios to automate

Work top to bottom — each one adds a new technique.

  1. 1

    Role-based pages

    Beginner

    The employee sees access-denied on Approvals; the manager can't open Apply leave.

  2. 2

    Clock in/out

    Beginner

    clock-in starts worked-time ticking; clock-out records last-session.

  3. 3

    Disabled weekends

    Intermediate

    Saturday/Sunday chips in leave-from/leave-to are disabled; leave-to before leave-from is disabled too.

  4. 4

    Working-day maths

    Intermediate

    A Fri → Mon range counts 2 days in leave-days; half-day (same day only) counts 0.5.

  5. 5

    Validation rules

    Intermediate

    Exceeding the balance, overlapping an existing request or a short reason each show error-*.

  6. 6

    Approval workflow

    Advanced

    Employee submits → manager approves → leave-status Approved and balance-* drops by leave-days.

  7. 7

    Reject with comment

    Advanced

    reject-leave requires a reason; the employee sees it under their request.

  8. 8

    Directory paging

    Beginner

    Filter by department, page with page-next, open an employee-modal and read employee-email.

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.

Language
PeopleHubLeaveTest.java
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 ×3 · By.linkText ×1 · By.xpath ×7 · data-testid ×8
public class PeopleHubLeaveTest {
  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 appliesForLeaveAndGetsItApproved() {
    driver.get(BASE + "/apps/hr");
    type(By.id("hr-username"), "emp");
    type(By.name("hr-password"), "Hr@123");
    click(By.xpath("//button[normalize-space()='Sign in']"));
    expectText(tid("balance-casual"), "8");
    click(By.xpath("//button[normalize-space()='Clock in']"));
    expectText(tid("attendance-status"), "Clocked in");
    click(tid("apply-leave"));
    select(By.id("leave-type"), "casual");

    // First working day for both ends → a single-day, half-day leave
    click(By.cssSelector("[data-testid=\"leave-from\"]:not([disabled])"));
    click(By.cssSelector("[data-testid=\"leave-to\"]:not([disabled])"));
    check(By.id("half-day"));
    expectText(tid("leave-days"), "0.5");
    type(By.id("leave-reason"), "Doctor's appointment in the morning");
    click(By.xpath("//button[normalize-space()='Submit request']"));
    expectVisible(tid("leave-submitted"));
    expectText(tid("leave-status"), "Pending");
    click(By.xpath("//button[normalize-space()='Log out']"));

    // HR manager approves
    type(By.id("hr-username"), "hrmanager");
    type(By.name("hr-password"), "Hr@123");
    click(By.xpath("//button[normalize-space()='Sign in']"));
    click(By.linkText("Approvals"));
    clickNth("approve-leave", 0);
    expectText(tid("approval-toast"), "approved");
    click(By.xpath("//button[normalize-space()='Log out']"));

    // The employee's balance has dropped
    type(By.id("hr-username"), "emp");
    type(By.name("hr-password"), "Hr@123");
    click(By.xpath("//button[normalize-space()='Sign in']"));
    expectText(tid("balance-casual"), "7.5");
  }
}

Practicing another tool?