Java自动化测试系列[v1.0.0][Maven开发环境]

Java自动化测试环境

Selenium+Maven+Idea+TestNG+Reportng

Maven

用MAVEN建立项目的好处:

  • 项目非常大时,可借助Maven将一个项目拆分成多个工程,最好是一个模块对应一个工程,利于分工协作。而且模块之间还是可以发送消息的。
  • 借助Maven,可将jar包仅仅保存在“仓库”中,有需要该文件时,就引用该文件接口,不需要复制文件过来占用空间。
  • 借助Maven可以以规范的方式下载jar包,因为所有的知名框架或第三方工具的jar包已经按照统一的规范存放到了Maven的中央仓库中。
  • Maven会自动将你要加入到项目中的jar包导入,不仅导入,而且还会将该jar包所依赖的jar包都自动导入进来。

下载与配置

官方地址:http://maven.apache.org/download.cgi下载安装后配置MVN环境变量,如下图所示
在这里插入图片描述
变量名可用M2_HOME或者MAVEN_HOME,变量值就是安装目录,如果JDK配的很遛,这个同理
在这里插入图片描述
PATH:%M2_HOME%\bin;

检查Maven环境

在这里插入图片描述

配置仓库

配置MVN本地仓库物理地址,也就是maven会从中央仓库下载需要的jar包到本地,根据自己的安装,找到如下路径
在这里插入图片描述
再此路径下打开setting.xml,配置项如下,注意XML节点,配置成自己合适存放所下载jar包的路径即可
在这里插入图片描述
配置MVN中央仓库:将如下配置内容添加到镜像配置中

        <mirror>  
            <id>alimaven</id>  
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>  
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
        </mirror>

在这里插入图片描述
保存配置后,将该文件复制如下路径下
在这里插入图片描述
此处单配置了一个镜像地址,是阿里云的地址,官方地址下载太慢可以通过这个位置的配置代替
如果没有.m2路径,启动命令行执行命令mvn help:system来生成

配置IDEA

IDEA官方下载地址:https://www.jetbrains.com/idea/download/,默认安装即可,打开IDEA,新建一个maven项目,检查Maven的配置如图所示,箭头所指两个配置已经更新到了前边步骤中的位置
在这里插入图片描述
DEA会在右下角(注意右下角)提示你是否需要自动安装pom配置的jar包,一旦出现,就要enable它

配置pom

找到pom.xml文件,打开它,配置项目所需的依赖包,如下图配置

<dependencies>
        <!--- 导入webdriver相关   -->
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.0.0</version>
        </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
<build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                    <configuration>
                        <properties>
                            <property>
                                <name>usedefaultlisteners</name>
                                <value>false</value>
                            </property>
                            <property>
                                <!--使用reportng的 listener 生成测试报告-->
                                <name>listener</name>
                                <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
                            </property>
                        </properties>
                        <testFailureIgnore>true</testFailureIgnore>
                        <!--指定testng.xml的位置-->
                        <suiteXmlFiles>
                            <file>testng.xml</file>
                        </suiteXmlFiles>
                        <workingDirectory>target/</workingDirectory>
                        <forkMode>always</forkMode>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

到此Java环境完成Pom中配置的依赖可以在MVN的中央库http://mvnrepository.com/, 检索到你想要的包,配置进去即可

发布了214 篇原创文章 · 获赞 153 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/105539795
今日推荐