

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Appium 与设备互动
<a name="appium-endpoint-interaction"></a>

[创建远程访问会话](how-to-create-session.md)后，该设备将可用于 Appium 测试。在整个远程访问会话期间，你可以在设备上随心所欲地运行任意数量的 Appium 会话，对使用的客户端没有限制。例如，你可以先使用 IDE 中的本地 Appium 代码运行测试，然后切换到使用 Appium Inspector 来解决遇到的任何问题。会话最长可持续 [150 分钟](limits.md#service-limits)，但是，如果超过 5 分钟没有活动（通过交互式控制台或 Appium 端点），则会话将超时。

## 在 Appium 会话中使用应用程序进行测试
<a name="appium-endpoint-using-apps"></a>

Device Farm 允许您将应用程序用作远程访问会话创建请求的一部分，或者在远程访问会话本身期间安装应用程序。这些应用程序会自动安装到被测设备上，并作为任何 Appium 会话请求的默认功能注入。创建远程访问会话时，您可以选择传入应用程序 ARN（默认情况下，该应用程序将用作所有后续的 Appium 会话的`appium:app`功能）以及辅助应用程序 ARNs（将用作该功能）。`appium:otherApps`

例如，如果您使用应用程序`com.aws.devicefarm.sample`作为应用程序和`com.aws.devicefarm.other.sample`辅助应用程序之一来创建远程访问会话，那么当您开始创建 Appium 会话时，它将具有类似于以下内容的功能：

```
{
    "value":
    {
        "sessionId": "abcdef123456-1234-5678-abcd-abcdef123456",
        "capabilities":
        {
            "app": "/tmp/com.aws.devicefarm.sample.apk",
            "otherApps": "[\"/tmp/com.aws.devicefarm.other.sample.apk\"]",
            ...
        }
    }
}
```

在会话期间，您可以安装其他应用程序（在控制台中或使用 [https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_InstallToRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_InstallToRemoteAccessSession.html)API）。它们将覆盖以前用作该`appium:app`功能的任何现有应用程序。如果以前使用的应用程序具有不同的软件包名称，则它们将保留在设备上并用作`appium:otherApps`功能的一部分。

例如，如果您最初在创建远程访问会话`com.aws.devicefarm.sample`时使用应用程序，但随后在会话`com.aws.devicefarm.other.sample`期间安装了一个名为的新应用程序，则您的 Appium 会话将具有与以下内容类似的功能：

```
{
    "value":
    {
        "sessionId": "abcdef123456-1234-5678-abcd-abcdef123456",
        "capabilities":
        {
            "app": "/tmp/com.aws.devicefarm.other.sample.apk",
            "otherApps": "[\"/tmp/com.aws.devicefarm.sample.apk\"]",
            ...
        }
    }
}
```

如果你愿意，你可以使用应用程序名称明确指定应用程序的功能（分别使用适用于 Android 和 iOS 的`appium:appPackage`或`appium:bundleId`功能）。

如果您正在测试 Web 应用程序，请为 Appium 会话创建请求指定`browserName`功能。该`Chrome`浏览器可在所有 Android 设备上使用，该`Safari`浏览器可在所有 iOS 设备上使用。

Device Farm 不支持在远程访问会话`appium:app`期间传入远程 URL 或本地文件系统路径。将应用程序上传到 Device Farm，改为将其包含在会话中。

**注意**  
有关在远程访问会话中自动上传应用程序的更多信息，请参阅[自动上传应用程序](api-ref.md#upload-example)。

## 如何使用 Appium 终端节点
<a name="appium-endpoint-how-to-use"></a>

以下是从控制台、和访问会话的 Appium 端点的 AWS CLI步骤。 AWS SDKs这些步骤包括如何开始使用各种 Appium 客户端测试框架运行测试：

------
#### [ Console ]

1. 在 Web 浏览器中打开远程访问会话页面：  
![\[\]](http://docs.aws.amazon.com/zh_cn/devicefarm/latest/developerguide/images/aws-device-farm-appium-endpoint.png)

1. 要使用 Appium Inspector 运行会话，请执行以下操作：

   1. 单击 “**设置 Appi** um 会话” 按钮

   1. 按照页面上的说明进行操作，了解如何使用 Appium Inspector 启动会话。

1. 要从本地 IDE 运行 Appium 测试，请执行以下操作：

   1. 点击文本 **Appium** 端点网址旁边的 “复制” 图标

   1. 将此 URL 粘贴到您当前指定远程地址或命令执行器的本地 Appium 代码中。要查看特定语言的示例，请单击此示例窗口中您选择的语言的选项卡。

------
#### [ AWS CLI ]

首先， up-to-date通过[下载并安装最新版本来验证您的 AWS CLI 版本是否为最新版本](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

**重要**  
Appium 终端节点字段在旧版本的 AWS CLI 中不可用。

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
$ aws devicefarm get-remote-access-session \
    --arn "arn:aws:devicefarm:us-west-2:123456789876:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000"
```

这将显示如下输出：

```
{
    "remoteAccessSession": {
        "arn": "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000",
        "name": "Google Pixel 8",
        "status": "RUNNING",
        "endpoints": {
            "remoteDriverEndpoint": "https://devicefarm-interactive-global.us-west-2.api.aws/remote-endpoint/ABCD1234...",
        ...
}
```

无论您当前指定远程地址或命令执行器，都可以在本地 Appium 代码中使用此 URL。要查看特定语言的示例，请单击此示例窗口中您选择的语言的选项卡。

有关如何直接从命令行与端点交互的示例，您可以使用[命令行工具 curl](https://curl.se/) 直接调用端点： WebDriver 

```
$ curl "https://devicefarm-interactive-global.us-west-2.api.aws/remote-endpoint/ABCD1234.../status"
```

这将显示如下输出：

```
{
    "value":
    {
        "ready": true,
        "message": "The server is ready to accept new connections",
        "build":
        {
            "version": "2.5.1"
        }
    }
}
```

------
#### [ Python ]

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
# To get the URL
import sys
import boto3
from botocore.exceptions import ClientError

def get_appium_endpoint() -> str:
    session_arn = "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000"
    device_farm_client = boto3.client("devicefarm", region_name="us-west-2")

    try:
        resp = device_farm_client.get_remote_access_session(arn=session_arn)
    except ClientError as exc:
        sys.exit(f"Failed to call Device Farm: {exc}")

    remote_access_session = resp.get("remoteAccessSession", {})
    endpoints = remote_access_session.get("endpoints", {})
    endpoint = endpoints.get("remoteDriverEndpoint")

    if not endpoint:
        sys.exit("Device Farm response did not include endpoints.remoteDriverEndpoint")

    return endpoint

# To use the URL
from appium import webdriver
from appium.options.android import UiAutomator2Options

opts = UiAutomator2Options()
driver = webdriver.Remote(get_appium_endpoint(), options=opts)
# ...
driver.quit()
```

------
#### [ Java ]

*注意：此示例使用 AWS 适用于 Java v2 的 SDK，并且与 JDK 版本 11 及更高版本兼容。*

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
// To get the URL
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.devicefarm.DeviceFarmClient;
import software.amazon.awssdk.services.devicefarm.model.GetRemoteAccessSessionRequest;
import software.amazon.awssdk.services.devicefarm.model.GetRemoteAccessSessionResponse;

public class AppiumEndpointBuilder {
    public static String getAppiumEndpoint() throws Exception {
        String session_arn = "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000";

        try (DeviceFarmClient client = DeviceFarmClient.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(DefaultCredentialsProvider.create())
                .build()) {

            GetRemoteAccessSessionResponse resp = client.getRemoteAccessSession(
                    GetRemoteAccessSessionRequest.builder().arn(session_arn).build()
            );

            String endpoint = resp.remoteAccessSession().endpoints().remoteDriverEndpoint();
            if (endpoint == null || endpoint.isEmpty()) {
                throw new IllegalStateException("remoteDriverEndpoint missing from response");
            }
            return endpoint;
        }
    }
}

// To use the URL
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;

import java.net.URL;

public class ExampleTest {
    public static void main(String[] args) throws Exception {
        String endpoint = AppiumEndpointBuilder.getAppiumEndpoint();
        UiAutomator2Options options = new UiAutomator2Options();
        AndroidDriver driver = new AndroidDriver(new URL(endpoint), options);

        try {
            // ... your test ...
        } finally {
            driver.quit();
        }
    }
}
```

------
#### [ JavaScript ]

*注意：此示例使用 AWS 适用于 v JavaScript 3 的 SDK，使用 Node 18\$1 的 WebDriverIO v8\$1。*

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
// To get the URL
import { DeviceFarmClient, GetRemoteAccessSessionCommand } from "@aws-sdk/client-device-farm";

export async function getAppiumEndpoint() {
  const sessionArn = "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000";

  const client = new DeviceFarmClient({ region: "us-west-2" });
  const resp = await client.send(new GetRemoteAccessSessionCommand({ arn: sessionArn }));

  const endpoint = resp?.remoteAccessSession?.endpoints?.remoteDriverEndpoint;
  if (!endpoint) throw new Error("remoteDriverEndpoint missing from response");
  return endpoint;
}

// To use the URL with WebdriverIO
import { remote } from "webdriverio";

(async () => {
  const endpoint = await getAppiumEndpoint();
  const u = new URL(endpoint);

  const driver = await remote({
    protocol: u.protocol.replace(":", ""),
    hostname: u.hostname,
    port: u.port ? Number(u.port) : (u.protocol === "https:" ? 443 : 80),
    path: u.pathname + u.search,
    capabilities: {
      platformName: "Android",
      "appium:automationName": "UiAutomator2",
      // ...other caps...
    },
  });

  try {
    // ... your test ...
  } finally {
    await driver.deleteSession();
  }
})();
```

------
#### [ C\$1 ]

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
// To get the URL
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.DeviceFarm;
using Amazon.DeviceFarm.Model;

public static class AppiumEndpointBuilder
{
    public static async Task<string> GetAppiumEndpointAsync()
    {
        var sessionArn = "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000";

        var config = new AmazonDeviceFarmConfig
        {
            RegionEndpoint = RegionEndpoint.USWest2
        };
        using var client = new AmazonDeviceFarmClient(config);

        var resp = await client.GetRemoteAccessSessionAsync(new GetRemoteAccessSessionRequest { Arn = sessionArn });
        var endpoint = resp?.RemoteAccessSession?.Endpoints?.RemoteDriverEndpoint;

        if (string.IsNullOrWhiteSpace(endpoint))
            throw new InvalidOperationException("RemoteDriverEndpoint missing from response");

        return endpoint;
    }
}

// To use the URL
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;

class Example
{
    static async Task Main()
    {
        var endpoint = await AppiumEndpointBuilder.GetAppiumEndpointAsync();

        var options = new AppiumOptions();
        options.PlatformName = "Android";
        options.AutomationName = "UiAutomator2";

        using var driver = new AndroidDriver(new Uri(endpoint), options);
        try
        {
            // ... your test ...
        }
        finally
        {
            driver.Quit();
        }
    }
}
```

------
#### [ Ruby ]

会话启动并运行后，Appium 端点网址将通过`remoteDriverEndpoint`在响应 API 调用时命名的字段提供：[https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html](https://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetRemoteAccessSession.html)

```
# To get the URL
require 'aws-sdk-devicefarm'

def get_appium_endpoint
  session_arn = "arn:aws:devicefarm:us-west-2:111122223333:session:abcdef123456-1234-5678-abcd-abcdef123456/abcdef123456-1234-5678-abcd-abcdef123456/00000"

  client = Aws::DeviceFarm::Client.new(region: 'us-west-2')
  resp = client.get_remote_access_session(arn: session_arn)
  endpoint = resp.remote_access_session.endpoints.remote_driver_endpoint
  raise "remote_driver_endpoint missing from response" if endpoint.nil? || endpoint.empty?
  endpoint
end

# To use the URL
require 'appium_lib_core'

endpoint = get_appium_endpoint
opts = {
  server_url: endpoint,
  capabilities: {
    'platformName' => 'Android',
    'appium:automationName' => 'UiAutomator2'
  }
}

driver = Appium::Core.for(opts).start_driver
begin
  # ... your test ...
ensure
  driver.quit
end
```

------