安装与配置
环境配置
配置环境变量
将 Maven 下载到本地解压后,设置环境变量css
"个人电脑"右键菜单->属性->高级->环境变量->系统变量->新建html
变量名:MAVEN_HOME
变量值:D:\Service\apache-maven-3.3.9
找到"Path"变量名->"编辑"添加以下:java
变量名:Path
变量值:%MAVEN_HOME%\bin;
添加好了之后,打开"cmd"运行"mvn -version"来测试Maven是否配置正确python
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行之间的位置作以下修改:git
······
<!-- 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项目
配置好了仓库,咱们就能够建立项目了,建立项目以前先了解下各个参数都是什么意思github
- generate --- 用于建立Maven项目
- DgroupId --- 指定包的名称
- DartifactId --- 指定项目名称
- Dversion --- 指定版本号
打开"cmd",在指定的目录下输入如下配置构建Maven项目:web
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
【注意】:每一个参数之间都要有空格,并且参数对大小写很是敏感apache
因为第一次配置项目能够须要不少的依赖包,因此要下载一会,须要一点时间,配置好了之后,会发现刚刚进入的文件夹下多了一个目录json
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中,还须要运行下面的命令才能够:windows
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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xmlFileName>testng.xml</xmlFileName>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<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>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.52.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<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>
</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>
</suite>
运行Maven配置后,能够看到Selenium自动执行了,而且在指定的目录下""看到reportNG生成的报告



