安装包 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....
运行测试用例,脚本测试经过。