为啥 XCUITest 在 Xcode 11.4.1 中捕获的流失败

Posted

技术标签:

【中文标题】为啥 XCUITest 在 Xcode 11.4.1 中捕获的流失败【英文标题】:Why XCUITest failing on captured flow in Xcode 11.4.1为什么 XCUITest 在 Xcode 11.4.1 中捕获的流失败 【发布时间】:2020-05-18 06:56:10 【问题描述】:

我在点击一个简单的文本视图并在文本视图中写一些文本时捕获了一个流程。首先,捕获非常慢,有时它也会跳过一些输入。当我尝试使用捕获的流运行测试时,它在 textview 的 tap() 方法上崩溃了

未能获得匹配的快照:没有找到匹配来自输入 ( 其他, 其他, 其他 )。

我已经截取了错误的屏幕截图

这是捕获的代码

谁能告诉我我做错了什么或者我该如何克服这个问题。

let app = XCUIApplication()
let textView = app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 0).children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element(boundBy: 0).children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element(boundBy: 1).children(matching: .other).element(boundBy: 1).children(matching: .textView).element
textView.tap()

  

【问题讨论】:

【参考方案1】:

Xcode 中的录制功能有很多缺陷,但可能对新手有所帮助。

您应该自己编写测试,因为生成的代码很慢。

阅读这些文章以更好地了解如何定位元素并与之交互 https://developer.apple.com/documentation/xctest/xcuielementquery https://developer.apple.com/documentation/xctest/xcuielement

在你的情况下,代码应该是这样的:

let app = XCUIApplication()
let textView = app.textViews.element

textView.tap()
textView.type("Hello, World!")

【讨论】:

【参考方案2】:

有时,录制功能提供的代码有点复杂,甚至会导致 UI 元素无法找到,但从那里获取一些代码然后手动调整它可能是一个好的开始。

生成的代码似乎暗示它认为您的 UI 层次结构很深,对吗?我的意思是,子元素的子元素的多个子元素..?

【讨论】:

以上是关于为啥 XCUITest 在 Xcode 11.4.1 中捕获的流失败的主要内容,如果未能解决你的问题,请参考以下文章