Tuesday 26 July 2016

Test Video using Selenium WebDriver


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);
//Mute Player
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


47 comments:

  1. Hello,
    The 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

    ReplyDelete
  2. Hi, can i get to know how to find the components of jwplayer for automating purpose.
    Like how to find the elements(pause/play/reload). Because we need to use Node.Js for automation.

    ReplyDelete
    Replies
    1. Hi Abhishek,
      As 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.

      Delete
    2. https://omega.cloud.netrovert.net/instructionalvideos
      please check and how to verify whether video is running or not

      Delete
  3. 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?

    ReplyDelete
    Replies
    1. Hi Mahantesh,
      The above example is for JW Player Video, If Youtube uses JWplayer then the above example should work for that too.

      Delete
  4. Also how do we automate some of the following basic video testing using Selenium? Have you tried anything of this sort?

    1. 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?

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

    ReplyDelete
    Replies
    1. How to define jwplayer() in selenium,got error

      Delete
    2. As you see in the above example. YOu donot need to define jwplayer(). It is a string being passed to webdriver instance casted to javaScriptExecutor.
      JavascriptExecutor jse = (JavascriptExecutor) driver;
      jse.executeScript("jwplayer().play();");

      Delete
    3. I also got the same error.

      org.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

      Delete
    4. Usually this error is thrown, when driver do not find the Jwplayer in the Page.
      There 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.

      Delete
    5. 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.

      Delete
    6. Hi Shiva,
      Thanks 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!!

      Delete
    7. @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.
      For your case, i do not have the answer right now.

      Delete
    8. @Shiva Prakash,
      Thank you for your reply.

      How will I come to know that the video is of type jwplayer?

      Delete
    9. @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'.

      But, the video in your page has names like 'vp-player-inner'

      Delete
    10. Thanks for your help @Shiva Prakash

      Delete
  6. Hi Shiva
    Thanks 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.

    ReplyDelete
    Replies
    1. 1. As you said you have multiple links & you need to validate if it is going to right URL after clicking
      -- 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.

      Delete
  7. how do i play a you tube video in my web page after navigating to the src

    ReplyDelete
    Replies
    1. For Youtube videos, you donot have to navigate to source,
      driver.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);

      Delete
  8. How to get Error message like "Error loading player:"

    ReplyDelete
    Replies
    1. Actually, It depends on the video player being used.
      If you can provide any sample, I can try let you know the details.

      Delete
  9. You can try with below code:

    System.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();

    ReplyDelete
  10. You can try with below code to open the video:

    System.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/");

    ReplyDelete
    Replies
    1. 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();

      Delete
  11. You can try with below code to open the video:

    System.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();

    ReplyDelete
  12. Hi Shiva,
    this 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.

    ReplyDelete
  13. 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?

    ReplyDelete
    Replies
    1. Certainly not a stupid one... :).
      You 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

      Delete
  14. Hi this is my error message

    Starting 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)

    ReplyDelete
  15. Hey,
    Very helpful post for video testing.

    Good one.. Keep it up

    ReplyDelete
  16. 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..
    Oracle DBA Online Training

    ReplyDelete
  17. I'm getting play, pause not a function error, please help me to get new names

    ReplyDelete
  18. is it possible to change video quality in youtube video?

    ReplyDelete
  19. I 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?

    ReplyDelete
    Replies
    1. capture the src url of the video & then navigate /open that url in same/new window.

      Delete
  20. I 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?

    ReplyDelete
  21. Hi,
    I 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

    ReplyDelete
    Replies
    1. if you could give sample url which has this video, i can try & get back to you

      Delete
  22. 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?

    ReplyDelete