安裝包 java
selenium-server-2.26.0.zip web
selenium-java-2.26.0.zip 瀏覽器
瀏覽器:火狐 版本17 app
selenium IDE firefox插件 eclipse
軟件包下載路徑:http://code.google.com/p/selenium/downloads/list 測試
這是測試程序的截圖 ui
步驟: google
1.新建eclipse項目,加入junit4支持 firefox
2.添加 selenium-java-2.26.0.jar、selenium-server-2.26.0.jar、selenium-server-standalone-2.26.0.jar三個jar包的支持 插件
3.火狐下,使用selenium IDE錄製訪問百度首頁的腳本
4.創建OpenUrl類
import java.util.concurrent.TimeUnit; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class OpenUrl { private WebDriver driver; private String baseUrl; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); baseUrl = "http://www.baidu.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testOpen() throws Exception { driver.get(baseUrl + "/"); driver.findElement(By.id("kw")).clear(); driver.findElement(By.id("kw")).sendKeys("selenium"); driver.findElement(By.id("su")).click(); driver.findElement(By.linkText("Selenium - Web Browser Automation")).click(); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { Assert.fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } }備註:
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");若是沒有加入這句,會報以下的異常信息。
org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP....
運行測試用例,腳本測試經過。