Road to automate Html5 video with Webdriver (Selenium)

In this post I will show you how to automate HTML5 video.  To automate this we will use java scripts function to control video. So here we use “document.getElementById("Video ID")” , it return the HTML 5 video object. Using this object we can simulate video. Following are some java script function to handle video.
1. Play video
            document.getElementById("Video ID").play();  

2. Pause video
            document.getElementById("Video ID").pause() ;         
3. To check video is paused or not use below code
            document.getElementById("Video ID").Paused;           
4. To increase volume
            document.getElementById("Video ID").volume=0.5;    
5. To reload video
            document.getElementById("Video ID").load();            
6. To replay from stating or forward and back playing video. You can set current playing time.
            document.getElementById("Video ID").currentTime=0 
            document.getElementById("Video ID").currentTime=10
            document.getElementById("Video ID").currentTime=20
7. To check video is muted.
            document.getElementById("Video ID").muted

Example: here is example of Webdriver to automate html 5 video, I am using JavascriptExecutor class to execute java script code.
public class HtmlVideo {
 
public WebDriver driver;
 
@Test
public void testVideo() throws InterruptedException
{  
 driver = new FirefoxDriver();
 driver.manage().window().maximize();
 driver.get("http://www.w3.org/2010/05/video/mediaevents.html");
 JavascriptExecutor js = (JavascriptExecutor) driver;
  
 //play video
 js .executeScript("document.getElementById(\"video\").play()");
 Thread.sleep(5000);
  
 //pause playing video 
 js .executeScript("document.getElementById(\"video\").pause()");
  
 //check video is paused
 System.out.println(js .executeScript("document.getElementById(\"video\").paused"));
  
 js .executeScript("document.getElementById(\"video\").play()");
  
 // play video from starting
 js .executeScript("document.getElementById(\"video\").currentTime=0");
 Thread.sleep(5000);
  
 //reload video
 js .executeScript("document.getElementById(\"video\").load()");
}
}

7 comments:

  1. //check video is paused
    System.out.println(js .executeScript("document.getElementById(\"video\").paused"));

    Above line is throwing Null Pointer exception

    ReplyDelete
  2. @Sasi, You've given wrong id ie: video. May the video you're testing doesn't have that id. check again

    ReplyDelete
  3. System.out.println(js .executeScript("document.getElementById(\"video\").paused"));

    returns null value. it does not return the status of the video

    Other functions Play/pause/load/currentTime works fine

    CAn you please help

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Replies
    1. Try to find the tag, there must be id of the player.

      Delete
  6. U can use xpath to find WebElement first and then use
    executor.executeScript("arguments[0].play();", el);

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution