How to catch flaky iOS tests before your team stops trusting them
Bắt test iOS chập chờn trước khi team hết tin vào test
A flaky test passes and fails on the same code. One flaky test is an annoyance; five of them and people start rerunning CI until it goes green, and real bugs slip through.
The usual suspects
- Timing: tapping before a screen finishes loading or animating.
- Fragile selectors: matching on text that changes (“Good morning” vs “Good evening”) or on screen position.
- Popups that sometimes appear: permission dialogs, “Save password?”, rating prompts.
- Shared state: a test that depends on what the previous one left behind.
Let the history tell you
AppFlight saves every run, from the Run button, the AI chat or a workflow. Test Run → History shows the pass rate over the last 10 runs and marks a test Flaky when its results flip between pass and fail. It also names the step that fails most, which is usually where the fix belongs.
Typical fixes
- Replace fixed sleeps with Wait visible or Wait for animations to end.
- Prefer accessibility ids; for changing labels use Text Contains or Text One Of.
- Wrap optional popups in Only If… element is visible.
- Start each test from a known state with a shared Login flow instead of relying on the previous test.
Fix the step the history points at, run the test a few times, and watch the badge disappear.
Test chập chờn là test lúc pass lúc fail trên cùng một đoạn code. Một test chập chờn thì khó chịu; năm test như vậy thì mọi người bắt đầu bấm chạy lại CI tới khi xanh, và lỗi thật lọt qua.
Thủ phạm quen thuộc
- Thời gian: chạm khi màn hình chưa tải xong hoặc còn đang chạy hiệu ứng.
- Selector dễ gãy: khớp theo chữ hay đổi (“Good morning” và “Good evening”) hoặc theo toạ độ.
- Popup lúc có lúc không: hộp xin quyền, “Lưu mật khẩu?”, lời mời đánh giá.
- Trạng thái dùng chung: test phụ thuộc vào thứ test trước để lại.
Để lịch sử chạy lên tiếng
AppFlight lưu mọi lần chạy, từ nút Run, AI chat hay workflow. Test Run → History hiện tỉ lệ pass của 10 lần gần nhất và gắn nhãn Flaky khi kết quả lúc pass lúc fail. Nó còn chỉ ra bước hay lỗi nhất, thường là chỗ cần sửa.
Cách sửa thường gặp
- Thay chờ cố định bằng Wait visible hoặc Wait for animations to end.
- Ưu tiên accessibility id; với nhãn hay đổi dùng Text Contains hoặc Text One Of.
- Bọc popup không chắc xuất hiện trong Only If… element is visible.
- Cho mỗi test bắt đầu từ trạng thái biết trước bằng flow Login dùng chung, thay vì dựa vào test trước.
Sửa đúng bước mà lịch sử chỉ ra, chạy lại vài lần, và nhìn nhãn Flaky biến mất.