大多數瀏覽器使用控件(如 Flash) 來播放規頻,但是,不同的瀏覽器需要使用不同的插件。HTML5 定義了一個新的元素<video>,,指定了一個標準的方式來嵌入電影片段。IE9+、Firefox、Opera、Chrome都支持該元素。
學習蟲師的自動化測試selenium 的4.17節 《處理 HTML5 的視頻播放》。但是無法運行該章節的demo,所以自己搞了一套。從HTML5寫代碼開始。
首先要保證環境可用
MP4視頻在我本機裝的火狐firefox瀏覽器不支持運行,索性使用chrome。之前寫selenium腳本都是基於FirefoxDriver,所以需要下載ChromeDriver。需要注意,selenium版本+ChromeDriver版本+Chrome版本一定要匹配,不然會報錯org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser。據說firefox的驅動還是selenium得人員開發的,而google的驅動是google內部人員開發的,兼容性欠佳。
具體版本匹配可以參考:
http://blog.csdn.net/xqhadoop/article/details/77892796
我目前使用的是selenium 2.49+chrome driver 2.31+chrome version 59
Chrome Driver下載完畢後,可以通過以下兩種方法指定ChromeDriver位置:
1)通過配置ChromeDriver.exe位置到path環境變量實現。
2)通過webdriver.chrome.driver.系統屬性實現。實現代碼如下:
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
準備工作完成,寫一個最簡單的打開瀏覽器的腳本試一下:
package video;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Video3 {
public static void main(String[] args) throws InterruptedException {
System.out.println("chrome driver");
// ChromeDriver沒有放在C盤,所以使用 System 的 setProperty()方法指定瀏覽器驅動的路徑
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// 打開瀏覽器
driver.get("http://www.baidu.com");
Thread.sleep(3000);
driver.quit();
}
}
在Chrome瀏覽器上順利打開,說明驅動沒有問題。
接下來準備html文檔,先隨便下載一個MP4格式的文件,存放在本地,此處存放的路徑爲E:/selenium/aaa.mp4
然後設置controls=“controls”,可控制播放暫停。存放這個html的路徑爲file:///C:/Users/Administrator/Desktop/video2.html
<!DOCTYPE html>
<html>
<head>
<title>HTML5-video</title>
</head>
<body>
<video width="320" height="240" controls="controls">
<source src="E:/selenium/aaa.mp4" type="video/mp4">
您的瀏覽器不支持 video 屬性。
</video>
</body>
</html>
最後基於myEclipse編寫selenium腳本
Chrome瀏覽器可以使用自帶的developer tools定位視頻播放控件的xpath,具體方法可參考http://blog.csdn.net/mayanyun2013/article/details/72148734
WebElement video = driver.findElement(By.xpath("/html/body/video"));
完整代碼:
package video;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Video2 {
public static void main(String[] args) throws InterruptedException {
System.out.println("chrome driver");
// ChromeDriver沒有放在C盤,所以使用 System 的 setProperty()方法指定瀏覽器驅動的路徑
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// 打開瀏覽器
driver.get("file:///C:/Users/Administrator/Desktop/video2.html");
// 定位視頻播放控件
WebElement video = driver.findElement(By.xpath("/html/body/video"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
//獲得視頻的URL
jse.executeScript("return arguments[0].currentSrc", video);
// 播放視頻,播放15秒
jse.executeScript("return arguments[0].play()", video);
Thread.sleep(15000);
// 暫停視頻
jse.executeScript("arguments[0].pause()", video);
driver.quit();
}
}
JavaScript 函數有個內置的對象叫作 arguments。argument 對象包含了函數調用的參數數組,[0]表示取對象的第1個值。
currentSrc 熟悉返回當前音頻/視頻的 URL。如果未設置音頻/視頻,則返回空字符串。
load()、play()、pause() 等控制着視頻的加載、播放和暫停。