安装与配置
环境配置
配置环境变量
将 Maven 下载到本地解压后,设置环境变量
"我的电脑"右键菜单->属性->高级->环境变量->系统变量->新建
变量名:MAVEN_HOME
变量值:D:\Service\apache-maven-3.3.9
找到"Path"变量名->"编辑"添加如下:
变量名:Path
变量值:%MAVEN_HOME%\bin;
添加好了以后,打开"cmd"运行"mvn -version"来测试Maven是否配置正确
C:\Users\YukiOne>mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: D:\Service\apache-maven-3.3.9\bin\..
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: D:\Program\Java\jdk1.7\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"
配置Maven仓库
修改 Maven 仓库的路径。 打开...\apache-maven-3.3.3\conf\settings.xml 文件, 大概在49行到57行之间的位置做如下修改:
······
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/Code/Java/TestMaven/Warehouse</localRepository>
······
创建Maven项目
配置好了仓库,我们就可以创建项目了,创建项目之前先了解下各个参数都是什么意思
- generate --- 用于创建Maven项目
- DgroupId --- 指定包的名称
- DartifactId --- 指定项目名称
- Dversion --- 指定版本号
打开"cmd",在指定的目录下输入以下配置构建Maven项目:
C:\Users\YukiOne>d:
D:\>cd D:\Code\Java\TestMaven\Project
D:\Code\Java\TestMaven\Project>mvn archetype:generate -DgroupId=com.jase.test -DartifactId=MyWebDriver -Dversion=1.0
【注意】:每个参数之间都要有空格,而且参数对大小写非常敏感
由于第一次配置项目可以需要很多的依赖包,所以要下载一会,需要一点时间,配置好了以后,会发现刚刚进入的文件夹下多了一个目录
D:\Code\Java\TestMaven\Project>dir
驱动器 D 中的卷是 Speed
卷的***是 9A8E-3352
D:\Code\Java\TestMaven\Project 的目录
2016/02/26 10:37 <DIR> .
2016/02/26 10:37 <DIR> ..
2016/02/26 10:37 <DIR> MyWebDriver
0 个文件 0 字节
3 个目录 32,927,522,816 可用字节
出现了目录但是当前不能直接导入到Eclipse中,还需要运行下面的命令才可以:
D:\Code\Java\TestMaven\Project\MyWebDriver>cd MyWebDriver
D:\Code\Java\TestMaven\Project\MyWebDriver>mvn clean compile
D:\Code\Java\TestMaven\Project\MyWebDriver>mvn eclipse:eclipse
配置完了以后,就可以导入Eclipse了
将Maven项目导入Eclipse中
1. 打开Eclipse
2. 导入项目,选择File->Import->Existing Projects into Workspace->点击"Next",添加刚才构建的"MyWebDriver"目录
3. 修改Maven配置文件路径,选择Window-->Perferences-->Maven-->User Settings
配置好了Eclipse,Maven基本都已经成型了。剩下的就是怎么用了。
包的管理与更新
Maven包的管理都是在pom.xml中配置的,可以访问Maven网络上的仓库,然后当你启动Eclipse的时候,Maven它就会从网络仓库上,把对应的包给下下来。
Maven网络仓库地址:http://search.maven.org/
在搜索框中,搜索"Selenium",就可以出现"Selenium"的包路径,包名以及对应的版本号
将对应包的信息记录以后,就可以在pom.xml中配置了,配置如下(PS: 下面配置了一些jar包,其中testNG用来管理用例,reportNG用来生成报告,Selenium用来执行测试用例,还有):
【注意】: Maven网络仓库中reportNG的包有问题,不支持中文,生成后的报告里面含有中文的都是乱码,有大神修改过这个包的源码,我们需要将这个包替换一下
reportNG支持中文的jar包链接: http://pan.baidu.com/s/1nunT2mH 密码: w5nv
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jase.test</groupId>
<artifactId>MyWebDriver</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>MyWebDriver</name>
<url>http://maven.apache.org</url>
<!-- maven 参数配置,这里引用不同的testng.xml -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xmlFileName>testng.xml</xmlFileName>
</properties>
<!-- maven 引用依赖不同的jar -->
<dependencies>
<!-- 依赖testNg -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<!-- 依赖reportNg 关联testNg -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 依赖Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<!-- 依赖Selenium驱动包 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.52.0</version>
<scope>compile</scope>
<!--
scope标签中对应值的解释:
* compile,缺省值,适用于所有阶段,会随着项目一起发布。
* provided,类似 compile,期望 JDK、容器或使用者会提供这个依赖。如 servlet.jar。
* runtime,只在运行时使用,如 JDBC 驱动,适用运行和测试阶段。
* test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
* system,类似 provided,需要显式提供包含依赖的 jar, Maven 不会在 Repository 中查找它。
-->
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加插件 关联testNg.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>res/${xmlFileName}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
<!-- <forkMode>always</forkMode> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
【注意:】上面配置如果报错,需要在项目下新建一个目录"res/testng.xml",他会去这个目录读取指定的XML
testng.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="com.jase.test.BaiduTest" />
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
运行Maven配置后,可以看到Selenium自动执行了,并且在指定的目录下""看到reportNG生成的报告