Appium 学习笔记 -- 1. 安装和验证

appium有两种安装方式,一种是使用npm命令行安装方式,另外一种是下载Appium Desktop图形化界面安装。本文使用npm命令行安装方式。

背景

  • 操作系统 : Windows 10 64位 专业版
  • node 已经安装 (v.10.16.3)
  • Java 已经安装 (jdk1.8.0_77)
    • 环境变量 JAVA_HOME 已经设置
  • Android SDK 已经安装
    • 环境变量 ANDROID_HOME 已经设置

为了命令行使用各种工具方便,我已经将以下内容添加到环境变量PATH中 :

  1. %ANDROID_HOME%\tools
  2. %ANDROID_HOME%\platform-tools

安装步骤

1. 安装 appium

打开一个cmd窗口,使用如下命令安装appium:

npm install -g appium --registry=https://registry.npm.taobao.org

备注 :

  1. 全局安装
  2. 使用国内淘宝镜像以提高速度

安装完成后使用如下命令查看所安装的appium版本 :

appium -v

结果输出 :

1.16.0

2. 安装 appium-doctor

打开一个cmd窗口,使用如下命令安装appium-doctor:

npm install -g appium-doctor --registry=https://registry.npm.taobao.org

备注 :

  1. 全局安装
  2. 使用国内淘宝镜像以提高速度

安装完成后使用如下命令查看所安装的appium-doctor版本 :

appium-doctor --version

结果输出 :

1.13.0

3. 使用 appium-doctor 检测 appium 工作环境

cmd窗口中,使用如下命令检测针对android平台appium安装是否就绪 :

appium-doctor --android

然后你会看到类似如下结果 :

info AppiumDoctor Appium Doctor v.1.13.0
info AppiumDoctor ### Diagnostic for necessary dependencies starting ###
info AppiumDoctor  ✔ The Node.js binary was found at: D:\Program Files\nodejs\node.EXE
info AppiumDoctor  ✔ Node version is 10.16.3
info AppiumDoctor  ✔ ANDROID_HOME is set to: D:\programs\Android\sdk
info AppiumDoctor  ✔ JAVA_HOME is set to: C:\Program Files\Java\jdk1.8.0_77
info AppiumDoctor  ✔ adb exists at: D:\programs\Android\sdk\platform-tools\adb.exe
info AppiumDoctor  ✔ android exists at: D:\programs\Android\sdk\tools\android.bat
info AppiumDoctor  ✔ emulator exists at: D:\programs\Android\sdk\tools\emulator.exe
info AppiumDoctor  ✔ Bin directory of %JAVA_HOME% is set
info AppiumDoctor ### Diagnostic for necessary dependencies completed, no fix needed. ###
info AppiumDoctor
info AppiumDoctor ### Diagnostic for optional dependencies starting ###
WARN AppiumDoctor  ✖ opencv4nodejs cannot be found.
info AppiumDoctor  ✔ ffmpeg is installed at: C:\Program Files\ImageMagick-7.0.9-Q16\ffmpeg.EXE. ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developers
WARN AppiumDoctor  ✖ mjpeg-consumer cannot be found.
WARN AppiumDoctor  ✖ bundletool.jar cannot be found
info AppiumDoctor ### Diagnostic for optional dependencies completed, 3 fixes possible. ###
info AppiumDoctor
info AppiumDoctor ### Optional Manual Fixes ###
info AppiumDoctor The configuration can install optionally. Please do the following manually:
WARN AppiumDoctor  ➜ Why opencv4nodejs is needed and how to install it: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md
WARN AppiumDoctor  ➜ mjpeg-consumer module is required to use MJPEG-over-HTTP features. Please install it with 'npm i -g mjpeg-consumer'.
WARN AppiumDoctor  ➜ bundletool.jar is used to handle Android App Bundle. Please read http://appium.io/docs/en/writing-running-appium/android/android-appbundle/ to install it. Also consider adding the ".jar" extension into your PATHEXT environment variable in order to fix the problem for Windows
info AppiumDoctor
info AppiumDoctor ###
info AppiumDoctor
info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied!
info AppiumDoctor

备注 :
以上结果第一部分是必要依赖部分,由以上结果可见,所有必要的依赖条件都满足了,这说明appium在本机可以正常使用了。

4. 运行一个 android 测试

4.1 下载测试应用

这是一个由 appium 官方提供的测试应用。
假设该应用下载到本地以后,保存为 d:\ApiDemos-debug.apk

4.2 新建测试脚本

基于Webdriver.io创建一个测试脚本 :

  1. npm init -y
  2. npm install webdriverio
  3. 创建脚本如下index.js
// javascript

