更新时间:2022-04-17 03:10:49
之前我就讲过一种方试来搭建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.!现在在命令提示符号下切换到项目目录下:
D:java\MySel20Proj> mvn eclipse:clean
D:java\MySel20Proj> mvn eclipse:eclipse 执行这个需要下载相应的包,时间会久一些。
现在把我们构建好的项目导入eclipse中,如果有问题请参考我的maven文章。
http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html
我们依然用之前的代码进行测试,(这段代码还是基于selenium 1.0的selenium 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>