问题描述
在我的项目上开始使用 Xcode 进行 UI 测试,遇到了障碍.
Starting UI Testing with Xcode on my project, hit a road block.
UITextField
有一个名为 RSCustomTextField
的子类,它有一些 UI 自定义.添加到 RSSearchHeadView
中,其中包含文本字段和按钮.
There is a subclass of UITextField
called RSCustomTextField
which has some UI customizations. Which is added to RSSearchHeadView
, that contains the text field and button.
所以我无法将文本字段查询为 app.textfields["Search field"]
因为它是一个自定义类,它显示为 app.otherElements["Search field"]代码>
So Im not able to query the textfield as app.textfields["Search field"]
since its a custom class, it shows as app.otherElements["Search field"]
它在 searchField.typeText("abc")
显示错误时崩溃 元素和任何后代都没有键盘焦点..
It crashes on searchField.typeText("abc")
displaying error Neither element nor any descendant has keyboard focus..
即使在元素上的 tap()
之后.请注意,我还尝试将子类和文本字段的可访问性特征设置为 UIAccessibilityTraitSearchField
但没有区别.
Even after tap()
on the element. Note, I also tried setting accessibility trait on the subclass and the textfield as UIAccessibilityTraitSearchField
yet no difference.
func testSearchIcon() {
let searchField = app.otherElements["Search field"]
searchField.tap()
searchField.typeText("abc")
app.keys["delete"].tap()
XCTAssertFalse(searchDubizzleElement.hittable)
}
在应用程序上打印 debugDescription
会得到以下输出:
Printing debugDescription
on app gives the following output:
Element subtree:
→Application 0x7fc1b3f3aa00: {{0.0, 0.0}, {375.0, 667.0}}, label: 'myappname-masked'
Window 0x7fc1b3f439b0: Main Window, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x7fc1b3f55c20: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x7fc1b3f43440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x7fc1b3f31830: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x7fc1b3f61ff0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x7fc1b3f52820: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
Other 0x7fc1b3f4e190: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
Button 0x7fc1b3f3a250: traits: 8589934593, {{5.0, 20.0}, {44.0, 44.0}}, label: 'search icon right'
Other 0x7fc1b3f349b0: traits: 8589935616, {{15.0, 20.0}, {345.0, 39.0}}, label: 'Search field'
Image 0x7fc1b3f3b3b0: traits: 8589934596, {{139.0, 22.0}, {97.0, 30.0}}, identifier: 'logo'
Button 0x7fc1b3f537a0: traits: 8589934593, {{326.0, 20.0}, {44.0, 44.0}}, label: 'your account icon'
CollectionView 0x7fc1b3f32450: traits: 35192962023424, {{0.0, 64.0}, {375.0, 603.0}}
Cell 0x7fc1b3f1e230: traits: 8589934592, {{0.0, 64.0}, {375.0, 50.0}}
......
推荐答案
我前段时间遇到了同样的问题.我找到了解决方法,但它很难看.XCTest 的基本 tap() 方法没有把焦点放在元素上,但是如果你只点击元素中心的坐标,它就会得到焦点.
I met same problem some time ago. I found workaround, but it was ugly.The basic tap() method from XCTest didn't put focus on element, but if you tap just on coordinates of element's center, it will get focus.
let coordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
coordinate.withOffset(CGVector(dx: self.frame.midX, dy: self.frame.midY)).tap()
在此之后,您将能够在此文本字段中输入一些文本.
After this you will be able to type some text in this textfield.
这篇关于Xcode UI 测试如何在自定义类文本字段中键入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!