一、下载必要的文件

1、eclipse

Eclipse官网

2、jdk

jdk官网

3、selenium IDE、Selenium Server、Selenium Client Drivers(Java)等等

Selenium下载地址  备注:需要代理服务器才能下载 我使用的是太太猫

4、maven安装、配置等

二、安装
    1、Eclipse解压缩就可以用了
    2、jdk安装、配置变量等
    3、Selenium相关的安装
    4、maven

最新版本的Eclipse已经自带maven

 

三、运行
    1、Eclipse建个maven工程
,建成后,直接修改pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>Selenium2Test</groupId>
  5. <artifactId>Selenium2Test</artifactId>
  6. <version>1.0</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.seleniumhq.selenium</groupId>
  10. <artifactId>selenium-java</artifactId>
  11. <version>2.25.0</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.opera</groupId>
  15. <artifactId>operadriver</artifactId>
  16. </dependency>
  17. </dependencies>
  18. <dependencyManagement>
  19. <dependencies>
  20. <dependency>
  21. <groupId>com.opera</groupId>
  22. <artifactId>operadriver</artifactId>
  23. <version>0.16</version>
  24. <exclusions>
  25. <exclusion>
  26. <groupId>org.seleniumhq.selenium</groupId>
  27. <artifactId>selenium-remote-driver</artifactId>
  28. </exclusion>
  29. </exclusions>
  30. </dependency>
  31. </dependencies>
  32. </dependencyManagement>
  33. </project>

pom.xml修改保存后,Eclipse会自动把需要的jar包下载完成
    给项目工程配置所需要的库
        工程右击properties->java build path->add library
        
    2、测试Firefox(ExampleForFirefox.java)

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. import org.openqa.selenium.support.ui.ExpectedCondition;
  6. import org.openqa.selenium.support.ui.WebDriverWait;
  7. public class ExampleForFireFox  {
  8. public static void main(String[] args) {
  9. // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
  10. //      System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
  11. // 创建一个 FireFox 的浏览器实例
  12. WebDriver driver = new FirefoxDriver();
  13. // 让浏览器访问 Baidu
  14. driver.get("http://www.baidu.com");
  15. // 用下面代码也可以实现
  16. // driver.navigate().to("http://www.baidu.com");
  17. // 获取 网页的 title
  18. System.out.println("1 Page title is: " + driver.getTitle());
  19. // 通过 id 找到 input 的 DOM
  20. WebElement element = driver.findElement(By.id("kw"));
  21. // 输入关键字
  22. element.sendKeys("zTree");
  23. // 提交 input 所在的  form
  24. element.submit();
  25. // 通过判断 title 内容等待搜索页面加载完毕,间隔10秒
  26. (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
  27. public Boolean apply(WebDriver d) {
  28. return d.getTitle().toLowerCase().endsWith("ztree");
  29. }
  30. });
  31. // 显示搜索结果页面的 title
  32. System.out.println("2 Page title is: " + driver.getTitle());
  33. //关闭浏览器
  34. driver.quit();
  35. }

运行正常的情况下就可以看到自动打开Firefox窗口,访问baidu.com,然后输入关键字并查询

在搭建、测试中出现了几个问题:

问题1:
    Description    Resource    Path    Location    Type ArtifactTransferException: Failure to transfer com.google.code.findbugs:jsr305:jar:1.3.9 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact com.google.code.findbugs:jsr305:jar:1.3.9 from/to central (http://repo.maven.apache.org/maven2): repo.maven.apache.org    pom.xml    /lesson    line 2    Maven Dependency Problem
解决方案:
    可能是连不上http://repo1.maven.org/maven2这个仓库,在pom.xml文件加一下下面的配置试试看。  
xml代码:

  1. <repositories>
  2. <repository>
  3. <snapshots>
  4. <enabled>false</enabled>
  5. </snapshots>
  6. <id>central</id>
  7. <name>Maven Repository Switchboard</name>
  8. <url>http://repo2.maven.org/maven2</url>
  9. </repository>
  10. </repositories>

问题2:
    ava文件(.java)右键run as没有java application
    
解决方法:
    public static void main(String[] args)

这几个必不可少
    public
    static
    void
    main
    String[] arg 或者 String arg[] 
    可能少掉其中一个元素

问题3:
    Caused by: com.thoughtworks.selenium.SeleniumException: Failed to start new browser session: java.lang.RuntimeException: Browser not supported: http://www.baidu.com (Did you forget to add a *?)
    分析:出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量  "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。
解决方法:
    如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
     System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");

    3、测试IE
    Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox 的代码修改为 IE 的,那是相当的容易,只需要简单地两步:
    1)把 ExampleForFireFox.java 另存为 ExampleForIE.java 
    2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver driver = new InternetExplorerDriver();

