activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

用户界面自动化活动

上次更新日期 2025年11月17日

开始使用移动自动化 API

本快速入门指南可帮助您使用移动自动化 API 创建第一个编码自动化。本教程使用 UiPath™ Android 基本应用程序和 Sauce Labs 的 GooglePixel 模拟器进行自动化测试。

1. 在移动设备管理器中添加移动设备和应用程序

要在编码自动化中使用所需的移动设备和应用程序,我们必须首先在 MDM(移动设备管理器)中创建它们:

  1. 要创建应用程序,请执行以下操作:
    1. 开启 MDM 中,转到“应用程序
    2. 选择“添加应用程序”
    3. 在“编辑应用程序”窗口中,输入所需的信息:
      1. 名称:在此示例中,使用 Basic App - Android
      2. 类型:对于原生应用程序,选择“App”
      3. 平台:选择“Android”以自动化 Android 应用程序。
      4. 应用程序:对于此示例,将以下链接输入到基本 Android 应用程序:https://uipathtestappsto.blob.core.windows.net/testapps/BasicAppAndroid.apk
      5. 应用程序包:输入移动应用程序包,例如 com.example.basicapp
      6. 应用程序活动:输入活动实例,例如:.core.MainActivity
      7. 对于其他所需功能,添加 appium:app 功能,以确保应用程序按预期运行。
      表 1. 其他所需功能
      名称
      appium:appappium:app https://uipathtestappsto.blob.core.windows.net/testapps/BasicAppAndroid.apk


  2. 要创建设备,请执行以下操作:
    1. 转到“设备”,然后选择“添加设备”
    2. 为设备命名。在此示例中,使用 Google Pixel 3a GoogleAPI Emulator
    3. 设置以下参数:Appium URL平台设备名称平台版本
      1. 从您选择的移动设备场提供程序检索参数。在此示例中,我们使用的是 Sauce Labs。
      2. 要获取 Appium URL,请转到“帐户”,然后转到“用户设置”。复制“驱动程序创建”下的按需 URL
      3. 要获取平台设备名称平台版本,请转到“自动化”,然后再转到“平台配置器”。选择 Google Pixel 3a GoogleAPI EmulatorAndroid Emulator version 10.0 的功能。在“配置脚本”下,选择您的 Appium version。在此示例中,请使用 2.0.0。
      4. 将 Sauce 实验室中的值复制并粘贴到 MDM 中。


    4. 选择“保存并关闭”
    5. 选择“在此设备上启动应用程序”,然后连接该设备与先前创建的应用程序。

2. 为移动应用程序创建对象存储库

要自动化您的首选移动应用程序,请在项目的对象存储库中记录其移动元素。在此示例中,我们创建了一个带有常规主屏幕的“基本应用程序”应用程序,其中包含我们用于自动化的三个用户界面元素。检查以下列表,查看对象存储库的结构如何显示:

  • BasicApp
    • HomeScreen
      • EditText
      • SingleClickButton
      • VerifyText


3. 建立连接

在 MDM 中配置设备和应用程序后,请开始为自动化编写代码。在开始使用移动自动化 API 之前,您需要使用相应的 API 在所选移动设备和应用程序之间建立连接。

A connection is a class that represents the connection that you create between a device and an application, which exposes the APIs that you can use to automate mobile testing scenarios. Visit Connection for more information on creating a connection.

To create a connection, you have a set of dedicated APIs that you can use. In this example, we used the Connect API, with the second overload, which requires the device and application names as Strings.

使用以下步骤创建连接:

  1. using 语句开始,以避免创建单独的变量。
  2. using 语句中,使用第二个重载版本调用连接 API。

    对于此重载,请输入设备名称和应用程序名称以建立连接。

    [TestCase]
            public void Execute()
            {
            using (Connection connection = mobile.Connect(
                "Google Pixel 3a GoogleAPI Emulator", 
                "Basic App - Android"))[TestCase]
            public void Execute()
            {
            using (Connection connection = mobile.Connect(
                "Google Pixel 3a GoogleAPI Emulator", 
                "Basic App - Android"))
  3. (可选)出于调试目的,您可以添加一条 Log 消息。
    Log("Connection established"); Log("Connection established");

4. 开始自动化移动应用程序和设备

此示例中的自动化流程包括点击某些按钮,获取移动设备屏幕上显示的文本以及滑动滑块控件。这些操作模拟现实生活中的移动自动化场景。

To automate the UI elements in this example, we used IElementDescriptors. These represent UI elements accessed directly from the Object Repository. In addition to IElementDescriptors, you can use other selector types with Mobile Automation APIs, such as ObjectRepositoryTarget, and SelectorTarget. Visit API selectors to learn how and when to use other selector types.
  1. Call the Tap API, to click the SingleClickButton from the Android Basic App.
    // Tap the SingleClickButton
                connection.Tap(Descriptors.BasicApp.HomeScreen.SingleClickButton);
                Log("Tapped SingleClickButton ");// Tap the SingleClickButton
                connection.Tap(Descriptors.BasicApp.HomeScreen.SingleClickButton);
                Log("Tapped SingleClickButton ");
  2. Use the SetText API to input a specific text into the EditText field.
    string expectText = "Random text";
                connection.SetText(Descriptors.BasicApp.HomeScreen.EditText, expectText);
                Log("Inserted random text");string expectText = "Random text";
                connection.SetText(Descriptors.BasicApp.HomeScreen.EditText, expectText);
                Log("Inserted random text");
  3. Get the text you previously set, and verify if it matches the text from the VerifyText UI element. Use the GetText and VerifyExpression APIs.
    var actualText = connection.GetText(Descriptors.BasicApp.HomeScreen.VerifyText);
                testing.VerifyExpression(expectText == actualText, "EditText validation failed.");
                SelectorTarget selector = null;var actualText = connection.GetText(Descriptors.BasicApp.HomeScreen.VerifyText);
                testing.VerifyExpression(expectText == actualText, "EditText validation failed.");
                SelectorTarget selector = null;
  4. Use the PositionalSwipe API to move the slider control.
    PositionalSwipe API 需要两个 Point 变量,表示滑动操作的起点和终点。在使用 API 之前创建这些点以增强可读性。
    // Perform a positional swipe operation to use the slider
    
                var startPoint = new Point(226, 304);
                var endPoint = new Point(296, 304);
                
                connection.PositionalSwipe(startPoint, endPoint);
    
                Log("Performed Swipe action."); // Perform a positional swipe operation to use the slider
    
                var startPoint = new Point(226, 304);
                var endPoint = new Point(296, 304);
                
                connection.PositionalSwipe(startPoint, endPoint);
    
                Log("Performed Swipe action.");
    要获取屏幕位置,请转到 MDM 中的活动连接。将光标悬停在移动屏幕上。这将在 MDM 的左下角显示光标的位置(以像素为单位)。


工作流示例

要按照此快速入门指南中的步骤操作,或者自己尝试,请下载移动自动化 API 快速入门示例

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo
信任与安全
© 2005-2025 UiPath。保留所有权利。