搭建 Appium 自动化测试环境

本文详细介绍了如何搭建JAVA和Android环境,配置SDK,安装安卓模拟器及连接设备。通过Appium进行自动化测试,包括环境配置、脚本编写、元素选择与页面操作,并给出了具体的测试步骤。同时,文章涵盖了使用adb命令进行设备交互和安装应用。
摘要由CSDN通过智能技术生成

一、JAVA 环境搭建

JAVA_HOME
Path: %JAVA_HOME%\bin %JAVA_HOME%\jre\bin
CLASSPATH: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;

二、SDK 环境搭建

下载并安装Android 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值