【问题标题】:Xcode UI Testing How to type text into custom class text fieldsXcode UI 测试如何在自定义类文本字段中输入文本
【发布时间】:2016-08-15 21:43:11
【问题描述】:

在我的项目中使用 Xcode 开始 UI 测试,遇到了障碍。

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

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

它在 searchField.typeText("abc") 显示错误时崩溃元素和任何后代都没有键盘焦点。。

即使在元素上的tap() 之后。请注意,我还尝试将子类和文本字段的可访问性特征设置为UIAccessibilityTraitSearchField,但没有区别。

func testSearchIcon() {

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

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

  XCTAssertFalse(searchDubizzleElement.hittable)
}

在应用程序上打印debugDescription 会得到以下输出:

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}}

......

【问题讨论】:

  • 我只是尝试将 UITextField 子类化并将其添加到情节提要中,debugDescription 将元素显示为 TextField。 RSCustomTextField 真的只是 UITextField 的子类吗?
  • 你试过“app.searchFields.element”吗?
  • @TomasCamin 你是对的。它没有直接添加到视图控制器中。它位于另一个名为RSSearchHeadView 的自定义视图中,它仅包含textField 自定义类一个按钮。我已经更新了这个问题。我的错。有什么帮助吗?
  • @Che app.searchFields.count 是 0
  • @carbonr 在这里查看我的其他答案:stackoverflow.com/a/36789510/574449

标签: ios swift xcode7 xcode-ui-testing


【解决方案1】:

我在 UITextField 的自定义子类中遇到了同样的错误。问题的根源原来是在 UITextField 上以编程方式插入 leftView。使用这些子视图属性会破坏自动化 UI 测试的可访问性。

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

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

【讨论】:

    【解决方案2】:

    我前段时间遇到了同样的问题。我找到了解决方法,但它很难看。 XCTest 的基本 tap() 方法没有将焦点放在元素上,但如果你只点击元素中心的坐标,它将获得焦点。

    let coordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))        
    coordinate.withOffset(CGVector(dx: self.frame.midX, dy: self.frame.midY)).tap()
    

    在此之后,您将能够在此文本字段中输入一些文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2015-12-25
      • 2017-01-11
      相关资源
      最近更新 更多