ios - Accessing descendant controls in custom UIView using XCTest -


the problem having have reusable views / controls contain text fields. these xib files custom ui view class, such following:

import uikit  @ibdesignable public class customcontrol: uicontrol {    @iboutlet public weak var textfield: uitextfield?   public var contentview: uiview?    required public init?(coder adecoder: nscoder) {     super.init(coder: adecoder)     setupviewfromnib()   }    override public init(frame: cgrect) {     super.init(frame: frame)     setupviewfromnib()   }    override public func awakefromnib() {     super.awakefromnib()     setupviewfromnib()   }    override public func prepareforinterfacebuilder() {     super.prepareforinterfacebuilder()     setupviewfromnib()     contentview?.prepareforinterfacebuilder()   }    func setupviewfromnib() {     guard let view = loadviewfromnib() else { return }     guard let textfield = self.textfield else { return }     view.frame = bounds     view.autoresizingmask = [.flexiblewidth, .flexibleheight]     addsubview(view)     contentview = view   }    func loadviewfromnib() -> uiview? {     let selftype = type(of: self)     let nibname = string(describing: selftype)     return bundle(for: selftype)         .loadnibnamed(nibname, owner: self, options: nil)?         .first as? uiview   } } 

this custom view being loaded storyboards used using storyboard interface builder.

the problem xctest not seem model descendants of these views, when trying write test involves typing text text field part of custom view, test bombs out error:

neither element nor descendant has keyboard focus.

currently work around appears to tap keys on keyboard instead of using typetext method. slower (long pauses between key presses) , more cumbersome test code wise.

the desired code:

let app = xcuiapplication() let view = app.otherelements["customview"] let textfield = view.textfields["textfield"] textfield.tap() textfield.typetext("12345") 

using test recording like:

let app = xcuiapplication() let view = app.otherelements["customview"] view.tap() app.typetext("12345") 

but running test causes aforementioned error.

the edited / working test becomes:

let app = xcuiapplication() let view = app.otherelements["customview"] // view appears leaf no descendants view.tap()  app.keys["1"].tap() app.keys["2"].tap() app.keys["3"].tap() app.keys["4"].tap() app.keys["5"].tap() 

i’m not convinced workaround remain feasible if custom view contain multiple controls, perhaps date of birth control want more granular control on field within custom control using.

is there solution allows me access fields within custom view , potentially use typetext method?

the problem has been solved. advised titouan de bailleul, problem accessibility custom view had been enabled hiding descendant text fields.

added sample project github:

https://github.com/stuartwakefield/xibxctestissuesample

thanks titouan.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -