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);
It is needed to play the video using JavaScript.
- jsExecutor.executeScript("arguments[0].play()", Player);
Now we need to declare the wait for the video to play in order not to skip this step and then the test may fail. Due to some network problems loading the video may lag for a few seconds before it really starts, meanwhile your test script may continue to the next step. Here this piece of code tells Selenium to wait until the video plays, as it may not start immediately.
- WebDriverWait wait2play = new WebDriverWait(driver, 10);
- wait2play.until(
- webdriver -> {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- return (boolean) js.executeScript("return arguments[0].paused", Player)==false;
- });
Now, the video is supposed to play, we need to wait some time.
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);
- });
You may want to add some basic verifications of the video status on the console,
- System.out.println(
- jsExecutor.executeScript("return arguments[0].paused"
- , Player));
and that's it!
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.