在 EAS 工作流和 Maestro 上运行端到端测试
了解如何使用 Maestro 在 EAS 工作流上设置和运行端到端测试。
在本指南中,你将学习如何使用 Maestro 在 EAS 工作流上运行端到端 (E2E) 测试。示例演示了如何使用 默认 Expo 模板 配置你的 E2E 测试工作流。对于你自己的应用,你需要调整流程以匹配应用的界面。
🌐 In this guide, you'll learn how to run end-to-end (E2E) tests on EAS Workflows using Maestro. The example demonstrates how to configure your E2E tests workflow using the default Expo template. For your own app, you'll need to adjust the flows to match your app's UI.
1
设置你的项目
🌐 Set up your project
如果你还没有这样做,请创建一个新项目并将其与 EAS 同步。
🌐 If you haven't already, create a new project and sync it with EAS.
请按照 使用 EAS 工作流入门指南 创建一个新项目并将其与 EAS 同步。然后,配置你的项目 并关联你的 GitHub 仓库。
🌐 Follow the Get started with EAS Workflows guide to create a new project and sync it with EAS. Then, configure your project and link your GitHub repository.
2
添加示例 Maestro 测试用例
🌐 Add example Maestro test cases
这是使用默认 Expo 模板创建的应用的界面样子:
🌐 This is what the UI of the app created from the default Expo template looks like:
让我们为示例应用创建两个简单的 Maestro 流程。首先,在项目目录的根目录下创建一个名为 .maestro 的文件夹。该文件夹将包含你将要配置的流程,并且应与 eas.json 位于同一层级。
🌐 Let's create two simple Maestro flows for the example app. Start by creating a directory called .maestro in the root of your project directory. This directory will contain the flows you'll configure and should be at the same level as eas.json.
在其中,创建一个名为 home.yml 的新文件。这个流程将启动应用,并断言在主屏幕上可以看到“Welcome!”这段文字。
🌐 Inside, create a new file called home.yml. This flow will launch the app and assert that the text "Welcome!" is visible on the home screen.
appId: dev.expo.eastestsexample # This is an example app id. Replace it with your app id. --- - launchApp - assertVisible: 'Welcome!'
接下来,创建一个名为 expand_test.yml 的新流程。该流程将在示例应用中打开“探索”屏幕,点击“基于文件的路由”可折叠部分,并断言屏幕上可见文本“此应用有两个屏幕。”
🌐 Next, create a new flow called expand_test.yml. This flow will open the "Explore" screen in the example app, click on the "File-based routing" collapsible, and assert that the text "This app has two screens." is visible on the screen.
appId: dev.expo.eastestsexample # This is an example app id. Replace it with your app id. --- - launchApp - tapOn: 'Explore.*' - tapOn: '.*File-based routing' - assertVisible: 'This app has two screens.*'
3
在本地运行 Maestro 测试(可选)
🌐 Run Maestro tests locally (optional)
要在本地运行 Maestro 测试,请按照 安装 Maestro 中的说明安装 Maestro CLI。
🌐 To run Maestro tests locally, install the Maestro CLI by following the instructions in Installing Maestro.
在本地 Android 模拟器或 iOS 模拟器上安装你的应用。打开终端,导航到 Maestro 目录,并运行以下命令以使用 Maestro CLI 启动测试:
- maestro test .maestro/expand_test.yml- maestro test .maestro/home.yml下方视频展示了 .maestro/expand_test.yml 流程的成功运行:
🌐 The video below shows a successful run of the .maestro/expand_test.yml flow:
4
为端到端测试建立配置文件
🌐 Build profile for E2E tests
端到端测试需要一个已构建的应用文件:Android 的 .apk 或 iOS 的 .app —— EAS 可以在模拟器/模拟器上安装并测试该文件。
🌐 E2E tests require a built app file: .apk for Android or .app for iOS — that EAS can install and test on an emulator/simulator.
在你的 eas.json 文件中,为 E2E 测试创建一个构建配置。如果文件不存在,运行 eas build:configure 来生成它。
🌐 In your eas.json file, create a build profile for E2E tests. If the file doesn't exist, run eas build:configure to generate it.
{ "build": { "e2e-test": { "withoutCredentials": true, "ios": { "simulator": true }, "android": { "buildType": "apk" } } } }
上述构建配置会为 Android 创建 .apk 文件,为 iOS 创建 .app 文件。工作流程使用此配置在 EAS 服务器上构建应用。
🌐 The above build profile creates an .apk for Android and an .app for iOS. The workflow uses this profile to build the app on EAS servers.
5
创建一个端到端测试工作流
🌐 Create an E2E test workflow
在你的项目根目录下,创建一个 .eas/workflows 目录。然后,为你的端到端测试工作流添加一个 YAML 文件,例如 .eas/workflows/e2e-test-android.yml。
🌐 At the root of your project, create an .eas/workflows directory. Then, add a YAML file for your E2E test workflow, such as .eas/workflows/e2e-test-android.yml.
name: e2e-test-android on: pull_request: branches: ['*'] # Run the E2E test workflow on every pull request. jobs: build_android_for_e2e: type: build params: platform: android profile: e2e-test # your eas build profile for E2E test maestro_test: needs: [build_android_for_e2e] type: maestro params: build_id: ${{ needs.build_android_for_e2e.outputs.build_id }} flow_path: ['.maestro/home.yml', '.maestro/expand_test.yml']
此工作流程使用上一步的 e2e-test 构建配置为 Android 构建 .apk。然后,它在构建好的 APK 上运行 .maestro/home.yml 流程。
🌐 This workflow builds an .apk for Android using the e2e-test build profile from the previous step. Then it runs the .maestro/home.yml flow on the built APK.
这是一个针对 iOS 的相同测试工作流程示例:
🌐 Here's an example of the same test workflow for iOS:
name: e2e-test-ios on: pull_request: branches: ['*'] jobs: build_ios_for_e2e: type: build params: platform: ios profile: e2e-test # your eas build profile for E2E test maestro_test: needs: [build_ios_for_e2e] type: maestro params: build_id: ${{ needs.build_ios_for_e2e.outputs.build_id }} flow_path: ['.maestro/home.yml', '.maestro/expand_test.yml']
了解更多关于 EAS 工作流程语法 的信息。
🌐 Learn more about Syntax for EAS Workflows.
6
运行 E2E 测试工作流程
🌐 Run the E2E test workflow
你可以通过两种方式运行 E2E 测试工作流程:
🌐 You can run the E2E test workflow in two ways:
- 手动使用 EAS CLI
- npx eas-cli@latest workflow:run .eas/workflows/e2e-test-android.yml- 当你打开拉取请求时自动
该工作流使用 pull_request 触发器,当有人向你的仓库提交拉取请求时会自动运行。了解有关 EAS 工作流触发器 的更多信息。
🌐 The workflow uses a pull_request trigger to run automatically when someone opens a pull request to your repository. Learn more about EAS Workflow triggers.
工作流启动后,你可以在 EAS 仪表板中跟踪其进度并查看结果。以下是已完成工作流运行的截图:
🌐 After the workflow starts, you can track its progress and view the results in the EAS dashboard. Here's a screenshot of a completed workflow run:
更多
🌐 More
了解有关 EAS 工作流程语法的更多信息。
了解有关 EAS 工作流示例 CI/CD 流程的更多信息。
了解更多关于 Maestro 流程以及如何编写它们的信息。