<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

一、下载文件

先要去官网(http://seleniumhq.org/download/)下载必需的文件:

  • Selenium IDE (专门用于 FireFox 测试的独立界面,可以录制测试步骤,但我更倾向于写代码做标准的功能测试)
  • Selenium Server (可以输入指令控制、可以解决跨域的 js 问题,等到后面学到了再讲吧)
  • The Internet Explorer Driver Server (专门用于IE测试的)
  • Selenium Client Drivers (可以找到你熟悉的语言,例如我选择的 Java)
  • Third Party Browser Drivers NOT SUPPORTED/DEVELOPED by seleniumhq(第三方开发的 Selenium 插件,第一个就是 Chrome 的,否则你就没办法测试 Chrome 了)
  • 其他的,就根据你自己的需要寻找吧,目前这些足够我用了。

二、安装 & 运行

貌似摆弄新东西时,只有 “Hello World” 蹦出来以后,我们这些初学者才会感到情绪稳定,那就赶紧开始吧。

对于初期打算直接用编程方式制作测试用例的情况来说,Selenium IDE、Selenium Server 都可以不用安装执行。 
英语好的朋友可以直接看官网的文档(http://seleniumhq.org/documentation/)就能够开始使用了。 
看中文的,就继续听我唠叨:

【1. 建立 Maven 工程】 
Selenium 支持 maven 工程,这会让你的工作更加简便。

用 Eclipse 建个 Maven 的工程,建成后,直接修改 pom.xml,(参考:http://seleniumhq.org/docs/03_webdriver.html#setting-up-a-selenium-webdriver-project)

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

pom.xml 修改保存后,eclipse 会自动把需要的 jar 包下载完成。

【2. 测试 FireFox】 
Selenium 最初就是在 FireFox 上做起来的插件,所以我们先来搭建 FireFox 的环境。 
确保你正确安装了 FireFox 后,就可以直接编写 java 代码测试喽。

在 lesson1 目录下建立 ExampleForFireFox.java 
(因为国内不少朋友访问 google 的时候会出问题,所以我就把代码中的 google 变成 baidu 了)

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

普通情况下,直接运行代码就可以看到会自动弹出 FireFox 窗口,访问 baidu.com,然后输入关键字并查询,一切都是自动完成的。

错误提醒: 
1)Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. 
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量  "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。

2)Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request

出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1  localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢?

经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)

【3. 测试 Chrome】 
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。 
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java ,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。

错误如下: 
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list 
这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin

设置路径后还是会报错: 
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment. 
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome 的第三方插件 ChromeDriver,去看第一个错误中提示给你的 网址:http://code.google.com/p/selenium/wiki/ChromeDriver 
按照人家给的例子来修改我们的测试代码吧:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package lesson1;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForChrome {
    public static void main(String[] args) throws IOException {
        // 设置 chrome 的路径
        System.setProperty(
                "webdriver.chrome.driver",
                "C:\\Documents and Settings\\sq\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe");
        // 创建一个 ChromeDriver 的接口,用于连接 Chrome
        @SuppressWarnings("deprecation")
        ChromeDriverService service = new ChromeDriverService.Builder()
                .usingChromeDriverExecutable(
                        new File(
                                "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe"))
                .usingAnyFreePort().build();
        service.start();
        // 创建一个 Chrome 的浏览器实例
        WebDriver driver = new RemoteWebDriver(service.getUrl(),
                DesiredCapabilities.chrome());
        // 让浏览器访问 Baidu
        driver.get("http://www.baidu.com");
        // 用下面代码也可以实现
        // driver.navigate().to("http://www.baidu.com");
        // 获取 网页的 title
        System.out.println("1 Page title is: " + driver.getTitle());
        // 通过 id 找到 input 的 DOM
        WebElement element = driver.findElement(By.id("kw"));
        // 输入关键字
        element.sendKeys("zTree");
        // 提交 input 所在的 form
        element.submit();
        // 通过判断 title 内容等待搜索页面加载完毕,间隔10秒
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().endsWith("ztree");
            }
        });
        // 显示搜索结果页面的 title
        System.out.println("2 Page title is: " + driver.getTitle());
        // 关闭浏览器
        driver.quit();
        // 关闭 ChromeDriver 接口
        service.stop();
    }
}

运行一下看看,是不是一切OK了?

补充:仔细看了一下官网的介绍:Chrome Driver is maintained / supported by the Chromium project iteslf.  看来如果使用 new ChromeDriver() 的话,应该要安装 Chromium 而不是 Chrome,我现在懒得折腾了,有兴趣的童鞋可以试验一下。

