好像Selenium在Web自动化测试中很火嘛,最近在GoogleReader中看到几篇博客都在讲Selenium,忽然周末花了点儿时间了解和实践了一下Selenium。尽管曾经在阿里做Web测试时还没怎么研究过它,不过那时积累过一些Java、Maven、JUnit、Watir方面东西,加上现在喜欢看英文文档了,所以这次随意学习Selenium还比较正常,遇到的各种问题在Google的帮助下都比较好地解决了。在Internet非常普及和流行的今天,就算目前没做Web,但是了解一些Web自动化测试,而且平时还可以拿来玩玩网页,也是挺不错的事情。
简单做下记录吧,也整理了一些各种参考文档,下次需要时可以回来看看,更快速上手。
本文中使用的软件版本为:
Apache Maven:3.0.4 Apache build manager for Java projects. Apache提供的用于Java项目的构建管理软件。
Selenium IDE: 1.9.0 Firefox上plug-in,可用于录制Selenium测试用的Java脚本。
Selenium: 2.25.0 新的Selenium,是自动化测试的核心,Selenium2相对于Selenium1有重大改进,其中最重要的是抛弃了其中的Selenium-RC(remote control),使用了WebDriver,WebDriver直接调用浏览器原生built-in支持来达到自动化的效果。
JUnit: 4.10 Java单元测试工具。
Java: 1.6.0_29 Java,你懂的,Java运行环境。
Eclipse: Version: Helios Service Release 2 Build id: 20110218-0911 一个Java IDE工具。
OS: Win7 x86_64
Browser:IE9、Firefox15.0
用maven来构建Selenium项目是非常方便的。建立一个项目,使用如下的一个实例pom.xml,然后用maven命令下载Selenium及其依赖的jar包。
4.0.0
MySel20Proj
MySel20Proj
1.0
org.seleniumhq.selenium
selenium-java
2.25.0
com.opera
operadriver
com.opera
operadriver
0.16
org.seleniumhq.selenium
selenium-remote-driver
maven命令如下:
mvn clean install #下载Selenium及其依赖的jar包
mvn eclipse:eclipse #生成为eclipse能识别的项目
然后倒入到Eclipse中,即可很方便的地添加基于Selenium的JUnit测试程序。
写了一个实例JUnit程序(SearchMyBlogTest.java)如下,其作用是打开我的博客(smilejay.cn),输入“Linux关键字”进行搜索,然后简单检查一下页面展示情况。(当然,可以使用Firefox中的Selenium IDE进行录制,然后导出为Java/Junit4/WebDriver这种类型的java文件。)
package com.selenium.test;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SearchMyBlogTest {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://smilejay.cn/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSearchMyBlog() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.name("s")).clear();
driver.findElement(By.name("s")).sendKeys("Linux");
driver.findElement(By.cssSelector("input.button")).click();
int check1 = driver.findElement(By.className("title")).getText().indexOf("搜索结果");
int check2 = driver.findElement(By.className("info")).getText().indexOf("关键字");
int check3 = driver.findElement(By.className("info")).getText().indexOf("Linux");
boolean flag = false;
if ((check1 != -1) && (check2 != -1) && (check3 != -1) ) {
flag = true;
}
System.out.println(driver.findElement(By.className("info")).getText());
Assert.assertTrue(flag);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
将上面程序以JUnit测试程序方式运行即可,它会打开Firefox浏览器然后做相应的操作。
如果该程序编译依赖的jar包,不完善自己需要在Eclipse中设置“Build Path”–“Add External JARs”,添加”junit-4.10.jar”和”selenium-server-standalone-2.25.0.jar”这两个JAR包。
Selenium参考资料:
https://code.google.com/p/selenium/
http://seleniumhq.org/download/
http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html
http://seleniumhq.org/docs/appendix_installing_java_driver_Sel20_via_maven.html
http://seleniumhq.org/docs/03_webdriver.html#introducing-webdriver
JUnit入门手册:
http://junit.sourceforge.net/doc/cookbook/cookbook.htm
http://www.jaredrichardson.net/articles/junit-tutorial.html
http://code.google.com/p/t2framework/wiki/JUnitQuickTutorial
http://www.junit.org/apidocs/org/junit/Assert.html
Java API doc:
http://docs.oracle.com/javase/6/docs/index.html
http://docs.oracle.com/javase/6/docs/api/index.html
http://docs.oracle.com/javase/7/docs/api/index.html
Maven:
http://maven.apache.org/download.html