centos7下Maven Java selenium3環境搭建

centos7下Maven Java selenium3環境搭建

一.Jdk安裝

我這裏用的是open-jdk。java

[adawang@localhost src]$ sudo yum search openjdk
...
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment with full debug on
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment with full debug on
...
[adawang@localhost src]$ sudo yum install java-1.8.0-openjdk-devel.x86_64

 

二.Manven安裝

Maven 能夠經過一小段描述信息來管理項目的構建,報告和文檔的優秀的項目構建工具。linux

打開http://mirrors.hust.edu.cn/apache/maven/maven-3/網站查看最新版本,目前最新版本是3.6.0。chrome

 

[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.0-bin.tar.gz -C /usr/local/  #解壓到/usr/local目錄

 

設置環境變量

方法一:apache

輸入如下內容後shift + zz保存退出vim

[adawang@localhost src]$ vim /etc/profile.d/maven.sh

#!/bin/bash
export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export PATH=$PATH:$MAVEN_HOME/bin

[adawang@localhost src]$ chmod 744 /etc/profile.d/maven.sh

[adawang@localhost src]$ source /etc/profile.d/maven.sh(加載腳本到當前環境下, source 能夠用 . 代替) 

方法二:centos

[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.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/maven3
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.2.3.el7.x86_64", arch: "amd64", family: "unix"

 若是沒有mvn這個命令說明環境變量沒有配置好。bash

 

三. Maven配置和使用

1.快速生成項目骨架

[adawang@localhost src]$ mvn archetype:generate -DgroupId=com.demo.maventest -DartifactId=seleniumtest

首次運行時,mvn會從遠程"中央倉庫"下載一些必需的文件到"本地倉庫" jvm

 

一路默認回車maven

等待出現 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.5</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.0:clean (default-clean) @ helloworld ---
[INFO] Deleting /home/adawang/java/helloworld/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2: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.0:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/adawang/java/helloworld/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.594 s
[INFO] Finished at: 2018-11-12T13:12:39+08:00
[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.0:clean (default-clean) @ helloworld ---
[INFO] Deleting /home/adawang/java/helloworld/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2: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.0:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/adawang/java/helloworld/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2: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.0:testCompile (default-testCompile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/adawang/java/helloworld/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ helloworld ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mavendemo.maventest.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 s - in com.mavendemo.maventest.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.253 s
[INFO] Finished at: 2018-11-12T13:21:44+08:00
[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.1:exec (default-cli) @ helloworld ---
Hello World!
Starting ChromeDriver 2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf) on port 19039
Only local connections are allowed.
十一月 12, 2018 1:26:05 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
百度一下,你就知道
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.912 s
[INFO] Finished at: 2018-11-12T13:26:06+08:00
[INFO] ------------------------------------------------------------------------
相關文章
相關標籤/搜索