1.java基础环境 (eclipse安装、jdk安装配置、maven安装配置、maven插件(在eclipse中安装)、testng插件(在eclipse中安装))
2. Android SDK Tools下载安装( http://www.androiddevtools.cn/ 不用可使用)
3.运行Android SDK Tools解压包中的“SDK Manager.exe”下载,然后配置android环境变量,(adb命令等需要配置android环境变量;uiautomatorviewer.bat运行需要正确的adb配置;https://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html)
在windows的运行窗口输入“cmd”,打开命令行窗口, 输入“adb”, 出现如下图所示内容则配置成功:
4.获取appPackage和appActivity
通过cmd命令,前提是先打开手机中你要获取包名的APP
1) adb shell
2)dumpsys activity | grep mFocusedActivity (获取当前页的Activity)
dumpsys activity | strfind mFocusedActivity
5.双击开启uiautomatorviewer.bat
6.uiautomatorviewer.bat开启后获得定位信息
----------------------------------------------------------------------------------------------------------------
appium服务端环境
安装 Node.js(https://nodejs.org/en/)
Appium-desktop项目地址:https://github.com/appium/appium-desktop
下载地址:https://github.com/appium/appium-desktop/releases
appium启动页配置(在windows环境变量配置后,会自动读入的):
------------------------------------------------
附上pom.xml文件
4.0.0
appiumTest
appiumTest
0.0.1-SNAPSHOT
jar
appiumTest
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
org.testng
testng
6.8.7
test
io.appium
java-client
6.1.0
简易例子,app已经安装在手机上的配置
package appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.Assert;
public class SmtTest {
private AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Redmi_5_Plus");// 设备名称
cap.setCapability("platformName", "Android"); // 安卓自动化还是IOS自动化
cap.setCapability("platformVersion", "7.1.2"); // 安卓操作系统版本
cap.setCapability("udid", "0504942b9805"); // 设备的udid (adb devices 查看到的)
cap.setCapability("appPackage", "被测app的包名");// 被测app的包名
cap.setCapability("appActivity", "被测app的入口Activity名称");// 被测app的入口Activity名称
//A new session could not be created的解决方法
cap.setCapability("appWaitActivity","被测app的入口Activity名称");
cap.setCapability("noReset", true);
//cap.setCapability("noSign", true); // 不重新签名apk
cap.setCapability("newCommandTimeout", "30"); // 没有新命令,appium30秒退出
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);// 把以上配置传到appium服务端并连接手机,appium服务器可以与程序运行环境不在同一台机器
}
@Test
public void test1() throws InterruptedException{
Thread.sleep(5000);
String s =driver.findElementById("元素id").getText();
System.out.println(s);
Assert.assertEquals("台风查询", s);
driver.findElementById("元素id").click();
/*
...
*/
Thread.sleep(5000);
}
@AfterClass
public void down() throws MalformedURLException {
driver.quit();
}
}