This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

在 EAS 工作流上用 Maestro 运行端到端测试

学习如何使用 Maestro 在 EAS 工作流程中使用 Android 和 iOS 开发构建自动化端到端(E2E)测试。


因为审查者无法在每次拉取请求上运行应用,所以破碎的导航流程和崩溃的屏幕经常会漏过代码审查。

🌐 Broken navigation flows and crashing screens often slip past code review because reviewers cannot run the app on every pull request.

学习成果

🌐 Learning outcomes

  • 为导航和屏幕内容测试编写 Maestro 流程文件
  • 在工作流中对 Android 和 iOS 开发版 运行端到端测试
  • 在拉取请求中使用 on.pull_request 自动触发端到端测试,或者使用 on.pull_request_labeled 按需触发

为什么 E2E 测试有用?

🌐 Why are E2E tests useful?

前几章的流水线会构建我们的项目并提供更新,还会发 Slack 通知。不过,导航流程出错或者某个屏幕崩溃仍然可能悄悄上线。端到端测试可以通过模拟真实用户操作并验证应用是否按预期运行,帮助在上线前捕获这些问题。

🌐 The pipeline from previous chapters builds our project and delivers updates, with Slack notifications. However, a broken navigation flow or a crashing screen can still make it to production silently. E2E tests can help catch these issues before they reach production by simulating real user interactions and verifying that the app behaves as expected.

Maestro 在 Android 模拟器或 iOS 模拟器上对已构建的应用运行自动化流程。它可以点击按钮、输入文字、验证屏幕内容,以及在各个屏幕之间导航。EAS 工作流会在构建好 Android 的 APK 或 iOS 模拟器构建之后,在云端运行这些流程。

设置端到端测试

🌐 Set up E2E tests

对于 Android 和 iOS,我们需要为 E2E 测试添加一个特殊的 构建配置文件。这个配置文件会跳过 Android 的凭证设置,并允许在模拟器中运行我们的 iOS 构建。

🌐 For Android and iOS, we need to add a special build profile for E2E tests. This profile skips credential setup for Android and allows running our iOS build inside a simulator.

1

添加 e2e 测试构建配置

🌐 Add the e2e-test build profile

在 eas.json 中添加一个 e2e-test 配置。这个配置会为 Android 创建一个未签名的 APK,并为 iOS 创建一个模拟器构建:

🌐 Add an e2e-test profile to eas.json. This profile creates an unsigned APK for Android and a simulator build for iOS:

eas.json
{ %%placeholder-start%%... %%placeholder-end%% "build": { "e2e-test": { "withoutCredentials": true, "android": { "buildType": "apk", "image": "latest" }, "ios": { "simulator": true, "image": "latest" } } } }

2

添加 Maestro 流程文件

🌐 Add Maestro flow files

在 Expo 项目的根目录下创建一个 .maestro/ 目录,然后添加以下测试文件。

🌐 Create a .maestro/ directory at the root of the Expo project and then add the following test files.

例如,下面的代码片段添加了一个基本的主屏幕测试,它会启动应用并检查“欢迎”文字是否可见。在 appId 中将 com.yourname.yourapp 替换为我们应用在 Android 上的 android.package 或 iOS 上的 ios.bundleIdentifier,两者都在 app.json 中定义:

🌐 For example, the following code snippet adds a basic home screen test that launches the app and checks that the "Welcome" text is visible. Replace com.yourname.yourapp in appId with our app's android.package on Android or ios.bundleIdentifier on iOS, both defined in the app.json:

.maestro/home.yml
appId: com.yourname.yourapp # Replace with your app's package name/bundle identifier from app config file --- - launchApp - assertVisible: 'Welcome'

然后,我们可以为设置屏再加一个测试:

🌐 Then, we can add another test for the settings screen:

.maestro/navigate.yml
appId: com.yourname.yourapp # Replace with your app's package name/bundle identifier from app config file --- - launchApp - tapOn: 'Settings' - assertVisible: 'Settings'

