最近在研究Java+selenium2的自动化测试,网上的资料比较多,本身学习的同时也顺便记录一下,加深印象的同时也方便之后回顾。 java
1、开发环境:web
一、JDK1.6浏览器
二、Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/eclipse
三、Selenium:selenium-java-2.39.0.zip,下载地址:http://code.google.com/p/selenium/downloads/list学习
解压selenium-java包,这个包里面包含四部分,以下图:测试
2、新建一个Java Project:ui
一、而后把上面解压出来的文件拷到新建的project目录下,目录结构以下图:google
二、添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARsfirefox
把libs文件夹下的jar包所有添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcscode
三、添加完以后目录结构以下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包:
四、关联webdriver的源码:
至此,环境工做准备就绪,下面来写一个简单的小例子。
3、在src下面新建测试类,以下图:
代码以下,主要是打开百度,而后在搜索框输入glen,点击搜索按钮,关闭浏览器。
1 package com.selenium.Glen; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.firefox.*; 7 8 public class TestHelloWorld { 9 10 public static void main(String[] args) { 11 12 //若是火狐浏览器没有默认安装在C盘,须要制定其路径 13 //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 14 WebDriver driver = new FirefoxDriver(); 15 driver.get("http://www.baidu.com/"); 16 17 driver.manage().window().maximize(); 18 19 WebElement txtbox = driver.findElement(By.name("wd")); 20 txtbox.sendKeys("Glen"); 21 22 WebElement btn = driver.findElement(By.id("su")); 23 btn.click(); 24 25 driver.close(); 26 27 } 28 29 }
而后直接右键-->Run As-->Java Application就能够看到效果了。