Testing a video is always a challenging task. Most of the video based web sites are manually tested & are out of scope of automation.
However, the javascript executor in selenium webdriver will enable us to play around with the video components. Thus, few of the test scenarios w.r.t video testing can be automated using the below approach.
Sample Video: Video Component Sample
Video Component: JW Player, JW HTML5 Video Player
Sample Script:
System.setProperty("webdriver.chrome.driver", "C:/folderpath/chromedriver.exe");
WebDriver driver= new ChromeDriver();
//Open the page containing video component.
driver.get("http://html5video.org/wiki/JW_HTML5_Video_Player");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//If video is placed in embed tag or iframe, navigate to the source.
//However, we can even switch to frame. [Else, skip this step]
WebElement elm = driver.findElement(By.id("video_iframe"));
String urlStr = elm.getAttribute("src");
System.out.println("Video Url : " + urlStr);
driver.navigate().to(urlStr);
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
JavascriptExecutor jse = (JavascriptExecutor) driver;
//Click on play button
jse.executeScript("jwplayer().play();");
Thread.sleep(2000);
//Pause
jse.executeScript("jwplayer().pause()");
Thread.sleep(2000);
//Play
jse.executeScript("jwplayer().play();");
Thread.sleep(2000);
// Set Volume
Thread.sleep(2000);
jse.executeScript("jwplayer().setVolume(50);");
Thread.sleep(2000);
jse.executeScript("jwplayer().setMute(true);");
Thread.sleep(2000);
//UnMute Player
jse.executeScript("jwplayer().setMute(false);");
Thread.sleep(2000);
//Stop the player
jse.executeScript("jwplayer().stop()");
Thread.sleep(2000);
driver.quit();
!!!!! Video Testing Using Selenium WebDriver is Possible. !!!!!
Download the Sample working code: TestVideo.java
Download the Sample working code: TestVideo.java
Hello,
ReplyDeleteThe Article on Test Video using Selenium WebDriver is nice. It give detail information about it .Thanks for Sharing the information on Selenium WebDriver . Software Testing Services
Hi, can i get to know how to find the components of jwplayer for automating purpose.
ReplyDeleteLike how to find the elements(pause/play/reload). Because we need to use Node.Js for automation.
Hi Abhishek,
DeleteAs you can see in the above example, I have used JavascriptExecutor class of selenium webdriver & passed the javascript string as parameter to the method.
You do not have to find objects specific to each component. You can just use the string "jwplayer().play();" / "jwplayer().pause();" etc... through Node.js to automate your scenario.
https://omega.cloud.netrovert.net/instructionalvideos
Deleteplease check and how to verify whether video is running or not
Hi Shiva Prakash, Thanks for sharing these inputs on controlling the videos using Selenium web driver via JavaScriptExecutor method. Can we use the same strategy for Youtube videos as well?
ReplyDeleteHi Mahantesh,
DeleteThe above example is for JW Player Video, If Youtube uses JWplayer then the above example should work for that too.
Also how do we automate some of the following basic video testing using Selenium? Have you tried anything of this sort?
ReplyDelete1. Check the Play/Pause button is correct (audio should not come on pressed pause button & audio play after pressed play button) - This can be done using your solution
2. Check the Video time is displayed or not - How to do this?
3. Navigation bar should work correct(by dragging left to right, right to left and noticed the time is varied according to them) - This can be done using your solution
4. Forward and Backward button should work correct(if it available) - How to do this?.
5. Both the Audio and Video should be Synchronize(matching video and audio) - How to do this?.
6. Audio should be played in both the left and right ear head set. - How to do this?
7. Blurring videos and the location(time) should be notice and report - How to do this?
8. Once the Video is End, the navigation bar should move to the initial position - This can be done using your solution.
9. How to apply checkpoints on a running or streaming video using Selenium web driver?
This comment has been removed by the author.
ReplyDeleteHow to define jwplayer() in selenium,got error
DeleteAs you see in the above example. YOu donot need to define jwplayer(). It is a string being passed to webdriver instance casted to javaScriptExecutor.
DeleteJavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("jwplayer().play();");
I also got the same error.
Deleteorg.openqa.selenium.WebDriverException: unknown error: jwplayer is not defined
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20 milliseconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'SANJANAPATEL', ip: '172.16.0.234', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1), userDataDir=C:\Users\SANJAN~1\AppData\Local\Temp\scoped_dir10376_17509}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.84, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 14417ac151226c3c9cb490e2a3ef67b1
Usually this error is thrown, when driver do not find the Jwplayer in the Page.
DeleteThere is a step to navigate to src of the video, which is necessary if it is in embed tag or in frame. Else, it should work as it is.
Sample Working java file is also attached for you reference. Use it accordingly. If you still have issues, let me know your video scenario & I can help you resolve it.
DeleteHi Shiva,
DeleteThanks for the response.
I am using below code to test the video on the webpage, but I am getting the same error "jwplayer is not defined".
driver.get("http://www.elastic.co/about");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement video = driver.findElement(By.id("player0"));
String url = video.getAttribute("src");
driver.navigate().to(url);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("jwplayer().play();");
Your help is appreciated. Thanks!!
@Sanjaja: The website url you have given has the video, but it is not of type jwplayer. The above given code works for video of type jwplayer.
DeleteFor your case, i do not have the answer right now.
@Shiva Prakash,
DeleteThank you for your reply.
How will I come to know that the video is of type jwplayer?
@Sanjana: Based on my observation,When it is JW Player.. most of the div under which video is located, contains className / id with text jw player like 'jw-video jw-reset','jw-controls-backdrop jw-reset'.
DeleteBut, the video in your page has names like 'vp-player-inner'
Thanks for your help @Shiva Prakash
DeleteHi Shiva
ReplyDeleteThanks for this useful code.
I Have an Online tutorial Portal where i have multiple Video links on on page, click on one link lends to new page & video starts Playing. How can i automate this process and check weather all links are lending to right page & all videos are playing fine.
1. As you said you have multiple links & you need to validate if it is going to right URL after clicking
Delete-- For every link get the url in 'src' or 'href' (without clicking on it) & validate with the expected URL.
2. To validate each jwplayer, get each link's src/href. Navigate to the src/href instead of clicking & use the above given code to test the video.
3. Loop/Repeat it for all the links in the page.
how do i play a you tube video in my web page after navigating to the src
ReplyDeleteFor Youtube videos, you donot have to navigate to source,
Deletedriver.get("https://youtu.be/juKRybHPMwE");
Thread.sleep(4000);
WebElement video = driver.findElement(By.tagName("video"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].pause()", video);
Thread.sleep(2000);
jse.executeScript("arguments[0].play();", video);
How to get Error message like "Error loading player:"
ReplyDeleteActually, It depends on the video player being used.
DeleteIf you can provide any sample, I can try let you know the details.
You can try with below code:
ReplyDeleteSystem.setProperty("webdriver.chrome.driver", "src/main/resources/executors/drivers/chromedriver.exe");
_driver = new ChromeDriver();
_driver.manage().window().maximize();
_driver.get("your url");
Actions action = new Actions(_driver);
WebElement elm = _driver.findElement(By.xpath("//button[@class='vjs-big-play-button']/span[@class='vjs-icon-placeholder' and 1]"));
action.click(elm).perform();
You can try with below code to open the video:
ReplyDeleteSystem.setProperty("webdriver.chrome.driver", "src/main/resources/executors/drivers/chromedriver.exe");
_driver = new ChromeDriver();
_driver.manage().window().maximize();
_driver.get("http://elearning.infogain.com:8383/moodle/");
Actions action = new Actions(_driver);
DeleteWebElement elm = _driver.findElement(By.xpath("//button[@class='vjs-big-play-button']/span[@class='vjs-icon-placeholder' and 1]"));
action.click(elm).perform();
You can try with below code to open the video:
ReplyDeleteSystem.setProperty("webdriver.chrome.driver", "src/main/resources/executors/drivers/chromedriver.exe");
_driver = new ChromeDriver();
_driver.manage().window().maximize();
_driver.get("http://elearning.infogain.com:8383/moodle/");
Actions action = new Actions(_driver);
WebElement elm = _driver.findElement(By.xpath("//button[@class='vjs-big-play-button']/span[@class='vjs-icon-placeholder' and 1]"));
action.click(elm).perform();
Great blog.Its really useful..thanks for sharing..Keep update Selenium Testing Course
ReplyDeletesoftware testing course in velachery chennai
Hi Shiva,
ReplyDeletethis is my code.
driver.get("http://staging.shemaroome.com/bollywood-retro-movies/amar-akbar-anthony-1");
Thread.sleep(5000);
WebElement elm = driver.findElement(By.id("iframe"));
String urlStr = elm.getAttribute("src");
System.out.println("Video Url : " + urlStr);
driver.navigate().to(urlStr);
Thread.sleep(2000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
//Click on play button
jse.executeScript("HTML5HLSPlayer().play();");
i am unable to play video.
Hi Shiva, may be this is stupid question, with your code I can perform actions on video, but, how will I assert whether things are working fine or not?
ReplyDeleteCertainly not a stupid one... :).
DeleteYou can perform assestions using the other methods of JwPlayer like the below.
jwplayer().getVolume() - gets the volumne percentage (0 t0 100)
jwplayer().getMute() - returns true when there's no sound.
jwplayer().getFullscreen() - returns true or false on fullscreen state
Hi this is my error message
ReplyDeleteStarting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 14843
Only local connections are allowed.
Mar 26, 2019 7:10:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: jwplayer is not defined
(Session info: chrome=70.0.3538.110)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.4.0-98-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'MWMGT189', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-98-generic', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.41.578700 (2f1ed5f9343c13..., userDataDir: /tmp/.org.chromium.Chromium...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:40050}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 70.0.3538.110, webStorageEnabled: true}
Session ID: b042b4252008a1d91dabf9792ede8fb6
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:485)
at com.seleniumbasics.TestVideo.main(TestVideo.java:32)
Hey,
ReplyDeleteVery helpful post for video testing.
Good one.. Keep it up
Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays..
ReplyDeleteOracle DBA Online Training
I'm getting play, pause not a function error, please help me to get new names
ReplyDeleteis it possible to change video quality in youtube video?
ReplyDeleteYes, it is possible.
DeleteI can't get it to click on the Full-Screen button, and that functionality is disabled for JS due to security reasons. Is there a way to launch Full-Screen JWPlayer video through automation?
ReplyDeletecapture the src url of the video & then navigate /open that url in same/new window.
DeleteI want to test a video is working with Python code using selenium. can someone please guide me how to do it using bealutiful soup or any other lib with Python?
ReplyDeleteHi,
ReplyDeleteI am working on a webpage that has watch video button --> when i click on that button --> a window with a video opens on the home page --> when i right click on the window i see reload frame, view frame source --> so i am guessing it is an iframe. i want to automate play and pause. i dont know how to find what player it is using. when i use ur code and use jwplayer.... i get "javascript error: jwplayer is not defined"
1. how do i find what player is used?
when i right click --> i see iframe and within this tag, the src says youtube and also in one of the lines it says /s/player and again one of the lines says "www-embed-player"... can u plz help me automate play and pause
if you could give sample url which has this video, i can try & get back to you
DeleteThank you for the great article.It is very helpful.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
ReplyDeleteThanks, this is generally helpful.
Still, I followed step-by-step your method in this selenium online training
selenium certification
selenium online courses
learn selenium online
how do you end the video? or control the slider and send it to the end of the video? i know how to start and pause the video but i need it to end quickly. is there a way to do that?
ReplyDeleteA really informative blog.
ReplyDeleteThanks and that i have a swell give: How To Plan House Renovation house renovation calgary
ReplyDelete