相信Selenium IDE 和 Selenium RC 不少人都用过, 如果使用最新的JUnit4 来编写功能测试, 就可以更容易的从CI 服务器来运行自动化的功能测试, 减少因为代码变更而出现功能与以前不一致的bug.
下载
用firefox 到http://selenium-ide.openqa.org/download.jsp 下载selenium-ide 这个firefox 插件, 然后安装,
到http://release.openqa.org/selenium-remote-control/ 下载selenium-rc , 它包括python,php,java ,ruby 等的测试驱动.
用Selenium 编写一个测试
在firefox 里面的菜单Tools –> Selenium IDE ,
打开了selenium ide 之后你可以录制一个测试案例,记得最后选择Java 的格式.
在selenium ide 里面Options ->Format -> Java . 然后保存.
注意,它默认的格式是junit3 的, 后面我们使用junit4 的格式.
开启和关闭Selenium Server
在下载的selenium-server 包里面,有一个selenium-server.jar 文件, 你可以在命令行下启动.
java -jar selenium-server.jar
如果你想关闭的话在浏览器里打开:
http://localhost:4444/selenium-server/driver/?cmd=shutDown
当然我们后面是用纯手工编程的方式.
创建一个Junit4 的Test Case
打开eclipse , 新建一个java 项目,引入junit4 library , 然后加上selenium-server.jar 和selenium-java-client-driver.jar .
新建一个Junit 测试. 比如如下这段代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class WebAppTest {
private static SeleniumServer server;
private static Selenium selenium;
@BeforeClass
public static void setUp() throws Exception {
server = new SeleniumServer();
server.start();
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
selenium.start();
}
@AfterClass
public static void tearDown() throws Exception {
selenium.close();
server.stop();
}
@Test
public void testNew() throws Exception {
// copy and paste from Selenium RC
selenium.open("/ig?hl=zh-CN");
selenium.type("q", "selenium ide");
selenium.click("btnG");
// check the title of the page
String title = selenium.getTitle();
Assert.assertEquals("iGoogle", title);
}
} |
Selenium Server 与 Firefox3
默认的Selenium Server 是不能与Firefox 3 一起工作的. 不过只要你修改一下selenium-server.jar 文件就可以了:
解压后的Selenium-server.jar 文件下有customProfileDirCUSTFFCHROME 和 customProfileDirCUSTFF 这两个文件, 在里面的每一个文件夹下都有install.rdf 这个文件,用文本工具打开, 把里面的
<em:maxVersion>2.0.0.*</em:maxVersion>
改成
<em:maxVersion>4.0.0.*</em:maxVersion>
一共有五个install.rdf 文件, 修改完之后重新打包成selenium-server.jar 文件. 注意文件夹的结构别变了,对照原来的selenium-server.jar 文件看一下.
如果你使用的winrar 这个解压工具,你可以不用解压直接在里面修改install.rdf 文件,你保存的时候winrar 会自动保存到里面,如果你想我这样使用的是7zip 这个工具, 就要先解压到一个文件夹,然后修改,然后可以打包成selenium-server.zip 文件,然后再把zip 的文件夹后缀改成jar 就可以了.
如果你在eclipse 里面不能运行上面的junit 测试的话 , 它可能提示没有找到firefox , 要把firefox 添加到path 里面, 你就在把你的firefox.exe 所在的那个文件夹添加到你的path 变量里面.
参考资料
1. http://jee-bpel-soa.blogspot.com/2008/10/web-application-testing-with-selenium.html
如何使用selenium ide ,selenium-rc 和junit 的问题.
2. http://notetodogself.blogspot.com/2008/10/use-selenium-rc-in-firefox-3.html 如何解决selenium-rc 与firefox 3问题
3. http://www.51testing.com/?26285/action_viewspace_itemid_17464.html
selenium 所有命令