我正在爲我的應用程序編寫UI測試。 如何在更新屏幕後刷新列表app.buttons
/app.otherElements
?在屏幕更新後刷新應用程序的XCUIElementQuery
例如: 我在登錄屏幕上按登錄按鈕,等待服務器響應。 響應到達後,我看到另一個屏幕,需要按下它,但button.exists == NO
。
每當屏幕更新時,我應該創建新的應用程序實例([[XCUIApplication alloc] init]
)嗎?
我正在爲我的應用程序編寫UI測試。 如何在更新屏幕後刷新列表app.buttons
/app.otherElements
?在屏幕更新後刷新應用程序的XCUIElementQuery
例如: 我在登錄屏幕上按登錄按鈕,等待服務器響應。 響應到達後,我看到另一個屏幕,需要按下它,但button.exists == NO
。
每當屏幕更新時,我應該創建新的應用程序實例([[XCUIApplication alloc] init]
)嗎?
您應該等待登錄操作完成,通過等待登錄屏幕上的唯一元素不存在存在。
XCUIElement *logInButton = app.buttons["signInButton"];
// Tap the log in button
[logInButton tap];
// Wait for the login operation to complete
NSPredicate *notExistsPredicate = [NSPredicate predicateWithFormat:@"exists == false"];
[self expectationForPredicate:notExistsPredicate evaluatedWithObject:logInButton handler: nil];
[self waitForExpectationsWithTimeout:10.0 handler: nil]; // Adjust timeout as needed
這將確保在屏幕內容更改時刷新XCUIApplication上可用的視圖層次結構。
您應該授予您在登錄accessibilityIdentifier後想要點按的按鈕,然後等待其存在。這可以確保您在新UI加載之前不會嘗試與您的應用進行交互,而不僅僅是舊UI已消失。
我強烈建議根據Joe Masilotti的優秀指南,將「等待這個元素出現」功能提取到測試助手中,可以找到here(儘管您必須在ObjC中重新實現它們,因爲他在Swift中編寫它們)。