当前位置: 首页 > 工具软件 > Flagship Docs > 使用案例 >

Appium翻译篇【 appium/docs/en/drivers/android-uiautomator2.md 】

仲孙默
2023-12-01

The UiAutomator2 Driver for Android

Appium’s flagship support for automating Android apps is via the UiAutomator2 driver. (New to Appium? Read our introduction to Appium drivers). This driver leverages Google’s UiAutomator2 technology to facilitate automation on a device or emulator.

Development of the UiAutomator2 driver happens at the appium-uiautomator2-driver repo.

Older Android-based drivers include:

  • The UiAutomator Driver

Requirements and Support

In addition to Appium’s general requirements:

  • Java 8 installed and configured correctly for your platform
  • Mac, Windows, or Linux OS with the ability to run the Android SDK

Further, the UiAutomator2 driver does not support Android versions below 5.0 (Lollipop, API level 21). If you are automating such versions, consider using the UiAutomator driver;

Usage

The way to start a session using the UiAutomator2 driver is to include the automationName capability in your new session request, with the value UiAutomator2. Of course, you must also include appropriate platformName (=Android), platformVersion, deviceName, and app capabilities, at a minimum.

It is highly recommended to also set the appPackage and appActivity capabilities in order to let Appium know exactly which package and activity should be launched for your application. Otherwise, Appium will try to determine these automatically from your app manifest.

Capabilities

The UiAutomator2 driver supports a number of standard Appium capabilities, but has an additional set of capabilities that modulate the behavior of the driver. These can be found currently at the Android section of the aforementioned doc.

For web tests, to automate Chrome instead of your own application, leave the app capability empty and instead set the browserName capability to Chrome. Note that you are responsible for ensuring that Chrome is on the emulator/device, and that it is of a version compatible with Chromedriver.

Commands

To see the various commands Appium supports, and specifically for information on how the commands map to behaviors for the UiAutomator2 driver, see the API Reference.

Basic Setup

  1. Ensure that you have Appium’s general dependencies (e.g., Node & NPM) installed and configured.

  2. Ensure that Java (the JDK, not just the JRE) is installed and Java binaries are added to your path. The instructions for this step differ for Mac/Linux and for Windows. Please consult platform-specific documentation, as this is a common task. An example of how to change the PATH on Windows is here.

  3. Ensure that the JAVA_HOME environment variable is also set to the JDK path. For Mac/Linux, for example (the specifics of this path will vary greatly by system), put this in your login script:

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home"
On Windows, this will be done by setting the environment variable in the control panel, using the same strategy as for setting PATH above. Android Studio also has JDK in the path like /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (Mac). You can specify the path, too.

  1. Install the Android SDK. The supported way of doing this nowadays is to use Android Studio. Use the provided GUI to install the Android SDK to a path of your choosing.

  2. Set the ANDROID_HOME environment variable to match this path. For example, if you installed the SDK to /usr/local/adt, then there will typically be a sdk folder inside of that which contains the SDK files. In that case, on Mac and Linux, add the following line to your login script (e.g., ~/.bashrc, ~/.bash_profile, etc…):

export ANDROID_HOME="/usr/local/adt/sdk"

On Windows, follow the same steps as before to set the environment variable in the control panel.

  1. Using the SDK manager, ensure you have installed the SDK for Android API levels you wish to automate (e.g., 24).

  2. On Windows, ensure that you always run Appium in Administrator mode.

At this point, your general system setup is done. Follow the steps below based on whether you want to automate an emulator or a real device. In addition you will need your app’s APK (preferably built in Debug mode), whose path or URL you will use as the value of the app capability when running your tests.

Emulator Setup

To run tests on emulators, use the AVD Manager included with Android Studio or the SDK. With this tool, create the emulator that matches your needs. With the emulator launched, Appium will automatically find and use it for its tests. Otherwise, if you specify the avd capability with the value matching the name of your emulator, then Appium will attempt to launch the emulator for you.

Additional tips for emulators:

  • There exists a hardware accelerated emulator for Android, though it has its own limitations. It can be installed from Intel’s website, or through the Android SDK Manager. For more information, go here.
  • Make sure that hw.battery=yes in your AVD’s config.ini, if you want to run any of the Appium tests, or use any of the power commands. (As of Android 5.0, this is the default.)

Real Device Setup

For Android automation, no additional setup is required for testing on real devices, other than these simple requirements:

  • Ensure that Developer mode is turned on for the device.
  • Ensure that the device is connected via USB to the Appium host, and can be seen by ADB (run adb devices to make sure).
  • Ensure that “Verify Apps” in settings is disabled, to allow Appium’s helper apps to function without manual intervention.

(For some specific commands, the device might need to be rooted, though this is not the norm.)

Android的UiAutomator2驱动程序

