代码改变世界

菜鸟学自动化测试(八)----selenium 2.0环境搭建(基于maven)

2012-02-04 13:11  虫师  阅读(35849)  评论(5编辑  收藏  举报

 

之前我就讲过一种方试来搭建selenium rc 的环境搭建,那个文章是基于selenium RC 1.0的的方式,目前官网已经不提供1.0的下载了,我所提供的下载链接是CSDN上的。

其实,如果大家熟悉maven的话,selenium RC将变成易常简单,当然,我的操作还是基于java语言阵营的。

前提:你要有maven环境。关于maven环境的搭建,请参考本博客的关于maven的文章。左侧导航栏有maven环境搭建的相关文章。

 

第一步:

Cmd打开命令提示符号,创建一个maven项目,命令如下:

mvn archetype:generate -DgroupId=MySel20Proj -DartifactId=MySel20Proj  -Dversion=1.0

 

打开创建的项目,找到项目目录下面的pom.xml文件,打开,在<dependencies>...</dependencies>之前加上如下内容:

 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.16.1</version>
</dependency>

 

我们将junit的版本改为4.2 。修改之后完整的pom.xml代码如下:

 

    <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>MySel20Proj</groupId>
<artifactId>MySel20Proj</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>MySel20Proj</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.16.1</version>
</dependency>
</dependencies>
</project>

 

Ok.!现在在命令提示符号下切换到项目目录下:

 

Djava\MySel20Proj> mvn eclipse:clean 

 

Djava\MySel20Proj> mvn eclipse:eclipse    执行这个需要下载相应的包,时间会久一些。

 

现在把我们构建好的项目导入eclipse中,如果有问题请参考我的maven文章。

 

http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html

 

我们依然用之前的代码进行测试,(这段代码还是基于selenium 1.0selenium RC

 

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class test extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore",
"http://www.baidu.com/");
selenium.start();
}

@Test
public void testTest() throws Exception {
selenium.open("/");
selenium.type("id=kw", "aaaa");
selenium.click("id=su");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

 

当然,你可以用可用selenium IDE录制脚本,将脚本导出成java所类型的语言进行测试,方式参考:
http://www.cnblogs.com/fnng/archive/2011/10/19/2217506.html  

--上面的代码与方式,已经不是推荐使用的方式了。因为selenium 2.0来了,selenium RC的舞台该退场了。

 

 

详解maven方式添加插件

 

为了得到一个不同的selenium Maven工件的概述,请看下图,它显示selenium Maven工件以及最重要的类以及这些类接口之间的依赖关系:

 

如果你想使用不同的WebDriver来运行代码,如:FirefoxDriver 你不必依靠selenium Java 构建(其中有很多的传递依赖),你只需要需要在pom.xml文件中添加相应的插件就可以了。

 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.18.0</version>
</dependency>

然后,在cmd(命令提示符)下,将项目重新构建一下就行了(再次执行mvn eclipse:eclipse 命令)。Maven自动下载我们所需要的FirefoxDriver 相关jar包。

 

 

如果您正在使用DefaultSelenium(或RemoteWebDriver的 实现),你仍然需要启动Selenium Server。最好的办法是下载selenium Server standalone.jar ,只是用它。此外,您还可以嵌入到自己的项目Selenium Server,下面的依赖关系,如果你添加到你的pom.xml:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.18.0</version>
</dependency>

注意:这个selenium Server 产品依赖于servlet-API-2.5 产品。如果你的项目运行在一个web应用程序容器的依赖,你应该排除。

 

 

第三方的浏览器不被支持,请用seleniumhq 开发的插件;

<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>0.10</version>
</dependency>

 

以上参考内容:

 

http://seleniumhq.org/docs/03_webdriver.html

http://seleniumhq.org/download/maven.html



 

什么是Selenium 2.0 

 

从较高的层次看,Selenium由三种工具组成。第一个工具Selenium IDE,是Firefox的扩展插件,支持用户录制和回访测试。录制/回访模式?存在局限性,对许多用户来说并不适合,因此第二个工具——Selenium WebDriver提供了各种语言环境的API来支持更多控制权和编写符合标准软件开发实践的应用程序。最后一个工具——Selenium Grid帮助工程师使用Selenium API控制分布在一系列机器上的浏览器实例,支持并发运行更多测试。在项目内部,它们分别被称为“IDE”、“WebDriver”和“Grid”。 

 

而现在我们大家能看到的:selenium2.0 = selenium1.0+ webdriver 

 

 

什么是WebDriver?

 

WebDriver是一个自动化测试Web应用程序的工具,尤其以验证它们是否如预期运作。它旨在提供一个友好的API,它很容易探讨和理解,更容易比的Selenium-RC(1.0)API,这将有助于使你的测试更容易阅读和维护使用。它不依赖任何特定的测试框架,因此它可以用来在一个单元测试,或从一个普通的旧的“main”的方法同样。 

下一节来学习如何selenium 2.0的新东东 webdriver ! 



Web Page Counters
Computer Desks