0

I am unsure why but no matter what I try, this doesn't seem to delete the entire text in the text field even when I inspect it and see the same number of delete characters as there are characters to delete. It's seemingly random and deletes different amounts of characters each iteration.


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) {
        guard let stringValue = self.value as? String else {
            XCTFail("Tried to clear and enter text into a non string value")
            return
        }


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

        self.typeText(deleteString)
        self.typeText(text)
    }
}
  • You can easily just do. `self.typeText.text = ""` – Putte Dec 13 '19 at 09:18
  • @Putte Thanks.But, it doesn't work. It appends "" to the existing string in the textfield. – gayathri dhanuskodi Dec 13 '19 at 09:25
  • So, why are you using the XCUIElement? What do you get from that? I just realized the typeText() is an inbuilt attribute, right? – Putte Dec 13 '19 at 09:33
  • I got this code from the link https://stackoverflow.com/questions/32821880/ui-test-deleting-text-in-text-field – gayathri dhanuskodi Dec 13 '19 at 09:42
  • Sometimes it deletes all the characters as expected, but sometimes it leaves 2 or 3 chars undeleted. – gayathri dhanuskodi Dec 13 '19 at 09:44
  • Ok. So what's the idea of your app? What are you trying to accomplish with it? Also, from the link you provided there is a comment in the answered post from Thomas Bak: `"let deleteString = stringValue.characters.map { _ in "\u{8}" }.joinWithSeparator("")"` Try that one. – Putte Dec 13 '19 at 09:44
  • Current version doesn't have "joinwithseparator". My task is like I have to search a name in search field in a loop. Each and every time I have to clear the search field and enter the next name for a search – gayathri dhanuskodi Dec 13 '19 at 09:51
  • I've never used this extension before, so I can't really answer on that one sorry. Good luck! :) – Putte Dec 13 '19 at 09:56
  • Thanks. Could you please suggest any swift developers? – gayathri dhanuskodi Dec 13 '19 at 09:58

1 Answers1

0
/*!  * Sends a tap event to a central point computed for the element. 
*/

- (void)tap;

/*!  * Sends a double tap event to a central point computed for the element.  */
- (void)doubleTap;

So if you send tap but text is longer than central point of control you will delete only left-part from the tapped point.

Try to use instead doubleTap, which as expected must select all text (exception, when selection will be disabled by some reason).

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Sorry! I have edited the code part now. I am not using tap() functionality at all. My project is for appleTv and it doesn't have tap functionality. Without tap itself, I was able to clear the complete text sometimes. Note : I am passing the same text for search all the time – gayathri dhanuskodi Dec 13 '19 at 11:37