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
2. Pause video
3. To check video is paused or not use below code
4. To increase volume
5. To reload video
6. To replay from stating or forward and back playing video. You can set current playing time.
7. To check video is muted.
Example: here is example of Webdriver to automate html 5 video, I am using JavascriptExecutor class to execute java script code.
1. Play video
document.getElementById("Video
ID").play();
2. Pause video
document.getElementById("Video
ID").pause() ;
document.getElementById("Video
ID").Paused;
document.getElementById("Video
ID").volume=0.5;
document.getElementById("Video
ID").load();
document.getElementById("Video
ID").currentTime=0
document.getElementById("Video
ID").currentTime=10
document.getElementById("Video
ID").currentTime=20
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()"); } }
//check video is paused
ReplyDeleteSystem.out.println(js .executeScript("document.getElementById(\"video\").paused"));
Above line is throwing Null Pointer exception
Try:
DeleteSystem.out.println(js.executeScript("return document.getElementById(\"video1\").paused"));
@Sasi, You've given wrong id ie: video. May the video you're testing doesn't have that id. check again
ReplyDeleteSystem.out.println(js .executeScript("document.getElementById(\"video\").paused"));
ReplyDeletereturns null value. it does not return the status of the video
Other functions Play/pause/load/currentTime works fine
CAn you please help
This comment has been removed by the author.
ReplyDeleteFrom where to get the video id*
ReplyDeleteTry to find the tag, there must be id of the player.
DeleteU can use xpath to find WebElement first and then use
ReplyDeleteexecutor.executeScript("arguments[0].play();", el);
For me I'm getting play is not a function error
ReplyDeleteI'm getting play is not a function error
ReplyDeleteAnd pause is not a function error
ReplyDeleteI'm getting play is not a function error
ReplyDeleteAnd pause not a function error