Java · TestNG
Education with Selenium
Sign up with live password rules or log in, enroll in a course, watch lessons to unlock “Mark complete”, finish the course to unlock a timed quiz, and earn a certificate.
App under test
🎓 LearnHub
- URL
- /apps/lms
- Learner
- learner@learnhub.dev
- Password
- Learn@123
- Pass mark
- 60%
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Sign-up validation
Beginnerpassword-strength updates as you type; a weak password, mismatched confirm or reused email each fail.
- 2
Catalog filters
BeginnerCombine course-search, category-* chips and level-filter; assert course-count.
- 3
Wait for enabled
Intermediatemark-complete is disabled until play-video finishes (~1.5s) — wait for it to be enabled.
- 4
Progress tracking
IntermediateEach completed lesson updates course-progress and the lesson check marks; state survives reloads.
- 5
Locked content
Intermediatequiz-locked shows until 100%; visiting /quiz directly also shows the locked state.
- 6
Mixed question types
IntermediateAnswer radio (q1-*), checkboxes (q2-*), a select (q3) and free text (q4).
- 7
Unanswered confirm
AdvancedSubmitting with blanks opens unanswered-modal; submit-anyway grades as wrong.
- 8
Timer auto-submit
AdvancedMock the clock past 5:00 and assert the quiz auto-submits to quiz-result.
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 ×1 · By.name ×1 · By.xpath ×5 · By.cssSelector ×5 · data-testid ×11
public class LearnHubCertificateTest {
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 completesACourseAndEarnsACertificate() {
driver.get(BASE + "/apps/lms");
type(By.id("login-email"), "learner@learnhub.dev");
type(By.name("login-password"), "Learn@123");
click(tid("login-submit"));
wait.until(urlContains("/apps/lms/courses"));
type(By.cssSelector("[aria-label='Search courses']"), "Selenium");
expectText(tid("course-count"), "1 course");
click(By.xpath("//button[normalize-space()='Enroll for free']"));
wait.until(urlContains("/apps/lms/course/c1"));
// Each lesson: play the video, then mark it complete (auto-advances)
click(By.cssSelector("[aria-label='Play video']"));
click(By.xpath("//button[normalize-space()='Mark complete']"));
click(By.cssSelector("[aria-label='Play video']"));
click(By.xpath("//button[normalize-space()='Mark complete']"));
click(By.cssSelector("[aria-label='Play video']"));
click(By.xpath("//button[normalize-space()='Mark complete']"));
expectText(tid("course-progress"), "100%");
click(tid("start-quiz"));
click(tid("q1-b"));
check(tid("q2-a"));
check(tid("q2-c"));
select(tid("q3"), "frame");
type(By.cssSelector("[placeholder='Type your answer']"), "data-testid");
click(By.xpath("//button[normalize-space()='Submit answers']"));
expectText(tid("quiz-score"), "100%");
expectVisible(tid("certificate"));
expectText(tid("certificate-id"), "LH-");
}
}