1.Appium Desktop下载,安装成功,配置host
https://github.com/appium/appium-desktop/releases
https://testerhome.com/topics/680
2.安卓模拟器-夜神模拟器
https://www.yeshen.com/
安装完成打开夜神模拟器,运行cmd,执行adb connect 127.0.0.1:62001,提示如下:
* daemon not running; starting now at tcp:5037
* daemon started successfully
connected to 127.0.0.1:62001
3.配置Appuim,以高德地图为例,点击:Start Session
配置信息
{
"deviceName": "127.0.0.1:62001",
"appPackage": "com.autonavi.minimap",
"appActivity": "com.autonavi.map.activity.NewMapActivity",
"platformName": "android",
"platformVersion": "5.1.1"
}
可以用下面两条命令,获取当前app和当前Activity的名字
adb shell dumpsys activity | find "mFocusedActivity"
adb shell dumpsys window w |findstr \/ |findstr name=
3.代码实现:
依赖jar包
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
public class TestAppuim {
public static void main(String[] args)throws MalformedURLException, InterruptedException {
// TODO Auto-generated method stub
//添加配置,创建DesiredCapabilities对象
DesiredCapabilities desiredCapabilities =new DesiredCapabilities();
//指定测试设备的名称
desiredCapabilities.setCapability("deviceName","127.0.0.1:62001");
//添加操作系统配置
desiredCapabilities.setCapability("platformName","Android");
//添加操作系统版本设置
desiredCapabilities.setCapability("platformVersion","5.1.1");
//指定想要测试应用的包名
desiredCapabilities.setCapability("appPackage","com.autonavi.minimap");
//指定想要测试应用的入口activity
desiredCapabilities.setCapability("appActivity","com.autonavi.map.activity.NewMapActivity");
/ /创建驱动...URL是appium的固定地址;指定appium通讯的地址,将相对应的配置传入到驱动里边
AndroidDriver androidDriver =new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
//加线程等待
Thread.sleep(2000);
//点击:同意
androidDriver.findElementById("com.autonavi.minimap:id/agree").click();
//退出
Thread.sleep(2000);
androidDriver.quit();
}
}
附录:笔者在连接模拟器的时没有拷贝adb.exe文件
如果需要拷贝请参考以下步骤:
1.把Android SDK目录下\platform-tools的adb.exe拷贝到Nox\bin目录,粘贴替换adb.exe,打开模拟器,cmd输入adb connect 127.0.0.1:62001
2. 如果夜神模拟器的adb版本和androidsdk的adb版本不一致
把Android SDK目录下\platform-tools的adb.exe拷贝,
打开Nox\bin目录,粘贴,不用替换adb.exe,重命名成nox_adb.exe,打开模拟器
cmd输入adb connect 127.0.0.1:62001
如果调试发现以下错误,重新执行adb connect 127.0.0.1:62001,如果还有问题请检查端口号占用情况,netstat -aon | findstr "5037"