分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

WebDriver高级应用实例(10)

发布时间:2023-09-06 02:35责任编辑:蔡小小关键词:Web

  10.1控制HTML5语言实现的视频播放器

  目的:能够获取html5语言实现的视频播放器视频文件的地址、时长、控制进行播放暂停

  被测网页的网址:

  http://www.w3school.com.cn/tiy/t.asp?f=html5_video_all

  Java语言版本的API实例代码

package cn.html5;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import java.io.File;import java.io.IOException;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.chrome.ChromeDriver;import org.testng.Assert;import org.testng.annotations.AfterMethod;public class TestHtml5VideoPlayer { ???WebDriver driver; ???String url = "http://www.w3school.com.cn/tiy/t.asp?f=html5_video_all"; ?@Test ?public void testVideoPlayer() throws InterruptedException, IOException { ?????//定义页面截图对象 ?????File captureScreenFile = null; ?????//打印页面源码 ?????System.out.println(driver.getPageSource()); ????//进入视频所在的frame ?????driver.switchTo().frame("i"); ?????//获取页面video对象 ?????WebElement videoPlayer = driver.findElement(By.tagName("video")); ?????//声明javascriptExecutor对象 ?????JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver; ?????//获取视频文件的网络地址 ?????String videoSrc = (String) javascriptExecutor.executeScript("return arguments[0].currentSrc;",videoPlayer); ?????//输出视频存储地址 ?????System.out.println(videoSrc); ?????//断言判断视频存储地址是否正确 ?????Assert.assertEquals("http://www.w3school.com.cn/i/movie.ogg",videoSrc); ?????//duration获取视频的时长 ?????Double videoDuration = (Double)javascriptExecutor.executeScript("return arguments[0].duration;", videoPlayer); ?????//输出视频时长 ?????System.out.println(videoDuration.intValue()); ?????//等待5秒 ?????Thread.sleep(5000); ?????//执行javascript,通过内部的函数play()来播放影片 ?????javascriptExecutor.executeScript("return arguments[0].play();", videoPlayer); ?????Thread.sleep(2000); ?????//执行javascript语句,通过内部的函数pause()来暂停影片 ?????javascriptExecutor.executeScript("return arguments[0].pause();", videoPlayer); ?????Thread.sleep(3000); ?????//对暂停的视频进行截图 ?????captureScreenFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); ?????//将截图的图片命名并保存在E盘上 ?????FileUtils.copyFile(captureScreenFile, new File("e:\\videoPaly_pause.jpg")); ?} ?@BeforeMethod ?public void beforeMethod() { ?????System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe"); ?????driver = new ChromeDriver(); ?????driver.manage().window().maximize(); ?????driver.get(url); ?} ?@AfterMethod ?public void afterMethod() { ?????driver.quit(); ?}}

   代码解释:

 控制视频播放器的原理均需使用JavaScript语句调用视频播放器内部的属性和接口来实现。

WebDriver高级应用实例(10)

原文地址:https://www.cnblogs.com/z-zzz/p/10552256.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved