<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.0modelVersion>
<groupId>com.test4groupId>
<artifactId>test4artifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<version>18.0version>
dependency>
<dependency>
<groupId>org.testnggroupId>
<artifactId>testngartifactId>
<version>7.0.0-beta1version>
dependency>
<dependency>
<groupId>org.uncommonsgroupId>
<artifactId>reportngartifactId>
<version>1.1.4version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.google.injectgroupId>
<artifactId>guiceartifactId>
<version>4.1.0version>
dependency>
<dependency>
<groupId>io.appiumgroupId>
<artifactId>java-clientartifactId>
<version>4.1.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.17version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/testng.xmlsuiteXmlFile>
suiteXmlFiles>
<properties>
<property>
<name>usedefaultlistenersname>
<value>falsevalue>
property>
<property>
<name>listenername>
<value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReportervalue>
property>
properties>
<argLine>-Dfile.encoding=UTF-8argLine>
<systemPropertyVariables>
<org.uncommons.reportng.escape-output>falseorg.uncommons.reportng.escape-output>
systemPropertyVariables>
configuration>
plugin>
plugins>
build>
project>
<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>
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();
}
}
}
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");//adb devices获取
des.setCapability("deviceName", "test");//随便起
//应用信息:
des.setCapability("appPackage", "com.android.contacts");//App安装后的包名
des.setCapability("appActivity", "com.android.contacts.PeopleActivityAlias");//activity启动页
//其他:
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);//手机联通的电脑IP地址、端口在appium上获取
JsonObject settings = driver.getSettings();
System.out.println("设备信息值" + settings);
Reporter.log(new Date() + " 【信息】 环境准备完成");
Reporter.log(new Date() + " 【信息】 测试开始");
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(" + s + "\" target=_blank>点击查看截图", true);
}
}
该代码笔者已执行过多次且可使用,如遇到报错问题,从下面几方面查找问题所在:
Appium+Java 手机自动化测试