ios – Xcode UI测试如何在自定义类文本字段中键入文本

前端之家收集整理的这篇文章主要介绍了ios – Xcode UI测试如何在自定义类文本字段中键入文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的项目中使用 Xcode开始UI测试,遇到障碍.

UITextField的子类名为RSCustomTextField,它有一些UI自定义.这是添加RSSearchHeadView,其中包含文本字段和按钮.

所以我无法将文本字段查询为app.textfields [“搜索字段”],因为它是一个自定义类,它显示为app.otherElements [“搜索字段”]

它在searchField.typeText(“abc”)上崩溃,显示错误.无论是元素还是任何后代都没有键盘焦点.

甚至在元素上点击()之后.注意,我也尝试在子类和文本字段上设置辅助功能特性为UIAccessibilityTraitSearchField但没有区别.

func testSearchIcon() {

  let searchField = app.otherElements["Search field"]    
  searchField.tap()

  searchField.typeText("abc")
  app.keys["delete"].tap()

  XCTAssertFalse(searchDubizzleElement.hittable)
}

在app上打印debugDescription会提供以下输出

Element subtree:
 →Application 0x7fc1b3f3aa00: {{0.0,0.0},{375.0,667.0}},label: 'myappname-masked'
    Window 0x7fc1b3f439b0: Main Window,{{0.0,667.0}}
      Other 0x7fc1b3f55c20: traits: 8589934592,667.0}}
        Other 0x7fc1b3f43440: traits: 8589934592,667.0}}
          Other 0x7fc1b3f31830: traits: 8589934592,667.0}}
            Other 0x7fc1b3f61ff0: traits: 8589934592,667.0}}
              Other 0x7fc1b3f52820: traits: 8589934592,64.0}}
                Other 0x7fc1b3f4e190: traits: 8589934592,64.0}}
                  Button 0x7fc1b3f3a250: traits: 8589934593,{{5.0,20.0},{44.0,44.0}},label: 'search icon right'
                  Other 0x7fc1b3f349b0: traits: 8589935616,{{15.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,label: 'your account icon'
              CollectionView 0x7fc1b3f32450: traits: 35192962023424,64.0},603.0}}
                Cell 0x7fc1b3f1e230: traits: 8589934592,50.0}}

......

解决方法

我遇到了与UITextField的自定义子类相同的错误.该问题的根源是在UITextField上以编程方式插入leftView.有关使用这些子视图属性的信息会破坏自动UI测试的可访问性访问.

为了解决这个问题,我避免在UITextFields上使用leftView或rightView.相反,我通过修改textRect方法来实现所需的缩进,如下面的答案之一(目前是第二个最受欢迎的)中所述:Set padding for UITextField with UITextBorderStyleNone

我无法从您的代码中判断出您的问题是否相同,但我想我会分享这个小小的发现.

猜你在找的iOS相关文章