π₯ 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();
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)