This instance is used to obtain the address and duration of the video file of the video player implemented in HTML5 language, and control the player to play or pause.
Tested webpage: http://www.w3school.com.cn/i/movie.ogg
package cn.om.TestHTML5; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; public class TestHTML5VideoPlayer { WebDriver driver; String url; @Test public void testHTML5VideoPlayer() throws IOException, InterruptedException { // Define the page screenshot file object for later screenshot storage File captureScreenFile = null; driver.get(url); System.out.println(driver.getPageSource()); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // The video object is placed in the iframe, you must switch to the iframe first to locate the video element WebElement iframe = driver.findElement(By.xpath(".//*[@id='result']/iframe")); driver.switchTo().frame(iframe); WebElement videoPlayer = driver.findElement(By.tagName("video")); JavascriptExecutor js = (JavascriptExecutor) driver; // The currentSrc property gets the network storage address of the video file String videoSrc = (String) js.executeScript("return arguments[0].currentSrc;", videoPlayer); System.out.println(videoSrc); // Assert whether the video network address is as expected Assert.assertEquals("http://www.w3school.com.cn/i/movie.ogg", videoSrc); // The duration property gets the playback duration of the video file Double videoDuration = (Double) js.executeScript("return arguments[0].duration", videoPlayer); System.out.println(videoDuration.intValue()); // wait 5 seconds for the video to load Thread.sleep(5000); // Using JavaScript, call play inside the player js.executeScript("return arguments[0].play()", videoPlayer); // wait for two seconds, use the pause method to pause Thread.sleep(2000); js.executeScript("return arguments[0].pause()", videoPlayer); // Pause for 3 seconds to prove whether it has been paused Thread.sleep(3000); captureScreenFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(captureScreenFile, new File("F:\\workspace\\WebDriver API\\2017-2-22\\videoPlay_pause.jpg")); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); url = "http://www.w3school.com.cn/tiy/t.asp?f=html5_video_all"; } @AfterMethod public void afterMethod() { driver.quit(); } }