1

I am trying to delete the previous value from text view,but I am unable to since the cursor is always at the beginning of text view.I am using

extension XCUIElement {
    /**
     Removes any current text in the field before typing in the new value
     - Parameter text: the text to enter into the field
     */
    func clearAndEnterText(text: String) -> Void {
        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 = stringValue.characters.map { _ in XCUIKeyboardKeyDelete }.joinWithSeparator("")

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

source : https://stackoverflow.com/a/32894080/2563133

Community
  • 1
  • 1
psijic
  • 91
  • 2
  • 9

2 Answers2

3

If you don't want to rely on the keyboard and you are not making use of the clipboard, you can cut it:

extension XCUIApplication {
    func clearTextOnElement(_ element: XCUIElement) {
        element.doubleTap()
        menuItems["Select All"].tap()
        menuItems["Cut"].tap()
    }
}

Based on Mr.ConstantChange answer.

rgkobashi
  • 2,551
  • 19
  • 25
2

In your case, it is easy if you double tap on your textfiled and tap on the delete button from key board. Following code should work:

yourTextFieldElement.doubleTap()  
app.keys["delete"].tap()