Xcode 7 UI Testing - can't type text into search bar
Asked Answered
G

5

16

The following UI Test code will successfully tap the UISearchBar element. The software keyboard appears and the search bar looks like it has focus. (ie. it animates as if someone tapped it)

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

However, typeText fails with:

UI Testing Failure - Neither element nor any descendant has keyboard focus. Element:

Note: Hardware->Keyboard->Connect Hardware Keyboard is toggled off. This solved the same issue for text fields but the search bar is still failing.

Goose answered 28/10, 2015 at 14:58 Comment(4)
Why are you using otherElements? Can you try with searchBars?Crawl
I don't see a searchBar element but I did try searchFields. Unfortunately no matches could be found. So I used the UI recorder and XCode identified the element with otherElements. Seems to be the only way I can access it. I'm guessing that's abnormal? Haha. Have you been able to access a UISearchBar with the searchFields command?Goose
What happens when you try to find the element by different methods? Like staticTexts() or elementBoundByIndex()? Is this simulating iPad or iPhone?Morphosis
Just hit this problem too. I had to remove the accessibility from the UISearchBar and then I was able to use XCUIApplication().typeText() to type into (it already has focus when the search view controller appears).Audile
M
14

I found something:

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

It looks strange, but it works for me.

Malayoindonesian answered 17/5, 2016 at 16:14 Comment(2)
but what if i want to identify searchField using accessible identifier? Because it causing issue while we providing multi-language support.Ankylosis
The above has been used using the accessible identifier only.. The Search is the accessible identifier for searchField.Incursion
M
5

it worked for me to set

searchBar.accessibilityTraits = UIAccessibilityTraitSearchField

in the view controller and then access it in the UI test by

let searchfield = app.searchFields.element(boundBy: 0) 
searchfield.typeText("foobar")
Murex answered 12/7, 2018 at 13:15 Comment(0)
J
3

I used the following workaround successfully:

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")

This effectively scrolls the search bar into view and taps it to focus, after that typeText() can be used.

Jerlenejermain answered 1/3, 2016 at 14:55 Comment(0)
G
2

I use the following (Swift 4, Xcode 9.4.1):

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

... where "Placeholder" is the string that appears in the textfield before typing the text.

Note: clearAndEnterText is a function created in a XCUIElement extension:

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

Hope this can help you.

Gird answered 10/8, 2018 at 6:42 Comment(0)
H
-1

Just put a wait for few seconds and it will work!

let searchBar = XCUIApplication().otherElements["accessibility_label"]
searchBar.tap()
sleep(5)
searchBar.typeText("search text")
Hilltop answered 21/4, 2017 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.