PracticeQAAutomation logoPracticeQAAutomation
Se

Java · TestNG

Agriculture with Selenium

Three tools in tabs: a sortable mandi price table with price alerts, a fertiliser calculator that turns soil-test levels into urea/DAP/MOP bag counts, and equipment rental with dates and day steppers.

App under test

🌾 FarmMart

URL
/apps/agri
Login
not required
Bags
50 kg each
Prices
₹ per quintal (fictional)

Scenarios to automate

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

  1. 1

    Tab switching

    Beginner

    agri-tab-* are role=tab with aria-selected; only the active panel is rendered.

  2. 2

    Sort a column

    Beginner

    sort-modal-price toggles descending/ascending; aria-sort on the header changes too.

  3. 3

    Dependent data

    Intermediate

    Changing price-state or price-crop reloads price-row rows and alert-market options.

  4. 4

    Alert logic

    Intermediate

    Setting an alert below the current price says “already above”; above it says “we'll notify you”.

  5. 5

    Calculator maths

    Advanced

    Wheat, 5 acres, soil N low / P medium / K high → urea 12, DAP 6, MOP 2 bags.

  6. 6

    Calculator validation

    Beginner

    Missing crop/soil or area outside 0–100 shows fert-error.

  7. 7

    Rental totals

    Intermediate

    rental-total = per-day rate × rental-days; book-rental stays disabled until equipment and date are chosen.

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
FarmMartToolsTest.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 ×4 · By.xpath ×3 · By.cssSelector ×2 · data-testid ×11
public class FarmMartToolsTest {
  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 checksPricesDosesFertiliserAndRentsATractor() {
    driver.get(BASE + "/apps/agri");
    select(By.id("price-state"), "Punjab");
    select(By.name("price-crop"), "wheat");
    expectNthText("modal-price", 0, "2,490");
    click(tid("sort-modal-price"));
    expectNthText("modal-price", 0, "2,175");
    select(By.id("alert-market"), "Ludhiana");
    type(By.name("alert-price"), "2400");
    click(By.xpath("//button[normalize-space()='Set alert']"));
    expectText(tid("alert-message"), "already above");
    click(tid("agri-tab-fertiliser"));
    select(By.id("fert-crop"), "wheat");
    type(By.name("fert-acres"), "5");
    select(By.id("soil-n"), "low");
    select(By.name("soil-p"), "medium");
    select(tid("soil-k"), "high");
    click(By.xpath("//button[normalize-space()='Calculate']"));
    expectText(tid("fert-urea-bags"), "12");
    expectText(tid("fert-dap-bags"), "6");
    expectText(tid("fert-mop-bags"), "2");
    click(tid("agri-tab-rental"));
    click(tid("equipment-tractor"));
    clickNth("rental-start", 0);
    click(By.cssSelector("[aria-label='Increase Days']"));
    click(By.cssSelector("[aria-label='Increase Days']"));
    expectText(tid("rental-total"), "₹3,600");
    click(By.xpath("//button[normalize-space()='Book equipment']"));
    expectText(tid("rental-booked"), "RNT-");
  }
}

Practicing another tool?