In this blog, I will show you how to handle HTML5 videos using JavaScript. First of all, I will be working on this link.
As a prerequisite for this blog, I would suggest you have the basic knowledge of Selenium TestNG and at least, a little insight of how the test methods are written.
Now, let's get down to work.
Here are the basic pieces of the code to identify the WebElement for the video player on the page and to initiate JavascriptExecutor object to handle the JavaScript.
- WebElement Player = driver.findElement(By.id("vplayer"));
- JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
- jsExecutor.executeScript("arguments[0].play()", Player);
- jsExecutor.executeScript("arguments[0].play()", Player);
- WebDriverWait wait2play = new WebDriverWait(driver, 10);
- wait2play.until(
- webdriver -> {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- return (boolean) js.executeScript("return arguments[0].paused", Player)==false;
- });
Using Explicit wait in Selenium, a piece of code is used to wait for the video to finish.
The video on this page mentioned here takes 25 seconds to fully play, here we need to wait for 40 seconds or even longer for the video status to be paused.
- WebDriverWait wait2stop = new WebDriverWait(driver, 40);
- wait2stop.until(
- webdriver -> {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- return js.executeScript("return arguments[0].paused", Player);
- });
- System.out.println(
- jsExecutor.executeScript("return arguments[0].paused"
- , Player));
The complete script for this test,
- import java.io.File;
- import org.apache.commons.io.FileUtils;
- import org.openqa.selenium.By;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.OutputType;
- import org.openqa.selenium.TakesScreenshot;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import org.testng.Assert;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.Test;
- public class HTML5Player
- {
- private WebDriver driver;
- @BeforeClass
- public void setUp() {
- System.setProperty("webdriver.gecko.driver",
- "c:/drivers/geckodriver.exe");
- driver = new FirefoxDriver();
- driver.get("http://cookbook.seleniumacademy.com/html5video.html");
- }
- @Test
- public void testHTML5VideoPlayer() throws Exception {
- WebElement Player = driver.findElement(By.id("vplayer"));
- JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
- String source = (String) jsExecutor.executeScript(
- "return arguments[0].currentSrc;", Player);
- long duration = (Long) jsExecutor.executeScript(
- "return arguments[0].duration", Player);
- Assert.assertEquals(source,"http://html5demos.com/assets/dizzy.mp4");
- Assert.assertEquals(duration, 25);
- jsExecutor.executeScript("arguments[0].play()", Player);
- System.out.println(
- jsExecutor.executeScript("return arguments[0].paused"
- , Player));
- WebDriverWait wait2play = new WebDriverWait(driver, 10);
- wait2play.until(
- webdriver -> {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- return (boolean) js.executeScript("return arguments[0].paused", Player)==false;
- });
- WebDriverWait wait2stop = new WebDriverWait(driver, 40);
- wait2stop.until(
- webdriver -> {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- return js.executeScript("return arguments[0].paused", Player);
- });
- System.out.println(
- jsExecutor.executeScript("return arguments[0].paused"
- , Player));
- File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
- FileUtils.copyFile(scrFile, new File("Screenshots/pause_play.png"));
- }
- @AfterClass
- public void tearDown() {
- driver.quit();
- }
- }
Thank you and best of luck everybody.

Sagar Pandurang KapPosted Jan 3, 2019, 12:08 AM
Thank you for lovely content
Abhishek MishraPosted Oct 4, 2018, 10:52 PM
This is helpful