(edit by king)最近开始尝试使用selenuim进行多浏览器的测试,编写代码前的第一步,当然是搞定整个开发环境。
为了方便以后的开发,目前选定的是使用java通过selenuim RC来执行测试代码。java的IDE,毫无疑问是eclipse了。为了方便管理依赖,使用maven来进行selenuim依赖的管理和最终的执行。
首先先要安装m2eclipse,很久没有安装了,突然发现原来的安装地址被改了,现在的是http://m2eclipse.sonatype.org/sites/m2e 。相比原来的安装,现在只需要选择一个插件就可以了,免去了原来选择n多插件的痛苦(特别是有的插件有依赖)。不过,现在内置的maven貌似没有了全局的settings.xml,后面执行的时候报这个文件找不到,指定了外部的maven后解决了这个问题。
然后就用m2eclipse创建一个工程。创建默认的工程就可以了。在工程中增加selenuim maven plugin,这个插件的配置,可以参考http://mojo.codehaus.org/selenium-maven-plugin/examples/using-with-surefire.html 这里的配置。搞定这个插件之后,需要增加selenium-java-client-driver这个依赖,引入selenuim rc的java driver。最后junit的版本可以根据自己的喜好改下,默认maven添加的是junit3.
现在就可以写代码了。先写段代码测试下:
public class TestClass {
@Test
public void testOpenFirefox() {
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444,
"*firefox", "http://localhost");
selenium.start();
selenium.open("http://localhost");
selenium.waitForPageToLoad("30000");
if(!selenium.isTextPresent("work")) {
selenium.captureEntirePageScreenshot("screenshot.png", "");
}
selenium.stop();
}
}
这里打开的是本地apache启动的默认页面,也就是传说中的it works页面。
遇到的问题:
- 提示settings.xml没有找到:通过指定外部的maven可以解决
- 测试代码未执行:在eclipse中选择run as -> maven install,试了下,使用maven package也不会运行集成测试
- 提示annotation不能使用:在eclipse中指定使用jdk1.5以上的兼容性,在项目的pom.xml中的plugins标签中增加:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin>
强制指定编译器使用的版本。