一、JAVA 环境搭建
JAVA_HOME
Path
: %JAVA_HOME%\bin
%JAVA_HOME%\jre\bin
CLASSPATH
: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
二、SDK 环境搭建
ANDROID_HOME
Path
: ;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools
下载并安装 Windows 10 SDK
三、安卓模拟器安装
https://www.bluestacks.com/bluestacks-5.html
四、连接模拟器
adb devices
emulator-5554
adb connect 127.0.0.1:5555
adb shell dumpsys window windows | findstr mFocusedApp
mFocusedApp=AppWindowToken{8ea0a2e token=Token{5e90030 ActivityRecord{740c973 u0 com.antfortune.wealth/com.alipay.mobile.quinox.LauncherActivity t16}}}
包名:com.antfortune.wealth
界面:com.alipay.mobile.quinox.LauncherActivity
获取操作系版本
adb shell getprop ro.build.version.release
7.1.2
推送文件
adb push /computer_path /phone_path
拉取文件
adb pull /phone_path /computer_path
安装包
adb install /computer_path/***.apk
五、安装 Appium
{
"platformName": "Android",
"platformVersion": "7.1.2",
"deviceName": "emulator-5554",
"automationName": "UiAutomator2",
"appPackage": "com.antfortune.wealth",
"appActivity": "com.alipay.mobile.quinox.LauncherActivity"
}
adb connect 127.0.0.1:5555
六、编写脚本
dotnet add package Appium.WebDriver
1、创建 Driver
var opts = new AppiumOptions();
opts.AddAdditionalCapability("platformName", "Android");
opts.AddAdditionalCapability("platformVersion", "7.1.2");
opts.AddAdditionalCapability("automationName", "UiAutomator2");
opts.AddAdditionalCapability("deviceName", "emulator-5554");
opts.AddAdditionalCapability("appPackage", "com.antfortune.wealth");
opts.AddAdditionalCapability("appActivity", "com.alipay.mobile.quinox.LauncherActivity");
var uri = new Uri("http://127.0.0.1:4723/wd/hub");
var driver = new AndroidDriver<AndroidElement>(uri, opts, INIT_TIMEOUT_SEC);
var driver = new WindowsDriver<WindowsElement>(uri, opts, INIT_TIMEOUT_SEC);
driver.Manager().Timeouts().ImplicitWait = IMPLICIT_TIMEOUT_SEC;
driver.Quit();
2、选择控件
var elem = driver.FindElementsByAccessibilityId("Content");
var elem = driver.FindElementsById("android:id/action_bar_container");
var elem = driver.FindElementsByClassName("android.widget.FrameLayout");
var elem = driver.FindElementsByXPath("//*[@class='android.widget.FrameLayout']");
3、操作页面
var searchBox = driver.FindElementById("txt_query_prefill");
searchBox.SendKeys("Hello World!");
var searchButton = driver.FindElementById("bt_start_search");
searchButton.Click();
var searchText = driver.FindElementById("android:id/search_src_text");
driver.StartActivity("io.appium.android.apis", ".app.AlertDialogSamples");
var openDialogButton = driver.FindElementById("io.appium.android.apis:id/two_buttons");
openDialogButton.Click();
var alert = driver.FindElementById("android:id/alertTitle");
var alertText = alert.Text;
driver.FindElementById("android:id/button1").Click();
driver.CurrentActivity;
driver.Url = "https://www.google.com"
Assert.AreEqual("Google", title);
七、开始测试
1、启动 Appium
2、打开 模拟器
3、ADB 连接模拟器
adb connect 127.0.0.1:5555
{
server: 'http://127.0.0.1:4723',
options: {
"platformName": "Android",
"platformVersion": "7.1.2",
"deviceName": "emulator-5554",
"automationName": "UiAutomator2",
"appPackage": "com.antfortune.wealth",
"appActivity": "com.alipay.mobile.nebulax.integration.mpaas.activity.NebulaActivity$Main",
"noReset": 'true',
"autoLaunch": 'false'
},
operations: [{
find: 'element',
by: 'id',
selector: 'android:id/button2',
operation: 'click'
}]
}
com.alipay.mobile.nebulax.integration.mpaas.activity.NebulaActivity$Main
com.alipay.mobile.quinox.LauncherActivity
adb shell uiautomator