首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Selenium VS Webdriver(1)

Selenium VS Webdriver(1)

追踪溯源,WebDriver 和 Selenium 本是两个独立的项目,实现机制也是不同的。那 Selenium 团队为什么会在 Selenium 2 中将两者合并,这究竟有什么用意呢?WebDriver 比 Selenium 又有什么优势呢?我们该如何选择使用 Selenium 还是 WebDriver 呢?别着急,您将在本文中找到答案,并将了解一些 WebDriver 的基本知识和使用方法。
    为方便表述,在本文中,我们称 Selenium 2 为 WebDirver,Selenium 为 Selenium 1.x(因为 Selenium1.x 时通常指的是 Selenium RC,所以 Selenium 也指 Selenium RC)。
WebDriver 是… …?Selenium 2,又名 WebDriver,它的主要新功能是集成了 Selenium 1.0 以及 WebDriver​(WebDriver 曾经是 Selenium 的竞争对手)。也就是说 Selenium 2 是 Selenium 和 WebDriver 两个项目的合并,即 Selenium 2 兼容 Selenium,它既支持 Selenium API 也支持 WebDriver API。
    那 Selenium 团队为什么会将两个项目合并呢?我们通常认为其中部分原因是 WebDriver 解决了 Selenium 存在的缺点(比如,能够绕过 JS 沙箱),部分原因是 Selenium 解决了 WebDriver 存在的问题(比如,支持更广泛的浏览器和编程语言),不论真正的原因是什么两个项目的合并为用户提供了一个优秀的自动化测试框架。
    现在让我们看看两个工具有什么具体的不同。在开始之前,我们首先看一下用 Selenium 和用 Webdriver 构建出来的测试工程是什么样的,后文会在这个基础上阐述 Webdriver 和 Selenium 的异同。
说明:因为现在 WebDriver 还在改进和优化过程中,所以我们以下的举例和说明都是基于版本 selenium-2.28.0 的基础上。
构建一个 Selenium 测试工程Selenium API 则支持更多的编程语言,这里我们还是以 Java 为例。
图 1. Selenium 测试工程清单 1. 使用 Selenium API 的脚本 - 登录 SmartCloud iNotes
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
package demo;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class SeleniumDemo {

public static void main(String[] args) throws InterruptedException {
        // 创建一个 Selenium 实例

Selenium selenium = new DefaultSelenium("localhost", 4444, \
   "*firefox", "https://apps.na.collabserv.com/");

        // 启动 selenium session
        selenium.start();
        // 打开测试网页
        selenium.open("https://apps.lotuslive.com/");
        // 输入用户名,密码
        selenium.type("//input[@id='username']", \
             "autouser01@e3yunmail.mail.lotuslive.com");
selenium.type("//input[@id='password']", "test");
        // 登录
        selenium.click("//input[@id='submit_form']");
        // 等待直到页面出现 Mail 链接
int count = 60;
while(count > 0){
if(selenium.isElementPresent("//a[contains(text(),'Mail')]")){
break;
}else{
Thread.sleep(1000);
count--;
            }
                 
        }
        // 登出
        selenium.click("//a[contains(text(),'Log Out')]");
        // 测试结束后,终止 selenium session
        selenium.stop();
    }
}




构建一个 WebDriver 测试工程WebDriver API 可以通过 Python、Ruby、Java 和 C#访问,支持开发人员使用他们偏爱的编程语言来创建测试。这里我们以 Java 为例。首先需要准备好自己的 Eclipse 环境,并在 selenium 的官方网站下载 Selenium 2 的 Jar 包。
图 2. WebDriver 测试工程清单 2. 使用 WebDriver API 的脚本 - 登录 SmartCoud iNotes
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
package demo;

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 WebDriverDemo {
    public static void main(String[] args) {
        //创建一个 firefox driver 实例
WebDriver driver = new FirefoxDriver();
        //打开测试网址
driver.get("https://apps.na.collabserv.com/");
//定义用户名和密码文本框
        WebElement username=driver.findElement(By.id("username"));
        WebElement password=driver.findElement(By.id("password"));
        //输入用户名和密码
username.sendKeys("autouser01@e3yunmail.mail.lotuslive.com");
        password.sendKeys("test");
        //点击 login 登录
        WebElement login=driver.findElement(By.id("submit_form"));
        login.click();
        //设置页面等待直到出现 Mail 链接
(new WebDriverWait(driver, 500)).until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver dr) {
                return dr.findElement(By.linkText("Mail"));
            }
        });
        //登出
        WebElement logout=driver.findElement(By.linkText("Log Out"));
        logout.click();
        //关闭浏览器
        driver.quit();
    }
}

返回列表