本文将step by step的讲述第一个selenium实例:
step1:下载selenium-remote-control.下载地址:http://www.openqa.org/selenium-rc/download.action
step2:打开eclipse 新建java project.
step3:将junit.jar,selenium-remote-control-0.9.0/selenium-java-client-driver 以及selenium-remote-control-0.9.0/server/selenium-server添加至新建项目的编译路径下.
step4:新建Testgoogle.java,代码如下.
package test;
import junit.framework.TestCase;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class TestGoogle extends TestCase {
private Selenium selenium;
public void setUp() throws Exception{
String url="http://www.google.cn";
selenium=new DefaultSelenium("localhost",4444, "*firefox", url);
System.out.println("init selenium");
selenium.start();
System.out.println("start successfully");
}
public void tearDown() throws Exception{
selenium.stop();
}
public void testGoogleTestSearch() throws Throwable {
System.out.println("enter testGoogleTestSearch");
selenium.open("/");
System.out.println("open the google.com");
selenium.type("q", "selenium");
System.out.println("input type condition");
selenium.click("btnG");
System.out.println("begin search");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("s"));
System.out.println("finsh assert");
}
}
step5:右键选择testGoogle.java,选择run as junit.
看到junit的绿色通过提示条.
remark:1.firefox的安装路径为默认路径,如果为非默认路径安装,需要把firefox.exe的路径写入环境变量path中.
2 出现location.href权限不足错误,在url路径后加"/",另外要确保你的浏览器能够打开www.google.com.
本文代码中用了www.google.cn,因为本人浏览器中会自动跳转到cn,而不是com.selenium在录制时候, 这种跳转对应关系录制不到.