需要的jar
<!-- appium -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.android.AndroidKeyMetastate;
public class AppiumTest {
public static AndroidDriver driver; // 驱动对象,大家注意,由于新版的APPIUM对驱动做了改动,所以这里就不能再用以前的WEBDRIVER来实现了;
private static String apkName = "APP_.apk"; // 安卓的安装包,被测软件
private static String platformVersion = "7.1.2";// 安卓模拟器的版本
private static String serverURL = "http://127.0.0.1:4723/wd/hub";// appium测试服务的地址
private static String deviceName = "10.100.22.252:5555";// 指定设备,
private static String bundleId = "com.……";// 对应app的应用包
public static void main(String[] args) throws IOException, InterruptedException {
// 新建一个DesiredCapabilities对象,是importorg.openqa.selenium.remote.DesiredCapabilities;作者个人赶脚是用来收集驱动初始化信息用的;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); // 指定浏览器的名字,我们测试的是APK所以没有浏览器,故而为空;
capabilities.setCapability("platformName", "Android"); // 指定平台,为安卓
capabilities.setCapability("deviceName", deviceName); // 指定设备,
capabilities.setCapability("platformVersion", platformVersion); // 指定版本,
File classpathRoot = new File(System.getProperty("user.dir"));// 获得项目路径
File appDir = new File(classpathRoot, "apps");// 获得APK所在文件夹名称
File app = new File(appDir, apkName);// 获得APK文件
capabilities.setCapability("app", app.getAbsolutePath()); // 指定需要测试的APK,也就是需要安装的APK文件;
capabilities.setCapability("autoLaunch", "false"); // Appium是否需要自动安装和启动应用。
// support Chinese
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
// no need sign
capabilities.setCapability("noSign", "True");
// 核心关键步骤,创建驱动,指定服务地址,和初始化信息;
driver = new AndroidDriver<>(new URL(serverURL), capabilities);
// System.out.println(driver);
// driver.navigate().forward(); // 前进
// driver.navigate().back(); // 后退
// driver.navigate().refresh(); // 刷新
// driver.launchApp();
// driver.closeApp();; // 关闭
// driver.quit();; // 退出
// java 检查应用是否已经安装
// System.out.println(driver.isAppInstalled(bundleId));
// if (driver.isAppInstalled(bundleId)) {
// // 从设备中删除一个应用
// driver.removeApp(bundleId);
// }
// // java 安装应用到设备中去
// driver.installApp(app.getAbsolutePath());
// driver.launchApp();
// Thread.sleep(2000);
// driver.findElementByXPath("//*[contains(@text,'取消')]").click();
// Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'帐号登录')]").click();
List<WebElement> findElementsById = driver.findElementsById("com.miui.home:id/icon_icon");
findElementsById.get(1).click();
// Thread.sleep(2000);
// jietu();
// Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'授权并登录')]").click();
// driver.findElementById("com.ximalaya.ting.android:id/main_btn_sso_authorize").click();
// Thread.sleep(2000);
// jietu();
// Thread.sleep(1000);
// driver.findElementById("com.orion.xiaoya.speakerclient:id/view_guide1").click();
// Thread.sleep(2000);
// jietu();
// Thread.sleep(1000);
// driver.findElementById("com.orion.xiaoya.speakerclient:id/view_guide2").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// String text = driver.findElementByAccessibilityId("com.orion.xiaoya.speakerclient:id/tv_content").getText();
// System.out.println(text);
// Thread.sleep(2000);
// driver.findElementByXPath("//*[contains(@text,'技能')]").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// List<WebElement> findElementsByClassName =
// driver.findElementsByClassName("android.widget.RelativeLayout");
System.out.println(findElementsByClassName.size());
// findElementsByClassName.get(1).click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'立即进入')]").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'添加')]").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByClassName("android.widget.EditText").sendKeys("呵呵哒");
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'下一步')]").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'我说')]").click();
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByClassName("android.widget.EditText").sendKeys("呵呵哒");
// Thread.sleep(2000);
// jietu();
Thread.sleep(1000);
// driver.findElementByXPath("//*[contains(@text,'生成')]").click();
// Thread.sleep(2000);
// jietu();
// Thread.sleep(1000);
// driver.pressKeyCode(AndroidKeyCode.HOME);//物理按键
// driver.findElementByXPath("//android.widget.TextView[contains(@text,'关闭')]").click();;
// driver.findElementByXPath("//android.view.View[contains(@content-desc,'手机快捷登录')]").click();;
// WebElement findElementByXPath =
// driver.findElementByXPath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.view.View[1]/android.widget.EditText[1]");
// System.out.println(findElementByXPath.getText());
// WebElement findElementByXPath =
// driver.findElementByXPath("//*[@text='请输入您的账号']");
//System.out.println(driver.getPageSource());//获取当前页面的源码
// findElementByXPath.sendKeys("17744590115");
// List<WebElement> findElementsByClassName =
// driver.findElementsByClassName("android.widget.EditText");
// driver.findElementByName("请输入您的账号").sendKeys("asd");
// findElementsByClassName.get(0).sendKeys("17744590115");
// Thread.sleep(1000);
// findElementsByClassName.get(1).sendKeys("150283");
// driver.swipe(75, 500, 75, 0, 200);//滑动
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Set contextHandles = driver.getContextHandles();
// System.out.println(contextHandles);
// String context = driver.getContext();
// System.out.println(context);
// WebElement ById =
// driver.findElementById("com.orion.xiaoya.speakerclient:id/other_weather_layout");
// ById.click();
// WebElement ByIds = driver.findElementById("com.miui.home:id/icon_icon");
// ByIds.click();
// WebElement findElement =
// driver.findElement(By.id("com.orion.xiaoya.speakerclient:id/other_weather_layout"));
// findElement.
// driver.get
// WebElement webElement = findElements.get(1);
// swipeToUp(driver, 500,1,1);
// swipeToRight(driver, 500,1,1);
// swipeToLeft(driver, 500,1,1);
// Map appStringMap = driver.getAppStringMap();
// System.out.println(appStringMap);
}
//截图
public static void jietu() {
try {
// 获取screenshot文件
File screenShot = driver.getScreenshotAs(OutputType.FILE);
// 文件copy到指定的文件夹
FileUtils.copyFile(screenShot, new File(System.getProperty("user.dir") + File.separator + "img"
+ File.separator + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".jpg"));
} catch (WebDriverException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 上滑
*
* @param driver
* appium驱动
* @param during
* 这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快~ 一般设定在500~1000
* @param num
* 滑动的次数
* @param spacing
* 滑动的间隔
*/
public static void swipeToUp(AndroidDriver driver, int during, int num, int spacing) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
for (int i = 0; i < num; i++) {
driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
goSleep(spacing);
}
}
/**
* 下拉
*
* @param driver
* appium驱动
* @param during
* 这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快~ 一般设定在500~1000
* @param num
* 滑动的次数
* @param spacing
* 滑动的间隔
*/
public static void swipeToDown(AndroidDriver driver, int during, int num, int spacing) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
System.out.println(width);
System.out.println(height);
for (int i = 0; i < num; i++) {
driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
goSleep(spacing);
}
}
/**
* 向左滑动
*
* @param driver
* appium驱动
* @param during
* 这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快~ 一般设定在500~1000
* @param num
* 滑动的次数
* @param spacing
* 滑动的间隔
*/
public static void swipeToLeft(AndroidDriver driver, int during, int num, int spacing) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
for (int i = 0; i < num; i++) {
driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
goSleep(spacing);
}
}
/**
* 向右滑动
*
* @param driver
* appium驱动
* @param during
* 这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快~ 一般设定在500~1000
* @param num
* 滑动的次数
* @param spacing
* 滑动的间隔
*/
public static void swipeToRight(AndroidDriver driver, int during, int num, int spacing) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
for (int i = 0; i < num; i++) {
driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
goSleep(spacing);
}
}
/**
* 等待时间
*
* @param i
*/
private static void goSleep(int i) {
try {
Thread.sleep(i * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
这些是在做appium试验时的测试类,可以自己按需要改造一下