gpt4 book ai didi

ios - 在 UI 测试中找到 UIRefreshControl

转载 作者:行者123 更新时间:2023-12-01 16:21:51 28 4
gpt4 key购买 nike

我想在 UI 测试中测试 UIRefreshControl 是否存在。我定义我的控件所有 init:

itemRefreshControl.accessibilityIdentifier = "MyRefreshIndicator"
// allow UITest to find the refresh
if let refreshLabel = itemRefreshControl.subviews.first?.subviews.last as? UILabel {
refreshLabel.isAccessibilityElement = true
refreshLabel.accessibilityIdentifier = "MyRefreshLabel"
}

然后在我的测试用例中我尝试了:

let refreshCtlQuery = NSPredicate(format: "label CONTAINS[c] 'Refreshing'")
let refreshControl = app.staticTexts.containing(refreshCtlQuery)
expectation(for: exists, evaluatedWith: refreshControl, handler: nil)
start.press(forDuration: 0, thenDragTo: finish)
print(app.debugDescription)
waitForExpectations(timeout: 5, handler: nil)

我也试过:

let refreshControl = app.staticTexts["MyRefreshLabel"]

我试过了:

let refreshControl = app.activityIndicators["MyRefreshIndicator"]

在所有这些情况下,我都可以看到测试运行器执行拖动并且我在 UI 中看到刷新控件,但预期总是失败。它几乎就像测试 block ,直到刷新完成,然后检查是否存在,但它不存在。当我打印出 View 层次结构时,找不到 UIRefreshControl 的标签。我怎样才能最好地测试它?

最佳答案

确实,当 UIRefreshControl 执行其动画时,测试会挂断并说“等待 空闲”

您可以将 XCUIApplicationProcess.waitForQuiescenceIncludingAnimationsIdle: 调整为一个空方法,这样您就可以绕过此行为(基于 this answer )。

extension XCTestCase {

static var disabledQuiescenceWaiting = false

/// Swizzle `XCUIApplicationProcess.waitForQuiescenceIncludingAnimationsIdle(:)`
/// to empty method. Invoke at `setUpWithError()` of your test case.
func disableQuiescenceWaiting() {

// Only if not disabled yet.
guard Self.disabledQuiescenceWaiting == false else { return }

// Swizzle.
if
let `class`: AnyClass = objc_getClass("XCUIApplicationProcess") as? AnyClass,
let quiescenceWaitingMethod = class_getInstanceMethod(`class`, Selector(("waitForQuiescenceIncludingAnimationsIdle:"))),
let emptyMethod = class_getInstanceMethod(type(of: self), #selector(Self.empty))
{
method_exchangeImplementations(quiescenceWaitingMethod, emptyMethod)
Self.disabledQuiescenceWaiting = true
}
}

@objc func empty() {
return
}
}

然后调用测试用例的设置。

override func setUpWithError() throws {
disableQuiescenceWaiting()
}

你可以直接标记UIRefreshControl(为了方便我做了这个扩展)。

extension UIRefreshControl {

func testable(as id: String) -> UIRefreshControl {
self.isAccessibilityElement = true
self.accessibilityIdentifier = id
return self
}
}

// Create the instance like this.
UIRefreshControl().testable(as: "RefreshControl")

因此您可以很好地断言存在(从 otherElements 获取)。

let refreshControlElement = app.otherElements["RefreshControl"]        
XCTAssertTrue(refreshControlElement.waitForExistence(timeout: 1))

关于ios - 在 UI 测试中找到 UIRefreshControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537178/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com