Java · TestNG
Event ticketing with Selenium
Filter events, pick quantities across ticket tiers (sold out, only-N-left, max 5 per order), fill details per attendee with unique emails, apply a promo, pay and get QR tickets — or join a waitlist.
App under test
🎟️ EventHive
- URL
- /apps/events
- Promo
- EARLY10 (10%)
- Booking fee
- 5%
- Max per order
- 5 tickets
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Filters & switch
Beginnercategory-*, event-city, event-range and free-only combine; assert event-count.
- 2
Stock & order cap
Intermediatetier-*-inc disables at the tier's stock (VIP has 3) and when the order reaches 5 tickets.
- 3
Sold-out states
Beginnertier-sold-out-early shows on TestCon; Design Systems Meetup is fully sold out and shows the waitlist.
- 4
Dynamic attendee forms
IntermediateOne attendee-N block per ticket; first-is-buyer copies your details into ticket 1.
- 5
Unique emails
IntermediateTwo attendees with the same email show error-unique-emails.
- 6
Totals maths
AdvancedCheck ticket-subtotal, event-discount (10%), event-fees (5% after discount) and event-total.
- 7
Free registration
BeginnerThe free workshop skips card fields; the button says Register.
- 8
Waitlist position
Intermediatejoin-waitlist validates the email and shows waitlist-joined with your position.
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 ×5 · By.name ×5 · By.xpath ×4 · By.cssSelector ×2 · data-testid ×10
public class EventHiveTicketsTest {
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 buysTicketsForThreeAttendees() {
driver.get(BASE + "/apps/events");
click(By.xpath("//button[normalize-space()='tech']"));
expectText(tid("event-count"), "2 events");
click(tid("open-event-e1"));
expectVisible(tid("tier-sold-out-early"));
click(tid("tier-regular-inc"));
click(tid("tier-regular-inc"));
click(tid("tier-vip-inc"));
expectText(tid("ticket-count"), "3");
expectText(tid("ticket-subtotal"), "₹10,997");
click(By.xpath("//button[normalize-space()='Continue']"));
type(By.id("buyer-name"), "Amir Khan");
type(By.name("buyer-email"), "amir@example.com");
type(By.id("buyer-phone"), "9876543210");
check(By.id("first-is-buyer"));
type(By.name("attendee-1-name"), "Sara Khan");
type(By.name("attendee-1-email"), "sara@example.com");
type(By.id("attendee-2-name"), "Dev Patel");
type(By.name("attendee-2-email"), "dev@example.com");
type(By.cssSelector("[placeholder='Promo code']"), "EARLY10");
click(By.xpath("//button[normalize-space()='Apply']"));
expectText(tid("event-total"), "₹10,392");
type(By.id("card-number"), "4242424242424242");
type(By.cssSelector("[placeholder='MM/YY']"), "1230");
type(By.name("card-cvv"), "123");
click(By.xpath("//button[normalize-space()='Pay & get tickets']"));
expectVisible(tid("tickets-issued"));
expectNthText("ticket-holder", 2, "Dev Patel");
}
}