为端到端测试创建一个工作流程

🌐 Create a workflow for E2E tests

1

创建一个 e2e-tests.yml

🌐 Create an e2e-tests.yml

在 .eas/workflows/ 里面,添加一个名为 e2e-tests.yml 的新文件,内容如下:

🌐 Inside .eas/workflows/, add a new file called e2e-tests.yml with the following code:

.eas/workflows/e2e-tests.yml
name: E2E Tests jobs: build_android: name: Build Android for E2E type: build params: platform: android profile: e2e-test build_ios: name: Build iOS for E2E type: build params: platform: ios profile: e2e-test test_android: name: Run Maestro tests (Android) needs: [build_android] type: maestro params: build_id: ${{ needs.build_android.outputs.build_id }} flow_path: ['.maestro/home.yml', '.maestro/navigate.yml'] test_ios: name: Run Maestro tests (iOS) needs: [build_ios] type: maestro params: build_id: ${{ needs.build_ios.outputs.build_id }} flow_path: ['.maestro/home.yml', '.maestro/navigate.yml']

在上面的工作流程中,有两个针对 Android 和 iOS 的构建任务:build_android 和 build_ios。每次构建后,会有一个对应的 Maestro 任务(test_android 或 test_ios)运行该平台的测试。

🌐 In the above workflow, there are two build jobs for Android and iOS: build_android and build_ios. After each build, a corresponding Maestro job (test_android or test_ios) runs the tests for that platform.

每个 Maestro 任务通过 build_id 引用构建,并通过 flow_path 指向 Maestro 流程文件。

🌐 Each Maestro job references the build via build_id and points at the Maestro flow files via flow_path.

2

手动运行工作流

🌐 Run the workflow manually

使用 eas workflow:run 命令手动运行工作流:

🌐 Use the eas workflow:run command to run the workflow manually:

Terminal
- eas workflow:run .eas/workflows/e2e-tests.yml

在 EAS 仪表板上,你会注意到 Android 和 iOS 构建是并行开始的。每个构建完成后,相应的 Maestro 测试任务会对其运行流程文件。

🌐 On the EAS dashboard, notice that the Android and iOS builds start in parallel. Once each build completes, the corresponding Maestro test job runs the flow files against it.

自动触发端到端测试

🌐 Trigger E2E tests automatically

到目前为止,我们都是手动运行这个工作流的。我们可以通过 GitHub 事件触发它,这样它就会在每次拉取请求时运行,或者根据拉取请求标签按需运行。

🌐 So far we've run the workflow manually. We can trigger it from GitHub events so it runs on every pull request or on demand with a pull request label.

在每个拉取请求时运行

🌐 Run on every pull request

要在每个拉取请求上运行端到端测试,在工作流中添加一个 on.pull_request 触发器:

🌐 To run E2E tests on every pull request, add an on.pull_request trigger to the workflow:

.eas/workflows/e2e-tests.yml
on: pull_request: branches: ['*']

按需运行标签

🌐 Run on demand with a label

要仅在需要时运行 E2E 测试,可以使用 on.pull_request_labeled 触发器。它接受一个 labels 参数,并且只有当有人向拉取请求添加这些标签之一时,工作流才会运行。例如,我们可以使用 test 标签来触发我们的 E2E 测试工作流:

🌐 To run E2E tests only when needed, use the on.pull_request_labeled trigger. It accepts a labels parameter, and the workflow only runs when someone adds one of those labels to a pull request. For example, we can use the test label to trigger our E2E tests workflow:

.eas/workflows/e2e-tests.yml
on: pull_request_labeled: labels: ['test']

概括

🌐 Summary

Chapter 4: E2E tests

We created a Maestro E2E test workflow on development builds for Android and iOS, kept the trigger manual by default, and learned how to use a pull request label trigger for on-demand runs.

In the next chapter, learn how to create a production workflow with fingerprinting and OTA updates.

下一个:Chapter 5: Production deployments