本文將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在錄製時候, 這種跳轉對應關係錄製不到.
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在錄製時候, 這種跳轉對應關係錄製不到.