码迷,mamicode.com
首页 > 其他好文 > 详细

测试工作——selenium2环境搭建

时间:2016-01-20 19:03:09      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

环境搭建

一、开发环境:

  1、JDK1.6

  2、Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/

  3、Selenium:selenium-java-2.39.0.zip,下载地址:http://code.google.com/p/selenium/downloads/list

  解压selenium-java包,这个包里面包含四部分,如下图:

  技术分享

二、新建一个Java Project:

  1、创建一个java项目:

技术分享

2、然后把上面解压出来的文件拷到新建的project目录下,目录结构如下图:

  技术分享

3、添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs,把libs文件夹下的jar包全部添加上,再添加selenium-java-2.48.2-srcs.jar和selenium-java-2.48.2.jar

  技术分享

3、添加完之后目录结构如下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包:

技术分享

 

 至此,环境工作准备就绪,下面来写一个简单的小例子。

4、在src下面新建测试类,如下图:

技术分享

5、添加代码,实现:打开百度,然后在百度搜索框输入helloworld,点击搜索按钮,关闭浏览器。

技术分享
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;

public class TestClass {

    public static void main(String[] args) {
        
        //如果火狐浏览器没有默认安装在C盘,需要制定其路径
        //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.baidu.com/");
        
        driver.manage().window().maximize();
        
        WebElement txtbox = driver.findElement(By.name("wd"));
        txtbox.sendKeys("HelloWorld");
        
        WebElement btn = driver.findElement(By.id("su"));
        btn.click();
        
        driver.close();

    }

}
技术分享

然后直接右键-->Run As-->Java Application就可以看到效果了。

测试工作——selenium2环境搭建

标签:

原文地址:http://www.cnblogs.com/sheropan/p/5146021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!