将光标放在UITest下UITextView的末尾

时间:2016-07-22 09:38:46

标签: ios swift uitextfield uitextview xcode-ui-testing

这是我在UITests中清除UITextFieldsUITextViews的方式。

extension XCUIElement {

   func clear() {

      tap()

      while (value as! String).characters.count > 0 {
         XCUIApplication().keys["delete"].tap()
      }
   }
}

使用示例:

descriptionTextView.type("Something about Room.")
descriptionTextView.clear()

如果我运行UITests,它总是点击UITextView的开头。

如何点击最后?

2 个答案:

答案 0 :(得分:5)

您可以点击右下角将光标放在文本视图的末尾。

此外,您可以通过准备一个deleteString来提高删除速度,该XCUIKeyboardKeyDelete包含一次擦除整个文本字段的extension XCUIElement { func clear() { guard let stringValue = self.value as? String else { XCTFail("Tried to clear and enter text into a non string value") return } let lowerRightCorner = self.coordinateWithNormalizedOffset(CGVectorMake(0.9, 0.9)) lowerRightCorner.tap() let deleteString = [String](count: stringValue.characters.count + 1, repeatedValue: XCUIKeyboardKeyDelete) self.typeText(deleteString.joinWithSeparator("")) } }

E/OTPlugin: subscriber exception: Internal error -- WebRTC subscriber error., stream id: xxxxxxxx-xxxx-xxxx-xxxx-06afdb1e6504

答案 1 :(得分:0)

这是Tomas Camin适用于Swift 5.3(Xcode 12)的解决方案:

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

        let lowerRightCorner = self.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.9))
        lowerRightCorner.tap()

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