selenium学习之环境搭建

安装包

selenium-server-2.26.0.zip

selenium-java-2.26.0.zip

浏览器:火狐 版本17 

selenium IDE firefox插件

软件包下载路径:http://code.google.com/p/selenium/downloads/list

这是测试程序的截图 

selenium学习之环境搭建_第1张图片


步骤:

1.新建eclipse项目,加入junit4支持

2.添加 selenium-java-2.26.0.jar、selenium-server-2.26.0.jar、selenium-server-standalone-2.26.0.jar三个jar包的支持

3.火狐下,使用selenium IDE录制访问百度首页的脚本 

selenium学习之环境搭建_第2张图片

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....

运行测试用例,脚本测试通过。



你可能感兴趣的:(selenium,webdriver)