PracticeQAAutomation logoPracticeQAAutomation
Se

Java · TestNG

Hospitality with Selenium

Search a destination, filter by stars, price and amenities, pick a room with add-ons, enter guest details, pay and manage the reservation.

App under test

🏨 StayEase

URL
/apps/hotel
Login
not required
Taxes
12%
Card
any 16 digits

Scenarios to automate

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

  1. 1

    Search validation

    Beginner

    Search with no destination or date shows error-city and error-checkin.

  2. 2

    Steppers & date chips

    Beginner

    nights-inc / guests-inc change the summary; pick checkin-day by index.

  3. 3

    Filters & sorting

    Intermediate

    Combine star-*, max-price (range), amenity-* and sort; assert result-count and price order.

  4. 4

    Room capacity

    Intermediate

    With 3+ guests the Standard room is disabled and shows room-standard-capacity.

  5. 5

    Live price summary

    Intermediate

    addon-* checkboxes update price-addons, price-taxes and price-total.

  6. 6

    Checkout validation

    Intermediate

    Submit empty to get form-errors and error-* per field; Pay at hotel hides card fields.

  7. 7

    Async confirmation

    Advanced

    confirm-booking shows a processing overlay (~1.5s) before booking-confirmed.

  8. 8

    Cancel with reason

    Advanced

    cancel-<id> opens cancel-modal; a reason is required before confirm-cancel.

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
StayEaseBookingTest.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 ×5 · By.xpath ×3 · By.cssSelector ×2 · data-testid ×10
public class StayEaseBookingTest {
  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 booksAHotelEndToEnd() {
    driver.get(BASE + "/apps/hotel");
    select(By.id("city"), "goa");
    clickNth("checkin-day", 2);
    click(By.cssSelector("[aria-label='Increase Nights']"));
    click(By.xpath("//button[normalize-space()='Search hotels']"));
    wait.until(urlContains("/apps/hotel/results"));

    // Filter to 5-star stays, cheapest first
    check(tid("star-5"));
    select(By.name("sort"), "price-asc");
    expectText(tid("result-count"), "2 properties");
    click(tid("view-rooms-g1"));
    wait.until(urlContains("/apps/hotel/hotel/g1"));
    expectText(tid("hotel-title"), "Palm Grove Resort");
    click(tid("select-room-deluxe"));
    check(By.id("addon-breakfast"));
    expectVisible(tid("price-total"));
    click(By.xpath("//button[normalize-space()='Continue to booking']"));

    // Guest details and card payment
    type(By.name("guest-first-name"), "Ada");
    type(By.id("guest-last-name"), "Lovelace");
    type(By.name("guest-email"), "ada@example.com");
    type(By.id("guest-phone"), "5551234567");
    select(By.name("arrival-time"), "14:00 – 16:00");
    click(tid("pay-now"));
    type(By.id("card-number"), "4242424242424242");
    type(By.cssSelector("[placeholder='MM/YY']"), "1230");
    type(By.name("card-cvv"), "123");
    check(By.id("accept-policy"));
    click(By.xpath("//button[normalize-space()='Confirm booking']"));
    expectVisible(tid("booking-confirmed"));
    expectText(tid("reservation-id"), "SE-");
    expectText(tid("confirmed-room"), "Deluxe Room");
  }
}

Practicing another tool?