一些準備:JRE1.5+ 和 瀏覽器要求
Windows
如果你用的是Windows XP 或者是Windows2003,你可以使用IE 瀏覽器,或者安裝Mozilla Firefox 或者Opera瀏覽器。
如果你是用的是Windows2000,如果想使用IE瀏覽器,你就需要安裝reg.exe,但是如果使用Firefox不需要。我們建議(但不是必須)將你的瀏覽器執行路徑加到你的PATH環境變量中(如果你不知道如何將你的瀏覽器安裝目錄加到PATH中,那么你就必須將你的瀏覽器安裝到標準路徑下:Firefox的標準路徑:"c:\Program Files\Mozilla Firefox\firefox.exe";IE瀏覽器的標準路徑:"c:\Program Files\Internet Explorer\iexplore.exe")
1. 先去 http://selenium-rc.openqa.org/download.jsp 下載selenium包。解壓。
2. 用命令行來到解壓的文件夾下: \selenium-remote-control-0.9.2\selenium-server-0.9.2
3. 運行: java -jar selenium-server.jar 啟動selenium server
4. 在Eclipse創建一個項目,在項目的build path里面加上junit.jar和selenium-java-client-driver.jar(這個在剛解壓的包里面)
5. 然后在Eclipse里運行 “Run As -> unit Test”即可看到自動化的范例
最后粘貼一下那個測試程序
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class GoogleTest extends TestCase {
private Selenium selenium;
public void setUp() throws Exception {
String url = "http://www.google.com";
selenium = new DefaultSelenium("localhost", 4444, "*firefox", url); //4444 is default server port
selenium.start();
}
protected void tearDown() throws Exception {
selenium.stop();
}
public void testGoogle() throws Throwable {
selenium.open("http://www.google.com/webhp?hl=en");
assertEquals("Google", selenium.getTitle());
selenium.type("q", "Selenium OpenQA");
assertEquals("Selenium OpenQA", selenium.getValue("q"));
selenium.click("btnG");
selenium.waitForPageToLoad("5000");
assertEquals("Selenium OpenQA - Google Search", selenium.getTitle());
}
}
相關資料:
http://www.tkk7.com/becky/archive/2008/03/06/184267.html
http://www.javaeye.com/topic/160592
------君臨天下,舍我其誰
------