centos7下Maven Java selenium3环境搭建
一.Jdk安装
我这里用的是open-jdk。
[adawang@localhost src]$ sudo yum search openjdk
...
java-1.8.-openjdk-devel.i686 : OpenJDK Development Environment
java-1.8.-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.-openjdk-devel-debug.i686 : OpenJDK Development Environment with full debug on
java-1.8.-openjdk-devel-debug.x86_64 : OpenJDK Development Environment with full debug on
...
[adawang@localhost src]$ sudo yum install java-1.8.-openjdk-devel.x86_64
二.Manven安装
Maven 可以通过一小段描述信息来管理项目的构建,报告和文档的优秀的项目构建工具。
打开http://mirrors.hust.edu.cn/apache/maven/maven-3/网站查看最新版本,目前最新版本是3.6.0。
[adawang@localhost src]$ wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz [adawang@localhost src]$ tar zxf ./apache-maven-3.6.-bin.tar.gz -C /usr/local/ #解压到/usr/local目录
设置环境变量
方法一:
输入以下内容后shift + zz保存退出
[adawang@localhost src]$ vim /etc/profile.d/maven.sh #!/bin/bash
export MAVEN_HOME=/usr/local/apache-maven-3.6.
export PATH=$PATH:$MAVEN_HOME/bin [adawang@localhost src]$ chmod /etc/profile.d/maven.sh [adawang@localhost src]$ source /etc/profile.d/maven.sh(加载脚本到当前环境下, source 可以用 . 代替)
方法二:
[adawang@localhost src]$ echo 'export MAVEN_HOME=/usr/local/apache-maven-3.6.0' >> /etc/profile
[adawang@localhost src]$ echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> /etc/profile
[adawang@localhost src]$ source /etc/profile
[adawang@localhost src]$ mvn -v (检查是否成功)
Apache Maven 3.6. (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; --25T02::+:)
Maven home: /usr/local/maven3
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.-openjdk-1.8.0.191.b12-.el7_5.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-
OS name: "linux", version: "3.10.0-862.2.3.el7.x86_64", arch: "amd64", family: "unix"
如果没有mvn这个命令说明环境变量没有配置好。
三. Maven配置和使用
1.快速生成项目骨架
[adawang@localhost src]$ mvn archetype:generate -DgroupId=com.demo.maventest -DartifactId=seleniumtest
首次运行时,mvn会从远程"*仓库"下载一些必需的文件到"本地仓库"
一路默认回车
等待出现 BULD SUCCESS表示创建成功
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
2.maven项目的目录结构
[adawang@localhost java]$ cd seleniumtest [adawang@localhost seleniumtest]$ tree
.
├── pom.xml
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── demo
│ │ └── seleniumtest
│ │ └── App.java
│ └── test
│ └── java
│ └── com
│ └── demo
│ └── seleniumtest
│ └── AppTest.java
└── target
├── classes
│ └── com
│ └── demo
│ └── maventest
│ └── App.class
maven生成项目的目录结构,src/main/java约定用于存放源代码,src/test用于存放单元测试代码,target用于存放编译 打包后的输出文件。
3.在Maven 中配置selenium3
vim修改项目根目录的pom.xml文件
[adawang@localhost seleniumtest]$ vim pom.xml
在<dependencies></dependencies>中间添加以下代码
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.</version>
</dependency>
4.编译项目 mvn clean compile
注意:
mvn命令需要在项目根目录下进行
编译后会自动在target目录中生成class文件
[adawang@localhost seleniumtest]$ mvn clean compile [INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.mavendemo.maventest:helloworld >-----------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.:clean (default-clean) @ helloworld ---
[INFO] Deleting /home/adawang/java/helloworld/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/adawang/java/helloworld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to /home/adawang/java/helloworld/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.594 s
[INFO] Finished at: --12T13::+:
[INFO] ------------------------------------------------------------------------
5.进行单元测试 mvn clean test
[adawang@localhost helloworld]$ mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.mavendemo.maventest:helloworld >-----------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.:clean (default-clean) @ helloworld ---
[INFO] Deleting /home/adawang/java/helloworld/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/adawang/java/helloworld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to /home/adawang/java/helloworld/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.:testResources (default-testResources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/adawang/java/helloworld/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.:testCompile (default-testCompile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source file to /home/adawang/java/helloworld/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20.:test (default-test) @ helloworld ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mavendemo.maventest.AppTest
[INFO] Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 0.013 s - in com.mavendemo.maventest.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: , Failures: , Errors: , Skipped:
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.253 s
[INFO] Finished at: --12T13::+:
[INFO] ------------------------------------------------------------------------
6.编写selenium测试代码并运行
package com.demo.seleniumtest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
System.out.println(driver.getTitle());
driver.navigate().refresh();
driver.close();
}
}
先用记事本打开项目根目录下的pom.xml文件, 在<plugins></plugins>中添加以下内容:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath>
</classpath>
<argument>com.demo.seleniumtest.App</argument>
</arguments>
</configuration>
</plugin>
输入mvn exec:exec运行项目
[adawang@localhost helloworld]$ mvn exec:exec
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.mavendemo.maventest:helloworld >-----------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.2.:exec (default-cli) @ helloworld ---
Hello World!
Starting ChromeDriver 2.39. (dba483cee6a5f15e2e2d73df16968ab10b38a2bf) on port
Only local connections are allowed.
十一月 , :: 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
百度一下,你就知道
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.912 s
[INFO] Finished at: --12T13::+:
[INFO] ------------------------------------------------------------------------