【问题标题】:Xcode 7 UI Testing - can't type text into search barXcode 7 UI 测试 - 无法在搜索栏中输入文本
【发布时间】:2016-01-28 10:17:51
【问题描述】:

以下UI Test 代码将成功点击UISearchBar 元素。出现软件键盘,搜索栏看起来像是有焦点。 (即它的动画就像有人点击它一样)

let searchBar = XCUIApplication().otherElements["accessibility_label"]
searchBar.tap()
searchBar.typeText("search text")

但是,typeText 失败并显示:

UI 测试失败 - 元素和任何后代都没有键盘 重点。元素:

注意: Hardware->Keyboard->Connect Hardware 键盘已关闭。这解决了文本字段的相同问题,但搜索栏仍然失败。

【问题讨论】:

  • 你为什么使用otherElements?你可以试试searchBars吗?
  • 我没有看到 searchBar 元素,但我确实尝试了 searchFields。不幸的是,找不到匹配项。所以我使用 UI 记录器和 XCode 用 otherElements 识别元素。似乎是我可以访问它的唯一方法。我猜这不正常?哈哈。您是否能够使用 searchFields 命令访问 UISearchBar?
  • 当您尝试通过不同的方法查找元素时会发生什么?喜欢staticTexts()elementBoundByIndex()?这是模拟 iPad 还是 iPhone?
  • 也遇到了这个问题。我必须从 UISearchBar 中删除可访问性,然后我才能使用 XCUIApplication().typeText() 输入(当搜索视图控制器出现时它已经有了焦点)。

标签: swift xcode7 xctest xcode-ui-testing


【解决方案1】:

我使用以下(Swift 4,Xcode 9.4.1):

app.searchFields["Placeholder"].clearAndEnterText("String to search")
app.buttons["Search"].tap()

... 其中“Placeholder”是在输入文本之前出现在文本字段中的字符串。

注意:clearAndEnterText 是在 XCUIElement 扩展中创建的函数:

    func clearAndEnterText(_ text: String)
    {
        guard let stringValue = self.value as? String else {
            XCTFail("Tried to clear and enter text into a non string value")
            return
        }

        self.tap()

        let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: stringValue.count)

        self.typeText(deleteString)
        self.typeText(text)
    }

希望对你有帮助。

【讨论】:

    【解决方案2】:

    它对我有用

    searchBar.accessibilityTraits = UIAccessibilityTraitSearchField
    

    在视图控制器中,然后在 UI 测试中通过

    let searchfield = app.searchFields.element(boundBy: 0) 
    searchfield.typeText("foobar")
    

    【讨论】:

      【解决方案3】:

      只需等待几秒钟,它就会起作用!

      let searchBar = XCUIApplication().otherElements["accessibility_label"]
      searchBar.tap()
      sleep(5)
      searchBar.typeText("search text")
      

      【讨论】:

        【解决方案4】:

        我发现了一些东西:

        let app = XCUIApplication()
        app.tables.searchFields["Search"].tap()
        app.searchFields["Search"].typeText("something")
        

        看起来很奇怪,但对我有用。

        【讨论】:

        • 但是如果我想使用可访问标识符来识别 searchField 怎么办?因为它在我们提供多语言支持时会引起问题。
        • 以上内容仅用于可访问标识符。Search 是 searchField 的可访问标识符。
        【解决方案5】:

        我成功使用了以下解决方法:

        let firstCell = app.cells.elementBoundByIndex(0)
        let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
        let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 3))
        start.pressForDuration(0, thenDragToCoordinate: finish)
        
        app.searchFields.element.tap()
        app.searchFields.element.typeText("search text")
        

        这可以有效地将搜索栏滚动到视图中并点击它来聚焦,之后可以使用typeText()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-10
          • 2016-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多