测试网址:http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_video_simple
package com.allin.pc;
import java.io.File;
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.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.sun.jna.platform.FileUtils;
public class html5VedioPlay {
public WebDriver driver;
String baseUrl = "http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_video_simple";
@Test
public void testVideoPlay() throws InterruptedException{
//定义页面截图文件对象,用于后面的屏幕截图储存
File captureScreenFile = null;
//访问HTML5实现播放器的网页页面
driver.get(baseUrl);
//打印出HTML5视频播放器的源码
System.out.println(driver.getPageSource());
//获取页面中的video标签
WebElement videoPlayer = driver.findElement(By.tagName("video"));
//声明一个JavascriptExecutor对象
JavascriptExecutor javascritExecutor = (JavascriptExecutor) driver;
//使用JavaScriptExecutor对象执行JavaScript语句,通过播放器内部的
//currentsrc属性获取视频文件的网络存储地址
String videoSrc = (String) javascritExecutor.executeScript("return arguments[0].currentSrc;",
videoPlayer);
//输出视频文件的网络存储地址
System.out.println(videoSrc);
//断言视频网络地址是否合期望
Assert.assertEquals("http://www.w3school.com.cn/i/movie.ogg", videoSrc);
//使用JavascriptExecutor对象执行JavaScript语句,通过播放器内部的duration属性获取视频文件的播放时长
Double videoDuration = (Double) javascritExecutor.executeScript("return arguments[0].duration;",
videoPlayer);
//输出视频的播放时长
System.out.println(videoDuration.intValue());
//等待5秒视频加载时间
Thread.sleep(5000);
//使用JavaScriptExecutor对象执行JavaScript语句,通过调用播放器内部的play函数来播放影片
javascritExecutor.executeScript("return arguments[0].play();", videoPlayer);
Thread.sleep(2000);
//播放2秒后,使用JavaScriptExecutor对象执行JavaScript语句,通过调用播放器内部的pause函数暂停播放影片
javascritExecutor.executeScript("return arguments[0].pause();", videoPlayer);
Thread.sleep(3000);
//将暂停视频播放后的页面进行截图,并保存D盘上的videoPlay_pause.jpg文件
captureScreenFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//FileUtils.copyFile(captureScreenFile, new File("d:\\videoPlay_pause.jpg"));
}
@BeforeMethod
public void setUp(){
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}