3)一般大家的 IE都是默认路径,所以也就不用设置 property 了

问题1:
    运行出现
    WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones.' 
解决方法:

进入ie8工具选项->进入“安全”tab->保证"internet" "本地intranet" "可信站点" "受限站点"四个安全设置下的"启用保护模式(要求重新启动Internet Explorer)"的选择是一致的:要么都选,要么都不选。由于ie8在默认情况下,只"受限站点"安全设置内此选项是启用的,所以才会出现上述问题中的exception

自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】的更多相关文章

  1. Selenium Web 自动化 - Selenium(Java)环境搭建

    Selenium Web 自动化 - Selenium(Java)环境搭建 2016-07-29 1 下载JDK JDK下载地址:http://www.oracle.com/technetwork/j ...

  2. 自动化测试基础篇--Selenium Python环境搭建

    学习selenium python需要的工具: 1.浏览器 2.Python 3.Selenium 4.FireBug(Firefox) 5.chromedriver.IEDriverServer.g ...

  3. 引用 自动化测试基础篇--Selenium Python环境搭建

    原文链接:https://www.cnblogs.com/sanzangTst/p/7452922.html 鸣谢参藏法师. 学习selenium python需要的工具: 1.浏览器 2.Pytho ...

  4. 自动化测试selenium+java 环境搭建

    1.开发环境: a:安装jdk,配置java的环境变量 b:安装eclipse 工具.火狐浏览器(低版本) 2.下载selenium包 selenium-java-2.44.0.jar seleniu ...

  5. Selenium+Java环境搭建

    1. 安装JDK URL:http://www.oracle.com/technetwork/java/javase/downloads/ 2. 配置环境变量 JAVA_HOME = E:\Java\ ...

  6. Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建

    1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...

  7. Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...

  8. selenium webdriver 环境搭建--java

    selenium java环境的安装可以分为三个部分:jdk.eclipse和selenium. jdk jdk(java development kit)是sun公司针对java开发人员的产品,是整 ...

  9. java: Eclipse jsp tomcat 环境搭建(完整)

    ] 欢迎您! 要学习一门语言,首先要做的就是搭建环境,然后能写一个小的Demo(类似Helloworld),不仅可以建立信心,而且还可以为之后的学习搭建一个验证平台,事半功倍. net领域的vs,号称 ...

随机推荐

  1. mongodb的mongod.lock文件及oplog文件

    在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件.如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一 ...

  2. python 基础 1.5 python数据类型(四)--字典常用方法示例

    一. 字典 #字典 dict1 = {'name':'lzc','age':'20','sex':'man'} print dict1 print type(dict1) >>> { ...

  3. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:使用BootstrapPagination以分页形式展示数据信息

    上一篇介绍通过接口来获取数据,本篇将介绍如何以分页形式展示出接口获取到的数据 获取到的数据往往会很多,为了追去页面的美观和方便用户的检索,需要进行分页的展示: EasyNVR可接如多通道,当我们的通道 ...

  4. Python 进程、线程、协程、锁机制,你知多少?

    1.python的多线程到底有没有用? 2. 为什么在python里推荐使用多进程而不是多线程 3.进程.线程.协程.各种锁 4.Python多进程编程

  5. 九度OJ 1056:最大公约数 (GCD)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6278 解决:4075 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组输入,请 ...

  6. Hadoop实战-Flume之自定义Sink(十九)

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  7. Hadoop实战-Flume之Source multiplexing(十五)

    a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...

  8. mysql的安装与基本管理

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS ...

  9. 04-树7 二叉搜索树的操作集(30 point(s)) 【Tree】

    04-树7 二叉搜索树的操作集(30 point(s)) 本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType ...

  10. A. Drazil and Date

    这是codeforces#292 div2 的一道题,因为本人比较水,目前只能做div2了.问题简化版就是: 从 (0,0) 走到 (a, b) ,s 步能不能走完.每次能向上下左右走,且只能走一步. ...