SwiftUI XCUIElement - 如何在屏幕上抓取 XCUIElement?

问题描述 投票:0回答:2

TAKE_A_LOOK_AT_SCREENSHOT_HERE

我遇到了用 SwiftUI 编写的 UI 测试的问题。 我需要在屏幕上抓取一个元素,它是通过以下代码实现的:

HStack {
    Button(action: {
       if value != 0 {
          value -= 1
       }
    }) {
       Image(systemName: "minus.circle")
    }
    Text(String(value))
       .accessibilityIdentifier("person_\(text.lowercased())_count")
    Button(action: {
       value += 1
    }) {
       Image(systemName: "plus.circle")
    }
 }

当我尝试访问 minus.circle 或 plus.circle 时,记录器会向我打印以下代码,例如 minus:

app.windows.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .other)
    .element.children(matching: .button)
    .matching(identifier: "Remove").element(boundBy: 0)

我的问题是

如何减少呼叫量

.children(matching: .other)
或创建另一个正确的路径来访问这些按钮?

我尝试使用:

var minusCircleTeens: XCUIElement { 
    app.windows.children(matching:.other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .other)
        .element.children(matching: .button)
        .matching(identifier: "Remove").element(boundBy: 0)
}

func removeTeens() {      
     minusCircleTeens.tap() 
}

它工作正常,但我想切断通往元素的漫长道路,并以更好的方式做到这一点。有什么想法吗?

swift swiftui xcode-ui-testing xcuitest xcuielement
2个回答
0
投票

只要您的标识符是唯一的,您就可以使用类似的东西

func removeTeens() {      
     app.buttons["Remove"].tap()
}

0
投票

尝试在调试期间运行以下命令:

po XCUIApplication()
这将打印您可以使用的所有 UI 元素。我相信你可以像这样点击按钮图像:
app.images["plus.circle"].tap()
。除非你有更多这样的按钮,否则添加并使用可访问性标识符。

© www.soinside.com 2019 - 2024. All rights reserved.