原文来自:一颗糖果 http://www.cnblogs.com/linglingyuese/articles/8418311.html
一、环境搭建
略(后期补)
二、创建一个测试apk包的项目
三、新建项目之后导入jar包、apk等
1、在项目下面创建文件夹:apps、libs,并把要测试的apk安装包及jar包导入项目,准备工作完成后整个 框架如下图所示
注意:java-client和selenium-server-standalone的jar包的版本要对应,最好找齐如上截图所示的版本,要不然会版本冲突导致未知错误
四、新建一个测试Demo:QuizTest.java
package com.test.quiz; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.AppiumDriver; public class QuizTest { public AppiumDriver driver; private boolean isInstall = false; public void startRecord () throws IOException{ Runtime rc = Runtime.getRuntime(); rc.exec("cmd.exe /E:/Android/sdk/platform-tools adb shell screenrecord /sdcard/runCase.mp4");//录制屏幕运行视频 } /** * 开始启动 * @throws IOException */ @Before public void setUp() throws IOException{ // 启动appium DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "emulator-5554");//指定测试机 capabilities.setCapability("automationName", "Appium"); capabilities.setCapability("platformName", "Android");// 指定测试平台 capabilities.setCapability("platformVersion", "22");//平台版本 // 配置测试apk capabilities.setCapability("appPackage", "com.example.quizactivity");//测试的包 capabilities.setCapability("appActivity", "com.example.quizactivity.QuizActivity");//测试的activity capabilities.setCapability("sessionoverride", true);//每次启动时覆盖session,否则第二次后运行会报错不能新建session capabilities.setCapability("unicodekeyboard", true);//设置键盘 capabilities.setCapability("resetkeyboard", false); //设置默认键盘为appium的键盘 driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 安装APK,如果真机设备已经安装,则不需要重新安装 if (isInstall){ File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot,"apps"); File app = new File(appDir,"QuizActivity.apk"); capabilities.setCapability("app", app.getAbsolutePath()); } startRecord(); } /** * 点击显示上一条 * @throws InterruptedException */ @Test public void PreQuestion() throws InterruptedException{ driver.findElementById("com.example.quizactivity:id/prev_button").click();// 点击上一个按钮 Thread.sleep(2000); driver.findElementById("com.example.quizactivity:id/next_button").click();//点击下一个按钮 Thread.sleep(2000); String tips = driver.findElementById("com.example.quizactivity:id/question_text_view").getText(); Assert.assertEquals(tips, "The Suez Canal connects the Red Sea and the Indian Ocean."); Thread.sleep(2000); } /** * 结束启动 */ @After public void tearDown(){ driver.quit(); } }
其中:
1、指定测试机的获取:adb devices
2、测试包及测试的activity获取:adb shell dumpsys window | findstr mCurrentFocus
![](https://images2017.cnblogs.com/blog/348423/201802/348423-20180205163355607-1380528643.png)
3、元素ID的获取:可以利用android-sdk自带的UI Automator Viewer
UI Automator Viewer的目录地址:D:androidsdk oolsuiautomatorviewer.bat
双击打开uiautomatorviewer.bat,出现如下图所示页面:
获取PrevButton:
其他值也可以照样获取
五、执行appium
1、首先启动appium
2、在eclipse运行测试项目
3、查看手机可以看到APP自动运行所编写的脚本