此文主要通過 三種瀏覽器(Chrome、Firefox、IE)啟動腳本 功能,進行 Selenium2 三種瀏覽器啟動方法的實戰實例講解。文中所附源代碼於 2015-01-18 20:33 親測通過,敬請親們閱覽。進行編寫登錄自動化測試腳本,若您直接使用此文所附的源代碼運行測試,則需要修改對應 瀏覽器 或 webdriver 的路徑,否則將會引起相應的報錯,請知悉。
希望能對初學 Selenium2 WebUI 自動化測試編程的親們有所幫助。若有不足之處,敬請大神指正,不勝感激!
一、各瀏覽器 WebDriver 驅動文件下載
- Chrome 驅動文件(瀏覽器版本 31 下測試通過):http://yunpan.cn/cyadeJIxkwWwg 提取碼 e475
- Internet Explorer 驅動文件下載(瀏覽器版本 11 下測試通過):http://yunpan.cn/cyadeJIxkwWwg 提取碼 e475
- Firefox 為 Selenium 原生支持,無需下載 webdriver
二、各瀏覽器啟動腳本
當前使用的 Selenium Jar 文件為:selenium-server-standalone-2.42.2.jar
- Chrome

1 /** 2 * Aaron.ffp Inc. 3 * Copyright (c) 2004-2015 All Rights Reserved. 4 */ 5 package main.java.aaron.sele.demo; 6 7 import java.util.concurrent.TimeUnit; 8 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.chrome.ChromeDriver; 11 12 /** 13 * UI自動化功能測試腳本:啟動 Chrome 瀏覽器 14 * 15 * 實現 Chrome 瀏覽器啟動的步驟如下: 16 * 1.設定需要啟動的 Chrome 的安裝路徑 17 * 2.設定 Chrome 對應的 webdriver 18 * 3.啟動 Chrome, 並最大化 19 * 4.打開百度 20 * 5.關閉並退出 21 * 22 * @author Aaron.ffp 23 * @version $Id: StartBrowerChrome.java, V0.1 2015-1-18 15:07:49 Aaron.ffp Exp $ 24 */ 25 public class StartBrowerChrome { 26 private static WebDriver cd; 27 private static String baseUrl; // 百度首頁網址 28 29 /** 30 * 主方法入口 31 * @param args 32 */ 33 public static void main(String[] args) { 34 /* 啟動 chrome */ 35 chromeStart(); 36 /* 打開百度 */ 37 cd.get(baseUrl); 38 /* 等待加載 */ 39 cd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 40 /* 關閉 chrome */ 41 chromeQuit(); 42 } 43 44 /** 45 * Chrome WebDriver 設置, 網址及搜索內容初始化, 打開 Chrome 瀏覽器 46 */ 47 public static void chromeStart(){ 48 /* 設定 chrome 啟動文件的位置, 若未設定則取默認安裝目錄的 chrome */ 49 System.setProperty("webdriver.chrome.bin", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); 50 /* 設定 chrome webdirver 的位置 */ 51 System.setProperty("webdriver.chrome.driver", "C:/Windows/System32/chromedriver.exe"); 52 /* 百度首頁網址賦值 */ 53 baseUrl = "http://www.baidu.com/"; 54 /* 啟動 chrome 瀏覽器 */ 55 cd = new ChromeDriver(); 56 /* 瀏覽器最大化 */ 57 cd.manage().window().maximize(); 58 } 59 60 /** 61 * 關閉並退出 Chrome 62 */ 63 public static void chromeQuit(){ 64 /* 關閉 chrome */ 65 cd.close(); 66 /* 退出 chrome */ 67 cd.quit(); 68 } 69 }
- Firefox

1 /** 2 * Aaron.ffp Inc. 3 * Copyright (c) 2004-2015 All Rights Reserved. 4 */ 5 package main.java.aaron.sele.demo; 6 7 import java.util.concurrent.TimeUnit; 8 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.firefox.FirefoxDriver; 11 12 /** 13 * UI自動化功能測試腳本:啟動 Firefox 瀏覽器 14 * 15 * 實現 Firefox 瀏覽器啟動的步驟如下: 16 * 1.設定需要啟動的 Firefox 的安裝路徑 17 * 2.啟動 Firefox, 並最大化 18 * 3.打開百度 19 * 4.關閉並退出 20 * 21 * @author Aaron.ffp 22 * @version $Id: StartBrowerFirefox.java, V0.1 2015-1-18 15:08:46 Aaron.ffp Exp $ 23 */ 24 public class StartBrowerFirefox { 25 private static WebDriver ff; 26 private static String baseUrl; // 百度首頁網址 27 28 /** 29 * Firefox WebDriver 設置, 網址及搜索內容初始化, 打開 Firefox 瀏覽器 30 */ 31 public static void FirefoxStart(){ 32 /* 設定 Firefox 啟動文件的位置, 若未設定則取默認安裝目錄的 FirefoxQuit */ 33 // System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe"); 34 /* 設定 Firefox webdirver 的位置, Selenium 提供了對 Firefox 的原生支持,因而不要設定其 webdriver */ 35 36 /* 百度首頁網址賦值 */ 37 baseUrl = "http://www.baidu.com/"; 38 /* 啟動 Firefox 瀏覽器 */ 39 ff = new FirefoxDriver(); 40 /* 瀏覽器最大化 */ 41 ff.manage().window().maximize(); 42 } 43 44 /** 45 * @function 測試主入口 46 * @description 47 * @author Aaron.ffp 48 * @version V0.1: main, 2015年1月19日 下午5:26:05 Aaron.ffp Exp $ 49 * 50 * @param args 51 */ 52 public static void main(String[] args) { 53 /* 啟動 Firefox */ 54 FirefoxStart(); 55 /* 打開百度 */ 56 ff.get(baseUrl); 57 58 /* 等待加載 */ 59 ff.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 60 61 /* 關閉 Firefox */ 62 // FirefoxQuit(); 63 } 64 65 /** 66 * 關閉並退出 Firefox 67 */ 68 public static void FirefoxQuit(){ 69 /* 關閉 Firefox */ 70 ff.close(); 71 /* 退出 Firefox */ 72 ff.quit(); 73 } 74 }
- Internet Explorer

1 /** 2 * Aaron.ffp Inc. 3 * Copyright (c) 2004-2015 All Rights Reserved. 4 */ 5 package main.java.aaron.sele.demo; 6 7 import java.util.concurrent.TimeUnit; 8 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.ie.InternetExplorerDriver; 11 import org.openqa.selenium.remote.DesiredCapabilities; 12 13 /** 14 * UI自動化功能測試腳本:啟動 InternetExplorer 瀏覽器 15 * 16 * 實現 InternetExplorer 瀏覽器啟動的步驟如下: 17 * 1.設定需要啟動的 InternetExplorer 的安裝路徑 18 * 2.設定 InternetExplorer 對應的 webdriver 19 * 3.設定 InternetExplorerDriver 啟動參數 20 * 4.啟動 InternetExplorer, 並最大化 21 * 5.打開百度 22 * 6.關閉並退出 23 * 24 * @author Aaron.ffp 25 * @version $Id: StartBrowerIE.java, V0.1 2015-1-18 15:12:33 Aaron.ffp Exp $ 26 */ 27 public class StartBrowerIE { 28 private static WebDriver ie; 29 private static String baseUrl; // 百度網址 30 31 /** 32 * 設定系統環境, 啟動 IE 瀏覽器 33 */ 34 public static void ieStart(){ 35 /* 設定 IE 瀏覽器啟動文件路徑 */ 36 System.setProperty("webdriver.ie.bin", "C:/Program Files/Internet Explorer/iexplore.exe"); 37 /* 設定 IEDriverServer 文件路徑 */ 38 System.setProperty("webdriver.ie.driver", "c:/windows/system32/IEDriverServer.exe"); 39 40 /* 設定百度網址 */ 41 baseUrl = "http://www.baidu.com"; 42 43 /* 設定 InternetExplorerDriver 參數, 忽略安全驗證, 忽略后測試腳本將不穩定或難於調試 */ 44 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); 45 ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 46 ie = new InternetExplorerDriver(ieCapabilities); 47 48 /* 瀏覽器最大化 */ 49 ie.manage().window().maximize(); 50 } 51 52 /** 53 * 54 * @param args 55 */ 56 public static void main(String[] args) { 57 /* 啟動 IE 瀏覽器 */ 58 ieStart(); 59 /* 打開百度網址 */ 60 ie.get(baseUrl); 61 62 /* 等待加載 */ 63 ie.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 64 65 /* 退出並關閉 IE 瀏覽器 */ 66 ieQuit(); 67 } 68 69 /** 70 * 退出並關閉 IE 瀏覽器 71 */ 72 public static void ieQuit(){ 73 ie.close(); 74 ie.quit(); 75 } 76 }
若將第 44 - 46 行(忽略瀏覽器設定的安全域驗證)注銷,改為 ie = new InternetExplorerDriver(); 則運行腳本時無法通過瀏覽器設定的安全域驗證,會提示如下報錯信息:

1 Started InternetExplorerDriver server (32-bit) 2 2.37.0.0 3 Listening on port 38775 4 Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information) 5 Command duration or timeout: 1.18 seconds 6 Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37' 7 System info: host: 'AaronFan-PC', ip: '10.24.68.138', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0' 8 Driver info: org.openqa.selenium.ie.InternetExplorerDriver 9 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 10 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 11 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 12 at java.lang.reflect.Constructor.newInstance(Constructor.java:408) 13 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204) 14 at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162) 15 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) 16 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) 17 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:225) 18 at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:182) 19 at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:174) 20 at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:146) 21 at main.java.aaron.sele.demo.StartBrowerIE.ieStart(StartBrowerIE.java:38) 22 at main.java.aaron.sele.demo.StartBrowerIE.main(StartBrowerIE.java:47)
至此,WebUI 自動化功能測試腳本第 003 篇-三種瀏覽器(Chrome、Firefox、IE)啟動腳本 順利完結,希望此文能夠給初學 Selenium 的您一份參考。
最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^