上一篇appium運行環境搭建完後appium運行環境搭建,我們本地已經安裝了appium server服務了,打開cmd,輸入appium即可啓動appium。本篇將繼續appium自動化測試探索與學習。
- 新建maven項目
- 引入TestNg
新建完後,打開項目根目錄的pom.xml文件,添加TestNg依賴
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta1</version>
</dependency>
- 引入appium依賴
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${java-client.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java.version}</version>
</dependency>
版本號可以去maven倉庫去找最新的
- 引入測試報告分析模板Allure報告
allure是一款很好用的測試報告分析插件,界面如上所示,很美觀,並且有多種菜單從多個維度去生成測試報告,官方文檔鏈接:allure報告官方文檔
maven中需要添加如下依賴:
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-testng-adaptor</artifactId>
<version>${allure-testng-adaptor.version}</version>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>${allure-testng.version}</version>
</dependency>
<plugins>
<!-- 添加插件,添加插件, 關聯testNg.xml,添加ReportNg的監聽器,修改最後的TestNg的報告 -->
<plugin>
<!--這樣就可以實現Maven運行test命令時按照TestNG的testng.xml配置運行相應的測試用例,運行mvn test指令即可-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.1/aspectjweaver-1.9.1.jar"
</argLine>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
</properties>
<!-- 使allure-results在target文件夾下(很重要) -->
<workingDirectory>target/</workingDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.testng.TestNG</mainClass>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<id>package_shade_profile</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
- 新建testng.xml文件,配置TestNg測試入口
注意,xml的文件名必須是testng.xml,不然會識別不到,如果測試想要分模塊,那麼再重新新建一個xml文件,類似於這樣的結構
然後,testng.xml中再引入想要添加到測試組中的模塊
testng.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="測試套匯總">
<parameter name="XXX" value="XXX" />
<suite-files>
<!--<suite-file path="testng_OTT.xml"></suite-file>-->
<suite-file path="testng_Setting.xml"></suite-file>
</suite-files>
</suite>
接下來,寫出測試case,再執行maven test命令,即可運行測試套,執行完後再執行allure serve target/allure-results 即可生成allure報告。