Java + Appium + reportng自动生成测试报告
1.代码结构
2.Maven 依赖:
<?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.test4</ groupId>
< artifactId> test4</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< dependencies>
< dependency>
< groupId> com.google.guava</ groupId>
< artifactId> guava</ artifactId>
< version> 18.0</ version>
</ dependency>
< dependency>
< groupId> org.testng</ groupId>
< artifactId> testng</ artifactId>
< version> 7.0.0-beta1</ version>
</ dependency>
< dependency>
< groupId> org.uncommons</ groupId>
< artifactId> reportng</ artifactId>
< version> 1.1.4</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.google.inject</ groupId>
< artifactId> guice</ artifactId>
< version> 4.1.0</ version>
</ dependency>
< dependency>
< groupId> io.appium</ groupId>
< artifactId> java-client</ artifactId>
< version> 4.1.2</ version>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-surefire-plugin</ artifactId>
< version> 2.17</ version>
< configuration>
< suiteXmlFiles>
< suiteXmlFile> src/main/resources/testng.xml</ suiteXmlFile>
</ suiteXmlFiles>
< properties>
< property>
< name> usedefaultlisteners</ name>
< value> false</ value>
</ property>
< property>
< name> listener</ name>
< value> org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</ value>
</ property>
</ properties>
< argLine> -Dfile.encoding=UTF-8</ argLine>
< systemPropertyVariables>
< org.uncommons.reportng.escape-output> false</ org.uncommons.reportng.escape-output>
</ systemPropertyVariables>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>
3.testng.xml文件:配置日志生成规则
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
< suite name = " My suite" parallel = " tests" thread-count = " 4" >
< listeners>
< listener class-name = " org.uncommons.reportng.HTMLReporter" />
< listener class-name = " org.uncommons.reportng.JUnitXMLReporter" />
</ listeners>
< test verbose = " 2" name = " MyTest" >
< classes>
< class name = " com.test.Test1" />
</ classes>
</ test>
</ suite>
4.Utils文件:用于操作截图文件
package com. common;
import org. apache. commons. io. FileUtils;
import java. io. File;
import java. io. IOException;
public class Utils {
public static void copyFile ( String file, String dest) {
try {
File from = new File ( file) ;
File to = new File ( dest) ;
String toDir = to. getParent ( ) ;
mkdirs ( toDir) ;
FileUtils. copyFile ( from, to) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static void copyFile ( File file, File dest) {
String dir = dest. getParent ( ) ;
mkdirs ( dir) ;
try {
FileUtils. copyFile ( file, dest) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static void mkdirs ( String path) {
File dest = new File ( path) ;
if ( ! dest. exists ( ) && ! dest. isDirectory ( ) ) {
dest. mkdirs ( ) ;
}
}
}
5.测试类Test1
package com. test;
import com. common. Utils;
import com. google. gson. JsonObject;
import io. appium. java_client. android. AndroidDriver;
import org. openqa. selenium. By;
import org. openqa. selenium. OutputType;
import org. openqa. selenium. remote. DesiredCapabilities;
import java. io. File;
import java. net. MalformedURLException;
import java. net. URL;
import java. util. concurrent. TimeUnit;
import java. util. Date;
import org. testng. Reporter;
import org. testng. annotations. Test;
public class Test1 {
@Test ( description = "${caseName}" )
public void test ( ) throws MalformedURLException {
AndroidDriver driver = null;
try {
DesiredCapabilities des = new DesiredCapabilities ( ) ;
des. setCapability ( "platformName" , "Android" ) ;
des. setCapability ( "platformVersion" , "7.1.1" ) ;
des. setCapability ( "udid" , "YLZDIB5SYTQSKVIZ" ) ;
des. setCapability ( "deviceName" , "test" ) ;
des. setCapability ( "appPackage" , "com.android.contacts" ) ;
des. setCapability ( "appActivity" , "com.android.contacts.PeopleActivityAlias" ) ;
des. setCapability ( "unicodeKeyboard" , "True" ) ;
des. setCapability ( "resetKeyboard" , "True" ) ;
des. setCapability ( "autoGrantPermissions" , "True" ) ;
des. setCapability ( "noReset" , "True" ) ;
driver = new AndroidDriver ( new URL ( "http://127.0.0.1:4723/wd/hub" ) , des) ;
JsonObject settings = driver. getSettings ( ) ;
System. out. println ( "设备信息值" + settings) ;
Reporter. log ( new Date ( ) + " 【信息】 环境准备完成<p/>" ) ;
Reporter. log ( new Date ( ) + " 【信息】 测试开始<p/>" ) ;
driver. manage ( ) . timeouts ( ) . implicitlyWait ( 10 , TimeUnit. SECONDS) ;
driver. findElement ( By. xpath ( "//android.view.View[@content-desc=\"拨号\"]" ) ) . click ( ) ;
driver. findElement ( By. id ( "com.android.contacts:id/one" ) ) . click ( ) ;
driver. findElement ( By. id ( "com.android.contacts:id/seven" ) ) . click ( ) ;
driver. findElement ( By. id ( "com.android.contacts:id/three" ) ) . click ( ) ;
driver. findElement ( By. id ( "com.android.contacts:id/show_dialpad_btn" ) ) . click ( ) ;
Reporter. log ( new Date ( ) + " 【信息】 测试完成" ) ;
screenshots ( driver) ;
} catch ( Exception e) {
Reporter. log ( new Date ( ) + " 【错误】 测试错误,具体详情请" ) ;
screenshots ( driver) ;
} finally {
if ( null!= driver) {
driver. quit ( ) ;
}
}
}
public void screenshots ( AndroidDriver driver) {
File file = driver. getScreenshotAs ( OutputType. FILE) ;
File screenFile= new File ( "F:\\screenFile.png" ) ;
Utils. copyFile ( file, screenFile) ;
String s = "F:\\\\screenFile.png" ;
Reporter. log ( "<a href=\"" + s + "\" target=_blank>点击查看截图</a><p/>" , true ) ;
}
}
6.执行
maven clean
maven test
执行生成后的文件放在:工程路径\target\surefire-reports\html,可直接点开index.html查看
7.报错解决
该代码笔者已执行过多次且可使用,如遇到报错问题,从下面几方面查找问题所在:
Appium需要开启,并校验 本地/远程 IP、端口是否匹配。
仅支持安卓手机。
查看手机 udid 和 系统版本是否对应。
查看手机App 应用包 和 启动页 是否存在。
截图有F盘的路径,电脑是否存在F盘,否则修改路径。
8.信息获取
获取手机序列号udid:adb devices
获取手机启动APP的应用包和启动页:(com.android.contacts、 com.android.contacts.PeopleActivityAlias) 1.寻找包名 adb shell pm list package -f 2.根据包名寻找启动页
adb shell
dumpsys package 包名
* Appium+Java 手机自动化测试功能整理
Appium+Java 手机自动化测试