Skip to content

Commit 33f5183

Browse files
graycreateclaude
andcommitted
fix: improve UI test reliability by replacing sleep with proper wait
Addresses Copilot review comments: - Replace sleep() with waitForExistence() for element detection - Use XCTWaiter for timed assertions instead of sleep - Remove unnecessary sleep(10) for visual inspection 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2e4fbb5 commit 33f5183

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

V2erUITests/V2erUITests.swift

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,28 @@ class V2erUITests: XCTestCase {
3636
let app = XCUIApplication()
3737
app.launch()
3838

39-
// Wait for feed to load
40-
sleep(6)
41-
42-
// Find a feed item by looking for text that says "评论" (comment count)
39+
// Wait for feed to load by waiting for the first comment label to appear
4340
let commentLabels = app.staticTexts.matching(NSPredicate(format: "label CONTAINS '评论'"))
44-
guard commentLabels.count > 0 else {
45-
XCTFail("No feed items found (no comment labels)")
46-
return
47-
}
41+
let firstCommentLabel = commentLabels.element(boundBy: 0)
42+
XCTAssertTrue(firstCommentLabel.waitForExistence(timeout: 15), "Feed should load and show at least one comment label")
4843

4944
// Tap on the first comment label's parent area
50-
let firstCommentLabel = commentLabels.element(boundBy: 0)
51-
XCTAssertTrue(firstCommentLabel.waitForExistence(timeout: 10), "Comment label should exist")
5245
firstCommentLabel.tap()
5346

54-
// Wait for navigation animation
55-
sleep(3)
56-
57-
// Verify we're on detail page - look for "话题" text or "发表回复" placeholder
47+
// Wait for navigation animation by waiting for detail page element to appear
5848
let topicLabel = app.staticTexts["话题"]
5949
let replyPlaceholder = app.textViews.firstMatch
50+
let appeared = topicLabel.waitForExistence(timeout: 5) || replyPlaceholder.waitForExistence(timeout: 5)
51+
XCTAssertTrue(appeared, "Detail page did not appear after tapping comment label")
6052

61-
let onDetailPage = topicLabel.exists || replyPlaceholder.exists
62-
63-
XCTAssertTrue(onDetailPage, "Should be on detail page after navigation")
64-
65-
// Wait longer to verify page stays open (the original bug was page dismissing)
66-
sleep(3)
53+
// Wait to verify page stays open (the original bug was page dismissing immediately)
54+
let expectation = XCTestExpectation(description: "Wait 3 seconds to verify detail page stays open")
55+
let result = XCTWaiter.wait(for: [expectation], timeout: 3.0)
56+
XCTAssertEqual(result, .timedOut, "Expected to wait full 3 seconds")
6757

6858
// Verify still on detail page
6959
let stillOnDetail = topicLabel.exists || replyPlaceholder.exists
7060
XCTAssertTrue(stillOnDetail, "Detail page should remain open after 3 seconds")
71-
72-
// Leave app open for visual inspection
73-
sleep(10)
7461
}
7562

7663
func testLaunchPerformance() throws {

0 commit comments

Comments
 (0)