小编本文主要是关于Java自动化环境的配置搭建与大家进行分享。
       本篇内容包含(基于上篇的基础上根据不同端汇总环境配置):单元测试(JUnit5) + 接口自动化(RestAssured) + UI自动化(Selenium) + 测试报告(Allure)。
 

前置必备软件:

1、JDK1.8(必须8,自动化框架兼容最稳定)

2、IDEA2020+(任意专业版/社区版都可)

3、Maven 3.8.x / 3.9.x(不要用3.6及以下的)

4、Chrome浏览器(最新即可,Selenium4自动适配驱动)

5、Allure 2.23.1(和pom版本保持一致,用于生成可视化报告)

本地环境配置就翻一下小编上一篇笔记……(^_^)v


一、创建Maven自动化项目。

1、IDEA——New Project——选Maven——不勾选骨架——Next

2、填写:

Groupil:com.auto

Artifactid: java-auto-test

Version: 1.0.0

3、选择项目存放路径——Finish;

4、等待项目初始化完成,自动生成src目录;

5、删除自动生成的多余App类,干净项目结构。

Maven 核心依赖(直接复制pom.xml)
java项目中最常见的Maven 核心依赖,一般普通java项目中不会直接引这些,Maven插件/工具开发才会用到。

xml
  
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.auto</groupId>
    <artifactId>java-auto-test</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <junit.version>5.9.2</junit.version>
        <restassured.version>5.3.0</restassured.version>
        <selenium.version>4.15.0</selenium.version>
        <allure.version>2.23.1</allure.version>
    </properties>

    <dependencies>
        <!-- JUnit5 单元测试 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- 接口自动化 RestAssured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>${restassured.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>${restassured.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Web UI 自动化 Selenium -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- 日志、JSON处理 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.36</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 测试插件 + Allure报告 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <argLine>
                        -javaagent:${settings.localRepository}/io/qameta/allure/allure-java-common/${allure.version}/allure-java-common-${allure.version}.jar
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

操作:粘贴后点击IDEA右侧Maven——刷新,等待所有以来下载完成。


二、标准项目目录结构(直接照着建或者可以按照自己习惯创建都可以)。
 

plaintext
  
java-auto-test
└── src
    └── test
        └── java
            ├── unit        // 单元测试用例
            ├── api         // 接口自动化用例
            ├── web         // Web UI自动化用例
            ├── common      // 公共工具类
            │   ├── BaseTest.java
            │   ├── HttpUtil.java
            │   └── WebDriverUtil.java
            └── pojo        // 实体类、请求/响应实体
 


 
三、可直接运行 三大demo示例。


1、单元测试 unit/DemoTest.java

java
  
package unit;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DemoTest {

    @Test
    void testDemo() {
        int a = 10;
        int b = 20;
        assertEquals(30, a + b);
    }
}


2、接口自动化   api/ApiDemoTest.java
 

Java
  
package api;

import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class ApiDemoTest {

    @Test
    void testGetDemo() {
        given()
                .baseUri("https://httpbin.org")
        .when()
                .get("/get")
        .then()
                .statusCode(200)
                .body("url", equalTo("https://httpbin.org/get"));
    }
}


3、UI 测试  web / webDemoTest.java

java
  
package web;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDemoTest {
    private static WebDriver driver;

    @BeforeAll
    static void initDriver(){
        // 新版selenium自动适配浏览器驱动,无需手动下载
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    void openBaidu(){
        driver.get("https://www.baidu.com");
        System.out.println("当前页面标题:" + driver.getTitle());
    }

    @AfterAll
    static void closeDriver(){
        if(driver != null){
            driver.quit();
        }
    }
}
 


 
四、运行方式&Allure报告生成。


1、单条用例运行

IDEA 直接右键单个Test类 → Run 执行
2、Maven命令行执行全部用例:
Terminal输入:

bash
  
mvn test

3、Allure生成并打开报告

3.1、执行完mvn test 后,项目生成

3.2、命令行生成报告并自动打开:

allure generate allure-results-o allure-report --clean
allure open allure-report

五、常见报错及解决方案

1、Maven依旧爆红

刷新Maven——检查settings.xml阿里云镜像——File-invalidate Caches清理缓存重启IDWA

2、Chrome浏览器启动失败

Selenium4无需手动下载驱动,自动匹配

升级Chrome到最新版本

3、JUnit5注解不识别

确认pom已引入junit-jupiter-api / engine

模块SDK选为JDK8

4、Allure命令不存在

检查Allure是否配置环境变量Path

重启CMD/DEA再试

说在文尾:

如果你感觉小班的分享有帮助到你。

嘿嘿(搓手)关注下小编呗......希望在技术分享的路上同学们相互支持。
 

更多推荐