user2467785
user2467785

Reputation: 49

Play the video using Selenium WebDriver with Java

I am trying to play the video (Using JUnit) - Day01 of following website. http://www.itelearn.com/live-training/security-testing-live-training What I am trying to achieve is after playing the video I will take a screen shot to prove that video is playing properly. After clicking on Day01 video, it opens in a new window- when I looked at the code I realized that they have used iFrame. I am able to close this video window but not able to play/pause this video.

To close the video I have used code-- WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))).click();

I am new to testing please help me.

Upvotes: 1

Views: 18679

Answers (3)

Hukmaram
Hukmaram

Reputation: 533

WebDriver driver=new FirefoxDriver();
driver.get("https://www.wonderplugin.com/wordpress-lightbox");
WebElement element=driver.findElement(By.xpath("//a[contains(text(),'Open a Div in Lightbox')]"));
element.click();
WebElement frameElement=driver.findElement(By.xpath("//iframe[@src='https://www.youtube.com/embed/wswxQ3mhwqQ']"));
        driver.switchTo().frame(frameElement);
        driver.findElement(By.xpath("//button[@aria-label=\'Play\']")).click();

Upvotes: 0

Konstantin Yakovskyi
Konstantin Yakovskyi

Reputation: 33

Try to use JavaScriptExecutor. The following approach works for me:

import org.openqa.selenium.JavascriptExecutor;

JavascriptExecutor js = (JavascriptExecutor) driver;
js .executeScript("document.getElementById(\"video\").play()");

I was able to play video from https://www.w3.org/2010/05/video/mediaevents.html and verify Progress field.

Good luck!

Upvotes: 1

Yi Zeng
Yi Zeng

Reputation: 32855

So did you switch into the iframe?

Try the following untested Java code, please note that the logic here is simple, you find the iframe element by xpath or css selector, then switch to it, then click. However, automating player may not be that easy and stable. Provide feedback if you can, thanks.

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement playerIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#sb-player iframe")));
driver.switchTo().frame(playerIframe);

// make sure you have html5 video loaded, instead of flash
// otherwise Selenium won't find a thing
driver.findElement(By.cssSelector("svg.ytp-cued-icon")).click();

To load HTML5 by default, please see this page or load extension HTML5 Video for YouTube when starting Selenium.

Upvotes: 2

Related Questions