本文的主題是基於Selenium Server,使用 Java 語言編寫網頁交互流程,
實現多瀏覽器(IE Firefox Chrome)兼容性測試,為使用紀要。
Selenium
Selenium是一套瀏覽器自動化測試工具(http://www.seleniumhq.org/),
其中Selenium IDE為火狐的一個插件,此插件可以實現火狐瀏覽器上用戶操作的錄制和回放功能,
但是錄制結果不能再其他瀏覽器上驗證。
幸好其可以導出 JUnit框架等的測試代碼,讓其可以再其他瀏覽器上執行。
JUnit
是一個單元測試框架,官網見下:
https://github.com/junit-team/junit
博客園上有使用介紹博文:
http://www.cnblogs.com/mengdd/archive/2013/03/26/2983565.html
准備:
1、下載eclipse, 其自帶junit。 下載地址 www.Eclipse.org
2、下載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
3、下載IE Driver,
http://selenium-release.storage.googleapis.com/2.41/IEDriverServer_Win32_2.41.0.zip
4、下載Chrome Driver,
http://chromedriver.storage.googleapis.com/index.html
http://chromedriver.storage.googleapis.com/2.10/chromedriver_win32.zip
安裝配置:
1、啟動eclipse,
創建SeleniumTest工程,
創建包 com.selenium.test.junitcode,
創建類 seleniumTestJunit.java
將 Selenium IDE導出的JUnit代碼放到此類中,
此代碼中只支持Firefox,后面給出代碼支持 IE 和 Chrome。
2、選擇工程, 右鍵, Build Path -》 Add Library,選擇 Junit, 點擊next finish。
3、選擇工程, 右鍵, Build Path -》 Config Build Path,Library 標簽頁, 點擊 Add External Jars,
添加Selenium Server 和 Java Client Driver
4、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
附錄支持Selenium IDE導出JUnit代碼:
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; } } }
