Java · TestNG
Pharmacy with Selenium
Search medicines by name or salt, see cheaper generic substitutes, respect per-item quantity limits, and get blocked at checkout until you upload a prescription (or book a consult) for Rx items.
App under test
💊 MediKart
- URL
- /apps/pharmacy
- Mobile
- 9876543210
- OTP
- 445566
- Rx upload
- PDF/JPG/PNG ≤ 5 MB
- Free delivery
- ₹300+
- Consult
- ₹199
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Mobile + OTP login
BeginnerPasswordless sign-in: mk-login-id (mobile) → mk-login → mk-code (OTP) → mk-verify. A wrong OTP shows mk-code-error; an unknown mobile counts toward the 3-attempt lock.
- 2
Min-length autocomplete
Beginnermed-suggestions only appears after 2 characters; match by brand or salt.
- 3
Substitutes & savings
IntermediateOn Mox 500, saving-m3g shows “Save ₹50 (42%)”; add-substitute-* adds the generic.
- 4
Quantity limits
Intermediateinc-<id> disables at the medicine's max (antibiotics: 2–3); add-med-* disables when maxed.
- 5
Rx gate
AdvancedWith an Rx item, placing the order shows error-rx until rx-upload + prescription-file or rx-consult.
- 6
Upload validation
IntermediateA .txt or >5 MB file shows an error under prescription-file; valid files show prescription-file-name.
- 7
Totals
Intermediatemed-delivery is ₹40 under ₹300 and FREE above; rx-consult adds ₹199 to med-total.
- 8
Filters
Beginnermed-category-* chips and filter-all/otc/rx narrow the med-grid.
- 9
Reorder
Beginnerreorder-<id> on the orders page refills the cart with the same lines.
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 ×3 · By.name ×3 · By.linkText ×1 · By.xpath ×4 · By.cssSelector ×2 · data-testid ×16
public class MediKartRxTest {
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));
}
// File inputs take an absolute path via sendKeys (fixtures live in src/test/resources).
void upload(By by, String file) {
driver.findElement(by).sendKeys(new java.io.File("src/test/resources/" + file).getAbsolutePath());
}
@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 ordersMedicinesWithAPrescription() {
driver.get(BASE + "/apps/pharmacy");
// Sign in first
type(By.id("mk-login-id"), "9876543210");
click(By.xpath("//button[normalize-space()='Continue']"));
type(By.name("mk-code"), "445566");
click(tid("mk-verify"));
type(By.cssSelector("[aria-label='Search medicines']"), "amox");
click(tid("med-option-m3"));
expectText(tid("med-title"), "Mox 500");
expectText(tid("saving-m3g"), "Save ₹50");
click(By.xpath("//button[normalize-space()='Add instead']"));
expectText(tid("cart-count"), "1");
click(By.linkText("Shop"));
click(tid("add-med-m1"));
click(tid("add-med-m1"));
click(tid("nav-cart"));
expectVisible(tid("rx-required"));
click(By.xpath("//button[normalize-space()='Place order']"));
expectVisible(tid("error-rx"));
click(tid("rx-upload"));
upload(By.id("prescription-file"), "prescription.pdf");
expectText(tid("prescription-file-name"), "prescription.pdf");
select(By.name("delivery-slot"), "Tomorrow 8–11 am");
type(By.id("med-phone"), "9876543210");
type(By.name("med-address"), "12B Palm Residency, Baner, Pune");
expectText(tid("med-total"), "₹178");
click(By.cssSelector("[aria-label='Refill reminder']"));
click(By.xpath("//button[normalize-space()='Place order']"));
expectVisible(tid("med-order-placed"));
expectText(tid("med-order-id"), "MK-");
expectVisible(tid("refill-on"));
}
}