PracticeQAAutomation logoPracticeQAAutomation
Se

Java · TestNG

Ride hailing with Selenium

Pickup and drop autocomplete with swap, fares per vehicle computed from distance, a promo, then a live ride that moves from searching to completed — with OTP, a cancel fee rule, rating and tip.

App under test

🚕 RideGo

URL
/apps/ride
Promo
FIRSTRIDE (50%, max ₹75)
Stages
2s · 5s · 8s · 11s
Cancel fee
₹30 after arrival

Scenarios to automate

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

  1. 1

    Autocomplete

    Beginner

    Type in pickup-input / drop-input and choose *-option-<place>; the same place can't be both.

  2. 2

    Swap & shortcut

    Beginner

    use-home fills pickup; swap-locations exchanges both inputs and redraws ride-map.

  3. 3

    Fare maths

    Intermediate

    fare-<vehicle> = base + per-km × distance + per-min × time; check against trip-distance.

  4. 4

    Promo cap

    Intermediate

    FIRSTRIDE halves the fare but caps at ₹75 — fare-payable reflects the cap.

  5. 5

    Live status

    Advanced

    ride-status walks through all five stages on a timer — chain text waits, don't sleep.

  6. 6

    Cancel rules

    Advanced

    Cancel before arrival is free; after “Driver arrived” cancel-fee-warning shows ₹30 and cancel-fee records it.

  7. 7

    Moving marker

    Intermediate

    During the trip, map-car moves along the route (SVG text element).

  8. 8

    Rate & tip

    Beginner

    After completion pick rate-driver-* and tip-*; trip-total includes the tip.

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
RideGoTripTest.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 ×2 · By.name ×1 · By.xpath ×3 · By.cssSelector ×1 · data-testid ×14
public class RideGoTripTest {
  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 booksACabAndCompletesTheTrip() {
    driver.get(BASE + "/apps/ride");
    type(By.id("pickup-input"), "Tech");
    click(tid("pickup-option-office"));
    type(By.name("drop-input"), "air");
    click(tid("drop-option-airport"));
    expectText(tid("trip-distance"), "24.7 km");
    click(tid("ride-mini"));
    type(By.cssSelector("[placeholder='Promo code']"), "FIRSTRIDE");
    click(By.xpath("//button[normalize-space()='Apply']"));
    expectText(tid("fare-payable"), "₹432");
    select(By.id("ride-payment"), "upi");
    click(By.xpath("//button[normalize-space()='Book Mini']"));

    // The ride progresses on its own — wait for each status
    expectText(tid("ride-status"), "Driver assigned");
    expectVisible(tid("ride-otp"));
    expectText(tid("ride-status"), "Driver arrived");
    expectText(tid("ride-status"), "On trip");
    expectText(tid("ride-status"), "Completed");
    click(tid("rate-driver-5"));
    click(tid("tip-20"));
    click(By.xpath("//button[normalize-space()='Submit']"));
    expectVisible(tid("rating-saved"));
    expectText(tid("trip-total"), "₹452");
  }
}

Practicing another tool?