Java · TestNG
Legal with Selenium
Filter lawyers by practice area, city and language and book a consultation; then generate a rental agreement through a wizard with a live document preview, cross-field rules, typed e-signatures and a stamp-duty calculation.
App under test
⚖️ LexDesk
- URL
- /apps/legal
- Deposit cap
- 6 × monthly rent
- Stamp duty
- 0.25% (min ₹100)
- Consult days
- closed Sundays
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Directory filters
Beginnerarea-*, lawyer-city, lawyer-language and lawyer-sort change lawyer-count and order.
- 2
Booking modal validation
Intermediateconfirm-consult without mode/day/time or with a short description shows consult-error.
- 3
Live preview
IntermediateTyping in the wizard updates preview-* fields immediately; blanks show underscores.
- 4
Cross-field rules
AdvancedDeposit above 6 months' rent and landlord = tenant are rejected with specific error-* messages.
- 5
Exact-match signatures
Intermediatelandlord-signature / tenant-signature must match the names exactly (case and spaces).
- 6
Stamp duty maths
Intermediate₹25,000 × 11 months + ₹1,50,000 deposit → stamp-duty ₹1,063.
- 7
Wizard navigation
Beginnerdoc-back keeps entered values; the stepper marks completed steps.
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 ×6 · By.linkText ×1 · By.xpath ×3 · data-testid ×13
public class LexDeskAgreementTest {
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 booksALawyerAndSignsARentalAgreement() {
driver.get(BASE + "/apps/legal");
click(By.xpath("//button[normalize-space()='Property']"));
expectText(tid("lawyer-count"), "2 lawyers");
select(By.id("lawyer-sort"), "fee");
click(tid("book-l4"));
click(By.xpath("//button[normalize-space()='Video']"));
click(By.cssSelector("[data-testid=\"consult-day\"]:not([disabled])"));
select(By.name("consult-time"), "12:00");
type(By.id("consult-issue"), "Need a review of my rental agreement before signing.");
click(tid("confirm-consult"));
expectText(tid("consult-booked"), "LX-");
// Generate a rental agreement
click(By.linkText("Create agreement"));
type(By.name("landlord-name"), "Meera Kulkarni");
type(By.id("tenant-name"), "Amir Khan");
expectText(tid("preview-tenant"), "Amir Khan");
click(tid("doc-next"));
type(By.name("property-address"), "Flat 12B, Palm Residency, Baner");
select(By.id("property-city"), "Pune");
click(tid("doc-next"));
type(By.name("monthly-rent"), "25000");
type(By.id("security-deposit"), "150000");
select(By.name("term-months"), "11");
clickNth("agreement-start", 0);
expectText(tid("preview-rent"), "₹25,000");
expectText(tid("duty-estimate"), "₹1,063");
click(tid("doc-next"));
type(By.id("landlord-signature"), "Meera Kulkarni");
type(By.name("tenant-signature"), "Amir Khan");
check(By.id("esign-consent"));
click(By.xpath("//button[normalize-space()='Sign agreement']"));
expectText(tid("agreement-id"), "AGR-");
expectText(tid("stamp-duty"), "₹1,063");
expectText(tid("preview-signed"), "Signed electronically");
}
}