Java · TestNG
Real estate with Selenium
Buy/Rent search with BHK chips, budget ranges and type filters, grid/list views, a shortlist with side-by-side compare, a photo gallery, an EMI calculator and site-visit booking.
App under test
🏠 HomeNest
- URL
- /apps/realestate
- Login
- not required
- Cities
- Pune · Mumbai · Bengaluru
- EMI
- standard reducing-balance formula
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Buy vs Rent
Beginnermode-rent swaps the budget options (₹/month) and the listings; price labels end in /mo.
- 2
Combined filters
Intermediatebhk-* chips + budget-min/max + type-* + furnishing; a min above max shows budget-error.
- 3
Sorting & views
Intermediateestate-sort by price; view-list/view-grid changes data-view on property-list.
- 4
Shortlist state
Beginnershortlist-<id> toggles aria-pressed and the shortlist-count badge; it persists across reloads.
- 5
Compare limit
IntermediateTick compare-* on 2–3 shortlisted homes for compare-table; a 4th shows compare-limit.
- 6
Gallery carousel
Beginnergallery-next/prev wrap around; thumb-* jumps; gallery-counter shows “n / 4”.
- 7
EMI maths
AdvancedChange emi-down, emi-rate, emi-tenure and verify emi-loan and emi-result against the formula.
- 8
Masked phone & visits
Intermediatecontact-agent masks the number until reveal-phone; book-visit validates, then list/cancel it under My visits.
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 ×2 · By.cssSelector ×2 · data-testid ×9
public class HomeNestVisitTest {
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 shortlistsAFlatAndBooksASiteVisit() {
driver.get(BASE + "/apps/realestate");
click(By.xpath("//button[normalize-space()='buy']"));
select(By.id("estate-city"), "pune");
click(tid("bhk-2"));
select(By.name("budget-max"), "10000000");
check(By.id("type-apartment"));
select(By.cssSelector("[aria-label='Sort']"), "price-asc");
expectText(tid("property-count"), "2 properties");
click(tid("shortlist-p2"));
expectText(tid("shortlist-count"), "1");
click(tid("view-property-p2"));
expectText(tid("property-title"), "2 BHK Apartment in Baner");
click(By.cssSelector("[aria-label='Next photo']"));
expectText(tid("gallery-counter"), "2 / 4");
// EMI for ₹85 L with 20% down, 8.5% for 20 years
select(By.name("emi-down"), "20");
select(By.id("emi-rate"), "8.5");
select(By.name("emi-tenure"), "20");
expectText(tid("emi-result"), "59,012");
clickNth("visit-day", 0);
select(By.id("visit-time"), "11:00");
type(By.name("visitor-name"), "Amir Khan");
type(By.id("visitor-phone"), "9876543210");
type(By.name("visitor-email"), "amir@example.com");
click(By.xpath("//button[normalize-space()='Book visit']"));
expectText(tid("visit-confirmed"), "VIS-");
}
}