Appium对自动化Android应用程序的支持是通过UiAutomator2驱动程序实现的。(刚接触Appium吗?请阅读Appium驱动程序的介绍 –introduction to Appium drivers)。这个驱动程序利用谷歌的UiAutomator2技术来促进真机或模拟器上的自动化。

UiAutomator2驱动程序的开发在appium- UiAutomator2驱动程序回购中进行。

较老的基于android的驱动包括:

  • UiAutomator Driver

要求和支持

除了Appium的一般要求之外的要求:

  • Java 8已正确安装和配置
  • 有能够运行Android SDK的Mac、Windows或Linux操作系统
    此外,UiAutomator2驱动程序不支持5.0以下的Android版本(Lollipop, API级别21)。如果需要自动化此类版本,应使用UiAutomator驱动-- UiAutomator driver

用法

使用UiAutomator2驱动程序启动会话的方法是在新会话请求中包含automationName功能,值为UiAutomator2。当然,至少还必须包含适当的platformName (=Android), platformVersion, deviceName和app功能。

还应设置appPackage(包名)和appActivity(界面名),以便让Appium确切地知道应该为应用程序启动哪个包和活动。否则,Appium将尝试从应用程序清单中自动确定这些内容。

权限

UiAutomator2驱动程序支持许多标准的Appium功能–Appium capabilities,但是还有一组额外的功能来调节驱动程序的行为。这些可以在前面提到的文档的Android部分-- Android section找到。

对于web测试来说,要想自动化Chrome而不是其他浏览器,请将app功能设置为空,而将browserName功能设置为Chrome。请注意,要确保Chrome已安装在模拟器/真机上,并且它的版本与Chromedriver兼容–version compatible with Chromedriver

命令

要查看Appium支持的各种命令,特别是关于这些命令如何映射到UiAutomator2驱动程序的行为的信息,请参阅API参考-- API Reference

基本配置

  • 确保已经安装并配置了Appium的基本依赖项(例如,Node & NPM)。
  • 确保安装了Java (JDK,而不仅仅是JRE),并配置好系统环境变量path。对于Mac/Linux和Windows,此步骤的说明有所不同。请参考特定于平台的文档,因为这是一个常见的任务。这里–here有一个如何在Windows上设置path的示例。
  • 确保JAVA_HOME环境变量也被设置为JDK路径。例如,对于Mac/Linux(该路径的具体细节会因系统的不同而有很大的不同),把它放到你的登录脚本中:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home"

在Windows上,这将通过在控制面板中设置环境变量来完成,使用与上面设置PATH相同的方法。Android Studio中也有JDK,比如/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (Mac)。您也可以指定路径。

  • 安装Android SDK。现在支持的实现方式是使用Android Studio。使用提供的GUI将Android SDK安装到您选择的路径上。
  • 设置ANDROID_HOME环境变量来匹配此路径。例如,如果将SDK安装到/usr/local/adt,那么通常会有一个包含SDK文件的SDK文件夹。在这种情况下,在Mac和Linux上,将下面一行添加到您的登录脚本中(例如,~/.bashrc, ~/.bash_profile等):
export ANDROID_HOME="/usr/local/adt/sdk"

在Windows上,按照与前面相同的步骤在控制面板中设置环境变量。

  • 使用SDK管理器,确保已经为希望自动化的Android API级别安装了SDK
  • 在Windows上,确保是在管理员模式下运行Appium。
    此时,基本配置已经完成。接下来的步骤就根据是自动化模拟器还是真机。此外,在运行测试时,还需要将app的APK(最好在debug模式下构建)的路径或URL作为app功能的值。

模拟器配置

要在模拟器上运行测试,请使用包含在Android Studio或SDK中的AVD管理器。使用此工具,创建符合需要的模拟器。启动模拟器后,Appium将自动查找并使用它进行测试。否则,如果您用与模拟器名称匹配的值指定avd功能,那么Appium将尝试为您启动模拟器。
针对模拟器的其他建议:

  • 目前有一种针对Android硬件的加速模拟器,虽然它有自己的局限性。它可以从英特尔的网站上安装,也可以通过Android SDK管理器安装。想了解更多信息,请点击这里–here
  • 如果想要运行任何Appium测试,或使用任何power命令,需确保AVD的config.ini中是hw.battery=yes。(在Android 5.0中,这是默认设置。)

真机配置

对于Android自动化,真机不用额外配置,除了这些简单的要求:

  • 确保该真机的开发人员模式–Developer mode是打开的。
  • 确保设备通过USB连接到Appium主机,ADB可以看到(可运行adb devices命令来确认)。
  • 确保设置中的“Verify Apps”被禁用,以允许Appium的助手应用程序在没有人工干预的情况下运行。
    (对于某些特定的命令,设备可能需要被重启,尽管这不是标准。)
 类似资料: