Java · TestNG
Travel & Airline with Selenium
One-way or round trip with airport autocomplete and swap, passenger steppers with infant rules, filters and sort tabs, fare families, traveller forms, a seat map, payment and PNR.
App under test
✈️ SkyWays
- URL
- /apps/flights
- Login
- not required
- Max travellers
- 9 (1 infant/adult)
- Card
- any 16 digits
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Airport autocomplete
BeginnerType in from-city, pick from-option-<code>; free text without picking fails search-errors.
- 2
Swap & same-city rule
Beginnerswap-cities exchanges both fields; the same city on both sides is rejected.
- 3
Passenger rules
Intermediateinfants-inc is disabled once infants = adults; the total never exceeds 9.
- 4
Round trip dates
Intermediatereturn-day chips before the departure date are disabled.
- 5
Filters & sorting
IntermediateCombine stops-*, airline-*, dep-* and max-fare; sort-fastest orders by duration.
- 6
Fare families
Intermediateselect-flight expands fare-basic / fare-flex inside that card only.
- 7
Seat map
AdvancedPick seats with data-status="available"; selection auto-advances to the next traveller (assigned-seat-*).
- 8
Round-trip booking
AdvancedSelect outbound, then the return list appears; the ticket shows two ticket-leg rows.
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 ×7 · By.name ×7 · By.xpath ×6 · By.cssSelector ×1 · data-testid ×9
public class SkyWaysBookingTest {
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 booksAOnewayFlightForTwo() {
driver.get(BASE + "/apps/flights");
click(By.xpath("//button[normalize-space()='One way']"));
type(By.id("from-city"), "Mum");
click(tid("from-option-bom"));
type(By.id("to-city"), "Del");
click(tid("to-option-del"));
clickNth("depart-day", 1);
click(tid("passengers-toggle"));
click(tid("adults-inc"));
click(By.xpath("//button[normalize-space()='Done']"));
expectText(tid("passengers-summary"), "2 adults");
select(By.name("cabin-class"), "economy");
click(By.xpath("//button[normalize-space()='Search flights']"));
wait.until(urlContains("/apps/flights/results"));
check(By.id("stops-0"));
click(By.xpath("//button[normalize-space()='cheapest']"));
clickNth("select-flight", 0);
click(tid("fare-flex"));
wait.until(urlContains("/apps/flights/passengers"));
select(By.name("pax-0-title"), "mr");
type(By.name("pax-0-first"), "Amir");
type(By.id("pax-0-last"), "Khan");
select(By.name("pax-1-title"), "ms");
type(By.id("pax-1-first"), "Sara");
type(By.name("pax-1-last"), "Khan");
type(By.id("contact-email"), "amir@example.com");
type(By.name("contact-phone"), "9876543210");
click(By.xpath("//button[normalize-space()='Continue to seats']"));
// Each pick auto-advances to the next traveller
click(By.cssSelector("[data-testid^=\"seat-\"][data-status=\"available\"]"));
click(By.cssSelector("[data-testid^=\"seat-\"][data-status=\"available\"]"));
click(By.xpath("//button[normalize-space()='Continue to payment']"));
type(By.id("card-number"), "4242424242424242");
type(By.cssSelector("[placeholder='MM/YY']"), "1230");
type(By.name("card-cvv"), "123");
click(tid("pay-flight"));
expectVisible(tid("ticket-confirmed"));
expectText(tid("ticket-passengers"), "Sara Khan");
}
}