基于 Appium + Junit5 + Allure + Maven 进行 APP UI 自动化测试-环境准备

最近完成一项基于Appium + Junit5 + Allure + Maven进行Android APP UI自动化测试的项目。我会分不同的方面来整理所学和所遇到的问题。这里首先介绍测试环境的准备。

测试环境准备

Appium生态工具
  • adb:Android的控制工具,用于获取Android的各种数据和控制

    1. Android developer下载Android Command line tools,获得工具sdkmanager,并添加环境变量D:\\AndroidSdk\\tools\\bin
    2. 使用sdkmanager下载platform-tools(sdkmanager "platform-tools" "platforms;android-29")、build-tools(sdkmanager "build-tools;29.0.2"),并添加环境变量:
      D:\\AndroidSdk\\platform-tools;
      D:\\AndroidSdk\\build-tools\\;
    
  • Appium Server:Appium的核心工具,命令行工具

    1. 官网下载安装NodeJS
    2. 淘宝镜像安装appium:
      npm install-gcnpm--registry=https://registry.npm.taobao.org
      cnpm install -g appium
    
  • Appium Desktop:内嵌了Appium server和inspector工具的综合工具
    Github下载安装appium desktop

  • Appium Client:各种语言的客户端封装库,用于连接Appium Server

    添加如下配置到pom.xml文件

  <repositories>
      <repository>
          <id>jitpack.io</id>
          <url>https://jitpack.io</url>
      </repository>
  </repositories>

  <dependencies>
      <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
      <dependency>
          <groupId>io.appium</groupId>
          <artifactId>java-client</artifactId>
          <version>${appium-java-client.version}</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
Junit5

Junit5 由三个不同子项目中的3个模块组成:JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage。添加如下配置到pom.xml文件:

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>${junit.jupiter.version}</version>
  <scope>test</scope>
</dependency>
Allure Framework

Allure 是开源的测试报告框架,支持多种主流语言,生成适合团队所有人查看的测试报告。添加如下配置到pom.xml文件:

<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-junit5</artifactId>
    <version>${allure-junit5.version}</version>
    <scope>test</scope>
</dependency>
其他工具及其pom.xml配置
  • hamcrest 用于测试用例中的断言

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>${hamcrest-core.version}</version>
        <scope>test</scope>
    </dependency>
    
  • jackson 用于测试用例中读取YAML文件

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>RELEASE</version>
    </dependency>
        
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>RELEASE</version>
    </dependency>
    
- **[XPath](https://www.w3school.com.cn/xpath/index.asp)**


### Maven的pom.xml文件
Maven2017.3之后的版本支持Junit5,请注意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.erp.www</groupId>
    <artifactId>xiangmuname</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>

        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <aspectj.version>1.8.10</aspectj.version>
        <allure-junit5.version>2.13.1</allure-junit5.version>
        <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <allure-maven.version>2.10.0</allure-maven.version>
        <appium-java-client.version>7.3.0</appium-java-client.version>
        <jackson-dataformat-yaml.version>2.10.1</jackson-dataformat-yaml.version>
        <hamcrest-core.version>2.2</hamcrest-core.version>

        <encoding>UTF-8</encoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.encoding>UTF-8</project.encoding>
        <project.build.sourceEncoding>${project.encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.encoding}</project.reporting.outputEncoding>

        <java.version>1.8</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>

    </properties>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>${appium-java-client.version}</version>
            <scope>test</scope>
        </dependency>


        <!--JUNIT 5 Dependencies-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>${hamcrest-core.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-dataformat-yaml.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson-dataformat-yaml.version}</version>
            <scope>test</scope>
        </dependency>

        <!--Allure Reporting Dependencies-->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>${allure-junit5.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!-- Need at least 2.22.0 to support JUnit 5 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>io.qameta.allure.junit5.AllureJunit5</value>
                        </property>
                    </properties>
                    <includes>
                        <include>**/*Test*.java</include>
                    </includes>
                    <testFailureIgnore>false</testFailureIgnore>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <systemProperties>
                        <property>
                            <name>junit.jupiter.extensions.autodetection.enabled</name>
                            <value>true</value>
                        </property>
                        <property>
                            <name>allure.results.directory</name>
                            <value>${project.basedir}/target/allure-results</value>
                        </property>
                    </systemProperties>
                    <reportFormat>plain</reportFormat>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>${allure-maven.version}</version>
                <configuration>
                    <reportVersion>2.8.1</reportVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>