const wdio = require("webdriverio");
const assert = require("assert");

const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "8",
    deviceName: "Android Emulator",
    app: "d:\\ApiDemos-debug.apk",
    appPackage: "io.appium.android.apis",
    appActivity: ".view.TextFields",
    automationName: "UiAutomator2"
  }
};

async function main () {
  const client = await wdio.remote(opts);

  const field = await client.$("android.widget.EditText");
  await field.setValue("Hello World!");
  const value = await field.getText();
  assert.equal(value,"Hello World!");

  await client.deleteSession();
}

main();

注意这里使用的android平台版本为8,这里要求下面使用的android虚拟机跟这里一致。

4.3 启动 appium

打开一个cmd命令行窗口,使用如下命令行启动appium :

appium

4.4 打开一个安卓虚拟器

注意上面的测试脚本中使用的android平台版本为8,所以要求这里使用的android虚拟机也是版本8

4.5 运行测试脚本

node index.js

可以观测到如下运行结果 :

  • 控制台输出
D:\tmp\Appium>node index.js
2020-01-13T07:11:59.156Z INFO webdriverio: Initiate new session using the webdriver protocol
2020-01-13T07:11:59.163Z INFO webdriver: [POST] http://localhost:4723/wd/hub/session
2020-01-13T07:11:59.164Z INFO webdriver: DATA { capabilities:
   { alwaysMatch:
      { platformName: 'Android',
        platformVersion: '8',
        deviceName: 'Android Emulator',
        app: 'd:\\ApiDemos-debug.apk',
        appPackage: 'io.appium.android.apis',
        appActivity: '.view.TextFields',
        automationName: 'UiAutomator2' },
     firstMatch: [ {} ] },
  desiredCapabilities:
   { platformName: 'Android',
     platformVersion: '8',
     deviceName: 'Android Emulator',
     app: 'd:\\ApiDemos-debug.apk',
     appPackage: 'io.appium.android.apis',
     appActivity: '.view.TextFields',
     automationName: 'UiAutomator2' } }
2020-01-13T07:12:15.130Z INFO webdriver: COMMAND findElement("class name", "android.widget.EditText")
2020-01-13T07:12:15.134Z INFO webdriver: [POST] http://localhost:4723/wd/hub/session/e1483a67-07ec-40ca-a943-9b7df98c019c/element
2020-01-13T07:12:15.135Z INFO webdriver: DATA { using: 'class name', value: 'android.widget.EditText' }
2020-01-13T07:12:17.016Z INFO webdriver: RESULT { 'element-6066-11e4-a52e-4f735466cecf': 'adb1f252-017b-48d5-999c-d0592fd307c2',
  ELEMENT: 'adb1f252-017b-48d5-999c-d0592fd307c2' }
2020-01-13T07:12:17.030Z INFO webdriver: COMMAND elementClear("adb1f252-017b-48d5-999c-d0592fd307c2")
2020-01-13T07:12:17.031Z INFO webdriver: [POST] http://localhost:4723/wd/hub/session/e1483a67-07ec-40ca-a943-9b7df98c019c/element/adb1f252-017b-48d5-999c-d0592fd307c2/clear
2020-01-13T07:12:17.080Z INFO webdriver: COMMAND elementSendKeys("adb1f252-017b-48d5-999c-d0592fd307c2", "Hello World!", <object>)
2020-01-13T07:12:17.083Z INFO webdriver: [POST] http://localhost:4723/wd/hub/session/e1483a67-07ec-40ca-a943-9b7df98c019c/element/adb1f252-017b-48d5-999c-d0592fd307c2/value
2020-01-13T07:12:17.084Z INFO webdriver: DATA { text: 'Hello World!',
  value:
   [ 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' ] }
2020-01-13T07:12:18.570Z INFO webdriver: COMMAND getElementText("adb1f252-017b-48d5-999c-d0592fd307c2")
2020-01-13T07:12:18.575Z INFO webdriver: [GET] http://localhost:4723/wd/hub/session/e1483a67-07ec-40ca-a943-9b7df98c019c/element/adb1f252-017b-48d5-999c-d0592fd307c2/text
2020-01-13T07:12:19.275Z INFO webdriver: RESULT Hello World!
2020-01-13T07:12:19.282Z INFO webdriver: COMMAND deleteSession()
2020-01-13T07:12:19.286Z INFO webdriver: [DELETE] http://localhost:4723/wd/hub/session/e1483a67-07ec-40ca-a943-9b7df98c019c

虚拟机测试脚本执行截屏

在这里插入图片描述

参考资料

  1. Appium Get Started
  2. The UiAutomator2 Driver for Android
  3. Introduction to Appium
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页