返回提示词目录第 1311 页

移动端界面自动化测试编写

针对移动页面与操作流程编写独立测试,补充稳定标识、数据桩和持续集成运行配置。

完整提示词

<context>
The user is a mobile engineer or QA engineer adding UI tests for one screen and flow on {{platform}}. Mobile UI suites rot for predictable reasons: selectors bound to visible text or view hierarchy that change with copy and layout, fixed sleeps that are too short on CI emulators and too long everywhere else, tests that depend on a real backend and on state left by the previous test, and system dialogs (permissions, notifications, keyboard) that appear on one device and not another.

Use the platform's own tools: XCUITest for iOS; Espresso (with Compose testing APIs for Compose) for Android; Detox or Maestro for React Native; `integration_test` with `flutter_test` finders for Flutter. Name another option only if the code shows the team already uses it.
</context>

<task>
<screen_code>
{{screen_code}}
</screen_code>

<flow>
{{flow}}
</flow>

1. Plan the cases: the main path of the flow, then each state the screen can show (loading, empty, error, offline, success), and one input validation case. Keep it to the 3-7 tests that would catch real regressions.
2. Add stable identifiers: `accessibilityIdentifier` (iOS), `testTag` or resource IDs (Android), `testID` (React Native), `Key` values (Flutter). Do not select by display text unless the text itself is the behaviour under test; never by index or deep hierarchy.
3. Replace every wait with a condition: XCTest expectations or `waitForExistence(timeout:)`, Espresso idling resources or Compose `waitUntil`, Detox `waitFor().toBeVisible().withTimeout()`, Flutter `pumpAndSettle` or pumping until a finder matches (with a cap). No `sleep`, `Thread.sleep` or fixed delays.
4. Add test-only hooks: launch arguments or environment (`launchArguments`, instrumentation arguments, build flavour, `--dart-define`) that reset storage, log in a seeded user, set locale and time zone, disable animations and point the network layer at a stub (local stub server or injected fake client) with fixtures per state. Keep hooks out of release builds.
5. Handle system interruptions explicitly: pre-grant permissions where the tool allows it, otherwise an interruption handler; dismiss the keyboard deliberately.
6. Write a thin screen-object layer: one object per screen exposing user actions and assertions in domain words (`login.submit(email:password:)`), hiding identifiers.
7. Write the tests: one behaviour each, independent and runnable in any order, each starting from a known launch state.
8. Give the CI command and settings: device or emulator image and OS version, animations off, retries off by default, screenshots or recordings and logs kept on failure.

If the code does not show how the screen gets its data, ask how the network layer is created (so it can be stubbed) and stop.
</task>

<constraints>
- No fixed sleeps anywhere. No real production backend.
- Every identifier the tests use must be added in the screen code shown; list each change.
- Tests must not depend on order or on another test's state.
- Do not invent APIs of the user's app; mark assumed names as [ASSUMED].
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Test plan
Table: test | state or path | why it matters.
## Identifiers to add
Code changes to the screen, as a diff or snippets.
## Test hooks
Launch arguments, stub setup and fixtures, and how they stay out of release builds.
## Screen objects
Code.
## Tests
Code.
## Running in CI
Command, device or emulator, settings and failure artefacts.
</output_format>

可替换内容

  • {{platform}}
  • {{screen_code}}
  • {{flow}}
  • {{> guardrails/scope-discipline}}
  • [ASSUMED]

分类与标签

编程开发、移动端测试、界面自动化、测试数据桩、持续集成

前往 AI 图片创作