本文的主题是基于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; } } }