本文的主題是基於Selenium Server,使用 Java 語言編寫網頁交互流程,html
實現多瀏覽器(IE Firefox Chrome)兼容性測試,爲使用紀要。java
Selenium是一套瀏覽器自動化測試工具(http://www.seleniumhq.org/),git
其中Selenium IDE爲火狐的一個插件,此插件能夠實現火狐瀏覽器上用戶操做的錄製和回放功能, github
可是錄製結果不能再其餘瀏覽器上驗證。web
幸虧其能夠導出 JUnit框架等的測試代碼,讓其能夠再其餘瀏覽器上執行。 chrome
是一個單元測試框架,官網見下:api
https://github.com/junit-team/junit數組
博客園上有使用介紹博文:瀏覽器
http://www.cnblogs.com/mengdd/archive/2013/03/26/2983565.html安全
一、下載eclipse, 其自帶junit。 下載地址 www.Eclipse.org
二、下載Selenium Server 和 Java Client Driver, 下載地址 http://docs.seleniumhq.org/download/
http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar
http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip
三、下載IE Driver,
http://selenium-release.storage.googleapis.com/2.41/IEDriverServer_Win32_2.41.0.zip
四、下載Chrome Driver,
http://chromedriver.storage.googleapis.com/index.html
http://chromedriver.storage.googleapis.com/2.10/chromedriver_win32.zip
一、啓動eclipse,
建立SeleniumTest工程,
建立包 com.selenium.test.junitcode,
建立類 seleniumTestJunit.java
將 Selenium IDE導出的JUnit代碼放到此類中,
此代碼中只支持Firefox,後面給出代碼支持 IE 和 Chrome。
二、選擇工程, 右鍵, Build Path -》 Add Library,選擇 Junit, 點擊next finish。
三、選擇工程, 右鍵, Build Path -》 Config Build Path,Library 標籤頁, 點擊 Add External Jars,
添加Selenium Server 和 Java Client Driver
四、IE 和 Chrome Driver解壓到目錄:
C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\
點擊開始按鈕,依次執行指令,本例中爲打開 Baidu頁,搜索「母親節」, 分別執行 firefox chrome 和 IE。
其中,testCases爲填充測試用例函數。
以下對於每一個瀏覽器都執行下面相似三句,有重複語句,擴展性也很差;
本想使用數組容納函數, 循環執行, 惋惜Java不支持,請Java大俠指點。
prepareFirefoxDriver(); testCases(); testover();
package com.selenium.test.junitcode; import java.io.File; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.Select; public class seleniumTestJunit { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { baseUrl = "http://www.baidu.com"; } private void prepareIEDriver(){ /* IE driver */ File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); driver = new InternetExplorerDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } private void prepareFirefoxDriver(){ /* firefox driver */ driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } private void prepareChromeDriver(){ File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\chromedriver.exe"); System.setProperty("webdriver.chrome.driver", file.getAbsolutePath()); driver = new ChromeDriver(); } private void testCases() throws Exception { driver.get(baseUrl); Thread.sleep(1000); driver.findElement(By.id("kw1")).clear(); driver.findElement(By.id("kw1")).sendKeys("母親節"); Thread.sleep(1000); driver.findElement(By.id("su1")).click(); Thread.sleep(3000); } public void testover() throws Exception { driver.close(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } @Test public void testTt() throws Exception { prepareFirefoxDriver(); testCases(); testover(); prepareChromeDriver(); testCases(); testover(); prepareIEDriver(); testCases(); testover(); } @After public void tearDown() throws Exception { } }
若是IE運行中遇到以下打印, 請退出360安全衛士:
org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. IELaunchURL() returned HRESULT 80070057 ('參數不正確。') for URL 'http://localhost:48104/' (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 5.84 seconds
package com.example.tests; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class Seleniumbaidutest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://www.baidu.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testSeleniumbaidu() throws Exception { driver.get(baseUrl + "/"); driver.findElement(By.id("su")).click(); driver.findElement(By.id("su1")).click(); driver.findElement(By.id("kw1")).clear(); driver.findElement(By.id("kw1")).sendKeys("母親節"); driver.findElement(By.id("su1")).click(); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }