Wait Until HTML5 Video Finishes

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.
  1. WebElement Player = driver.findElement(By.id("vplayer"));
  2. JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
  3. jsExecutor.executeScript("arguments[0].play()", Player); 
It is needed to play the video using JavaScript.
  1. 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.
  1. WebDriverWait wait2play = new WebDriverWait(driver, 10);  
  2.         wait2play.until(  
  3.                 webdriver -> {  
  4.                     JavascriptExecutor js = (JavascriptExecutor) driver;  
  5.                     return (boolean) js.executeScript("return arguments[0].paused", Player)==false;  
  6.                 });  
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.
  1. WebDriverWait wait2stop = new WebDriverWait(driver, 40);  
  2.         wait2stop.until(  
  3.                 webdriver -> {  
  4.                     JavascriptExecutor js = (JavascriptExecutor) driver;  
  5.                     return js.executeScript("return arguments[0].paused", Player);  
  6.                 });  
You may want to add some basic verifications of the video status on the console,
  1. System.out.println(  
  2.                 jsExecutor.executeScript("return arguments[0].paused"  
  3.                         , Player));  
and that's it! 
 
The complete script for this test,
  1. import java.io.File;  
  2.   
  3. import org.apache.commons.io.FileUtils;  
  4. import org.openqa.selenium.By;  
  5. import org.openqa.selenium.JavascriptExecutor;  
  6. import org.openqa.selenium.OutputType;  
  7. import org.openqa.selenium.TakesScreenshot;  
  8. import org.openqa.selenium.WebDriver;  
  9. import org.openqa.selenium.WebElement;  
  10. import org.openqa.selenium.firefox.FirefoxDriver;  
  11. import org.openqa.selenium.support.ui.WebDriverWait;  
  12. import org.testng.Assert;  
  13. import org.testng.annotations.AfterClass;  
  14. import org.testng.annotations.BeforeClass;  
  15. import org.testng.annotations.Test;  
  16.   
  17. public class HTML5Player  
  18. {  
  19.     private WebDriver driver;  
  20.   
  21.     @BeforeClass  
  22.     public void setUp() {  
  23.         System.setProperty("webdriver.gecko.driver",   
  24.                 "c:/drivers/geckodriver.exe");  
  25.                   
  26.         driver = new FirefoxDriver();         
  27.         driver.get("http://cookbook.seleniumacademy.com/html5video.html");  
  28.     }  
  29.   
  30.     @Test  
  31.     public void testHTML5VideoPlayer() throws Exception {  
  32.       
  33.         WebElement Player = driver.findElement(By.id("vplayer"));  
  34.   
  35.               JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;  
  36.            
  37.         String source = (String) jsExecutor.executeScript(  
  38.                 "return arguments[0].currentSrc;", Player);  
  39.                   
  40.         
  41.         long duration = (Long) jsExecutor.executeScript(  
  42.                 "return arguments[0].duration", Player);  
  43.   
  44.         Assert.assertEquals(source,"http://html5demos.com/assets/dizzy.mp4");  
  45.         Assert.assertEquals(duration, 25);  
  46.   

  47.         jsExecutor.executeScript("arguments[0].play()", Player);  
  48.           
  49.         System.out.println(  
  50.                 jsExecutor.executeScript("return arguments[0].paused"  
  51.                         , Player));  
  52.           
  53.         WebDriverWait wait2play = new WebDriverWait(driver, 10);  
  54.         wait2play.until(  
  55.                 webdriver -> {  
  56.                     JavascriptExecutor js = (JavascriptExecutor) driver;  
  57.                     return (boolean) js.executeScript("return arguments[0].paused", Player)==false;  
  58.                 });  
  59.       
  60.         WebDriverWait wait2stop = new WebDriverWait(driver, 40);  
  61.         wait2stop.until(  
  62.                 webdriver -> {  
  63.                     JavascriptExecutor js = (JavascriptExecutor) driver;  
  64.                     return js.executeScript("return arguments[0].paused", Player);  
  65.                 });  
  66.           
  67.                  
  68.         System.out.println(  
  69.                 jsExecutor.executeScript("return arguments[0].paused"  
  70.                         , Player));  
  71.    
  72.         File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);  
  73.         FileUtils.copyFile(scrFile, new File("Screenshots/pause_play.png"));  
  74.     }  
  75.   
  76.     @AfterClass  
  77.     public void tearDown() {  
  78.         driver.quit();  
  79.     }  
  80. }   
Thank you and best of luck everybody.