DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How would you automate testing of an HTML5 video player's functionality using Selenium WebDriver in Java, ensuring robus

πŸ”₯ 90% of Selenium Engineers Struggle to Verify Video Playback:
You need to auto‑play a 30‑second demo, seek to 20β€―s, pause, then assert the correct frameβ€”all using Selenium WebDriver in Java. How would you handle the JavaScript‑driven video controls reliably?

πŸ“Œ Problem Statement
β€’ Video players expose state via DOM properties (paused, currentTime, duration) not standard click‑able elements.

❌ Relying only on click() on play/pause buttons often fails when controls are hidden or animated.

βœ… The solution uses JavascriptExecutor to invoke HTMLMediaElement methods and read properties directly.

πŸ’‘ Solution & Code Walkthrough

// Locate the <video> tag
WebElement video = driver.findElement(By.tagName("video"));
// Ensure video is loaded before interaction
new WebDriverWait(driver, Duration.ofSeconds(5))
    .until(wd -> ((JavascriptExecutor)wd)
        .executeScript("return arguments[0].readyState", video) == 4);
// Play the video via JavaScript
((JavascriptExecutor)driver).executeScript("arguments[0].play()", video);
// Wait until the video reaches 20 seconds
new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(driver1 -> {
        Long time = (Long)((JavascriptExecutor)driver1)
            .executeScript("return arguments[0].currentTime", video);
        return time != null && time >= 20;
    });
// Pause using JavaScript
((JavascriptExecutor)driver).executeScript("arguments[0].pause()", video);
// Verify the paused state
boolean isPaused = (Boolean)((JavascriptExecutor)driver)
    .executeScript("return arguments[0].paused", video);
assertTrue("Video should be paused", isPaused);
driver.quit();
Enter fullscreen mode Exit fullscreen mode

Key points: use explicit waits that poll JavaScript properties, avoid generic Thread.sleep, and keep locators simple.

πŸ”‘ Key Takeaways
β€’ Treat the <video> element as a native HTMLMediaElement and control it via JS.

β€’ Prefer explicit waits that poll currentTime or paused over generic Thread.sleep.

β€’ Keep locators simple (By.tagName("video")) and handle readyState before interaction.

❓ Quick Summary Q&A
β€’ Why not use Selenium’s built‑in video controls? They are often UI‑only and may not reflect true media state.

β€’ How do you wait for a specific playback time? Poll currentTime via JavascriptExecutor in an ExpectedCondition.

β€’ What if the video never loads? Check readyState and fallback gracefully.

TAGS: selenium, java, automation, video-testing

────────────────────────────────────────
App Store: ────────────────────────────────────────

πŸ“² 𝐅𝐑𝐄𝐄 πŒπŽππˆπ‹π„ 𝐀𝐏𝐏 β€” πŸ”πŸŽπŸŽ+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

πŸ€– 𝐆𝐨𝐨𝐠π₯𝐞 𝐏π₯𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐒𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261003

🍎 𝐀𝐩𝐩 π’π­π¨π«πž (π’πŽπ’):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261003&mt=8

────────────────────────────────────────
Play Store: ────────────────────────────────────────

πŸ“² 𝐅𝐑𝐄𝐄 πŒπŽππˆπ‹π„ 𝐀𝐏𝐏 β€” πŸ”πŸŽπŸŽ+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

πŸ€– 𝐆𝐨𝐨𝐠π₯𝐞 𝐏π₯𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐒𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261003

🍎 𝐀𝐩𝐩 π’π­π¨π«πž (π’πŽπ’):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261003&mt=8

────────────────────────────────────────
────────────────────────────────────────

Top comments (0)