亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

centos7下Maven Java selenium3環境搭建

標簽:
Java

一.Jdk安装

我这里用的是open-jdk。

复制代码

[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 可以通过一小段描述信息来管理项目的构建,报告和文档的优秀的项目构建工具。

打开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.0-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.0export 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 可以用 . 代替)

复制代码

方法二:

复制代码

[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-8OS 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.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 19039Only 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] ------------------------------------------------------------------------

复制代码

 

原文出处:https://www.cnblogs.com/jiang-cheng/p/9946066.html  

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消