【4. 测试 IE】 
想逃避 IE 吗?? 作为前端开发,IE 你是必须要面对的,冲吧! 
其实你会发现, Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox 的代码修改为 IE 的,那是相当的容易,只需要简单地两步: 
1)把 ExampleForFireFox.java 另存为 ExampleForIE.java  
2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver driver = new InternetExplorerDriver(); 
3)一般大家的 IE都是默认路径吧,所以也就不用设置 property 了

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

运行一下,是不是 so easy?

入门工作完成,现在完全可以利用 java 代码,让 Selenium 自动执行我们设置好的测试用例了,不过.....这仅仅是个开始。

自动化测试工具 Selenium WebDriver 入门教程相关推荐

  1. python selenium webdriver入门基本操作

    from selenium import webdriver import timedriver=webdriver.Chrome() driver.get('http://www.baidu.com ...

  2. Python3 爬虫学习笔记 C04 【自动化测试工具 Selenium】

    Python3 爬虫学习笔记第四章 -- [自动化测试工具 Selenium] 文章目录 [4.1]下载驱动 [4.2]声明浏览器对象 [4.3]访问页面 [4.4]启动参数 [4.5]查找节点 [4 ...

  3. 小白学 Python 爬虫(28):自动化测试框架 Selenium 从入门到放弃(下)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  4. Python实训day07am【爬取数据接口、webdriver、自动化测试工具selenium】

    Python实训-15天-博客汇总表 目录 1.网络爬虫-课后练习题 1.1.写法1 1.2.写法2 2.Selenium自动化测试工具 2.1.安装工具 2.2.命令行操作 直接爬取HTML (30 ...

  5. python自动测试p-python网络爬虫之自动化测试工具selenium[二]

    @ 前言 hello,大家好,在上章的内容里我们已经可以爬取到了整个网页下来,当然也仅仅就是一个网页. 因为里面还有很多很多的标签啊之类我们所不需要的东西. 额,先暂且说下本章内容,如果是没有丝毫编程 ...

  6. Selenium Webdriver 简易教程

    Selenium是ThroughtWorks公司开发的一套Web自动化测试工具. 它分为三个组件: Selenium IDE Selenium RC (Remote Control) Selenium ...

  7. 小白学 Python 爬虫(27):自动化测试框架 Selenium 从入门到放弃(上)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  8. 自动化测试工具-selenium介绍及基本使用方法

    Selenium是一个开源.免费.简单.灵活,对Web浏览器支持良好的自动化测试工具,在UI自动化.爬虫等场景下是十分实用的,能够熟练掌握并使用Selenium工具可以大大的提高效率. Seleniu ...

  9. Selenium爬虫入门教程

    Selenium自动化爬取谷歌学术(学术个人档案的h指数和i10指数) Selenium自动化爬取谷歌学术 Selenium自动化爬取谷歌学术(学术个人档案的h指数和i10指数) Selenium库与 ...

最新文章

  1. Android呼出电话流程(原)
  2. Android属性动画 XML
  3. 算法设计与分析:芯片测试问题、选择问题详解
  4. kettle MySQL blob_KETTLE BLOB 问题
  5. 云计算计算机二级,2021计算机二级office用的是哪个版本
  6. 自动化运维落实基础及常用工具对比
  7. 大工微电子器件可靠性基础作业四威布尔分布小题求参数m和t0
  8. 常用数字及模拟视频接口
  9. 华为手机助手上架流程_华为手机助手导入音乐文件在哪里看 详细步骤介绍
  10. 联想System X 3650M5 服务器装机问题记录
  11. 深入浅出HashMap
  12. Window和WindowManager--《Android开发艺术探索》阅读笔记——第八章
  13. 中文编码问题分析及几种乱码场景
  14. 树莓派串口简单测试语音识别模块
  15. unity中的一个简单易用的A*寻路算法类
  16. 将一元人民币兑换成1分、2分、5分,有几种兑换办法?
  17. Executors 源码解析(JDK8)
  18. java iqq_iQQ 基于WebQQ3.0协议Java开发 跨平台QQ客户端
  19. 用C语言实现一个简单的扫雷游戏
  20. 方法总结分享|金融时间序列联动相关及风险溢出

热门文章

  1. 健身房会员管理系统(Java+Web+MySQL)
  2. UE4 Sequencer的事件调用
  3. v-if与v-show(动态显示dom元素)
  4. 程序员高效的准备HR面试
  5. android ffmpeg neno优化
  6. PC与服务器的区别有哪些?
  7. linux常用命令课堂总结
  8. 调用tel://打电话
  9. Android 打电话程序
  10. php订阅号怎么借服务号权限,微信订阅号已经认证能否有网页授权功能