PracticeQAAutomation logoPracticeQAAutomation
Se

Java · TestNG

Automotive with Selenium

Configure a car — trim, colour swatches that recolour an SVG preview, wheels, interior and option packs — with live ex-showroom and on-road pricing, a trim comparison, then book a test drive.

App under test

🚘 AutoDrive

URL
/apps/auto
On-road
ex-showroom + 12%
Dealers
closed Sundays
Prices
fictional

Scenarios to automate

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

  1. 1

    Swatch state

    Beginner

    color-* buttons are role=radio with aria-checked; car-preview's data-color matches the chosen hex.

  2. 2

    SVG attributes

    Intermediate

    Changing wheels-* updates data-wheels on car-preview (and the wheel radius).

  3. 3

    Price maths

    Intermediate

    price-options sums colour, wheels, interior and packs; price-on-road = ex-showroom × 1.12.

  4. 4

    Compare modal

    Beginner

    compare-trims opens trim-compare-table with one column per trim.

  5. 5

    Save & carry over

    Intermediate

    save-config shows a CFG- code; the test-drive page shows the same code and price.

  6. 6

    Test-drive validation

    Intermediate

    Missing dealer, day, time, bad phone or unticked has-licence each show error-*.

  7. 7

    Disabled Sundays

    Beginner

    Pick the first enabled drive-day chip with :not([disabled]).

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
AutoDriveConfigTest.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 ×4 · By.name ×3 · By.xpath ×4 · By.cssSelector ×1 · data-testid ×11
public class AutoDriveConfigTest {
  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 configuresACarAndBooksATestDrive() {
    driver.get(BASE + "/apps/auto");
    click(tid("configure-aero"));
    click(tid("trim-vx"));
    click(tid("color-red"));
    expectText(tid("selected-color-name"), "Racing Red");
    click(tid("wheels-18"));
    click(tid("interior-leather"));
    check(By.id("package-tech"));
    expectText(tid("price-ex-showroom"), "₹9,27,000");
    expectText(tid("price-on-road"), "₹10,38,240");
    click(By.xpath("//button[normalize-space()='Compare trims']"));
    expectVisible(tid("trim-compare-table"));
    click(By.cssSelector("[aria-label='Close']"));
    click(By.xpath("//button[normalize-space()='Save configuration']"));
    expectText(tid("config-saved"), "CFG-");
    click(By.xpath("//button[normalize-space()='Book a test drive']"));
    wait.until(urlContains("/apps/auto/test-drive"));
    select(By.name("dealer"), "pune-baner");
    click(By.cssSelector("[data-testid=\"drive-day\"]:not([disabled])"));
    select(By.id("drive-time"), "11:30");
    type(By.name("driver-name"), "Amir Khan");
    type(By.id("driver-phone"), "9876543210");
    type(By.name("driver-email"), "amir@example.com");
    check(By.id("has-licence"));
    click(By.xpath("//button[normalize-space()='Confirm test drive']"));
    expectText(tid("test-drive-confirmed"), "TD-");
  }
}

Practicing another tool?