Xcode UI Testing How to type text into custom class text fields
Asked Answered
B

2

9

Starting UI Testing with Xcode on my project, hit a road block.

There is a subclass of UITextField called RSCustomTextField which has some UI customizations. Which is added to RSSearchHeadView, that contains the text field and button.

So Im not able to query the textfield as app.textfields["Search field"] since its a custom class, it shows as app.otherElements["Search field"]

It crashes on searchField.typeText("abc") displaying error Neither element nor any descendant has keyboard focus..

Even after tap() on the element. Note, I also tried setting accessibility trait on the subclass and the textfield as UIAccessibilityTraitSearchField yet no difference.

func testSearchIcon() {

  let searchField = app.otherElements["Search field"]    
  searchField.tap()

  searchField.typeText("abc")
  app.keys["delete"].tap()

  XCTAssertFalse(searchDubizzleElement.hittable)
}

Printing debugDescription on app gives the following output:

Element subtree:
 →Application 0x7fc1b3f3aa00: {{0.0, 0.0}, {375.0, 667.0}}, label: 'myappname-masked'
    Window 0x7fc1b3f439b0: Main Window, {{0.0, 0.0}, {375.0, 667.0}}
      Other 0x7fc1b3f55c20: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
        Other 0x7fc1b3f43440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
          Other 0x7fc1b3f31830: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x7fc1b3f61ff0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
              Other 0x7fc1b3f52820: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
                Other 0x7fc1b3f4e190: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
                  Button 0x7fc1b3f3a250: traits: 8589934593, {{5.0, 20.0}, {44.0, 44.0}}, label: 'search icon right'
                  Other 0x7fc1b3f349b0: traits: 8589935616, {{15.0, 20.0}, {345.0, 39.0}}, label: 'Search field'
                  Image 0x7fc1b3f3b3b0: traits: 8589934596, {{139.0, 22.0}, {97.0, 30.0}}, identifier: 'logo'
                  Button 0x7fc1b3f537a0: traits: 8589934593, {{326.0, 20.0}, {44.0, 44.0}}, label: 'your account icon'
              CollectionView 0x7fc1b3f32450: traits: 35192962023424, {{0.0, 64.0}, {375.0, 603.0}}
                Cell 0x7fc1b3f1e230: traits: 8589934592, {{0.0, 64.0}, {375.0, 50.0}}

......
Basseterre answered 22/4, 2016 at 10:3 Comment(10)
I just tried to subclass a UITextField adding it to a storyboard and debugDescription shows the element to be a TextField. Is RSCustomTextField really just a subclass of UITextField?Cutoff
Have you tried "app.searchFields.element" ?Fukien
@TomasCamin you are right. it wasn't directly added to the view controller. It was inside another custom view called RSSearchHeadView which simply holds the textField custom class a button. I have updated the question. my bad. Any help?Basseterre
@Fukien the app.searchFields.count is 0Basseterre
@Basseterre Check my other answer here: https://mcmap.net/q/145044/-xcode-ui-tests-can-39-t-find-views-that-are-added-programmaticallyCutoff
@Basseterre did anyone ever find the answer to this or a possible work around? I am having the same problem.Dang
@JenelEjercitoMyers tried everything but there was no solution I could find. I had to reduce the nesting on the textview to access it as a work around. If you find something please answer here for future readers.Basseterre
@Basseterre thanks. I'll let you know. Also, we should probably file a bugDang
@JenelEjercitoMyers Yes we should.Basseterre
I am having the same issue. Any solution yet?Abrahamsen
E
0

I ran into the same error with a custom subclass of UITextField. The source of the issue turned out to be the programmatic insertion of a leftView on the UITextField. Something about using these subview properties mucks up the accessibility access of the automated UI Tests.

To work around this, I am avoiding the use of leftView or rightView on UITextFields. Instead I am achieving the desired indentation by modifying the textRect methods as described in one of the answers (currently the second most upvoted) here: Set padding for UITextField with UITextBorderStyleNone

I can't tell from your code whether your issue is the same, but thought I would share this little discovery.

Elyot answered 3/4, 2017 at 21:21 Comment(0)
S
0

I met same problem some time ago. I found workaround, but it was ugly. The basic tap() method from XCTest didn't put focus on element, but if you tap just on coordinates of element's center, it will get focus.

let coordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))        
coordinate.withOffset(CGVector(dx: self.frame.midX, dy: self.frame.midY)).tap()

After this you will be able to type some text in this textfield.

Shutdown answered 12/9, 2021 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.