Xcode UI testing - Uiviews added with addSubview are completely invisible to the UI tests

左心房为你撑大大i 提交于 2019-12-09 07:16:23

问题


I've been giving a try to the new UI tests on XCode 7.3 and I've found what it seems a bug to me.

The problem is that views added through the "addSubview" method seems to be completely invisibles to the UI test system.

I have this view:

And this UIview creating code:

    let container = UIView(frame: CGRectMake(0, 0, 375, 200))
    container.backgroundColor = UIColor.orangeColor()
    container.isAccessibilityElement = false
    let label = UILabel(frame: CGRectMake(0, 100, 375, 20))
    label.backgroundColor = UIColor.yellowColor()
    label.textAlignment = NSTextAlignment.Center
    label.accessibilityIdentifier = "labelIdentifier"
    label.text = "I am a label"
    container.addSubview(label)

And this simple UI test:

func testExample() {

    let final  = XCUIApplication().staticTexts.containingType(.StaticText, identifier: "labelIdentifier").element
    XCTAssertEqual(final.label, "I am a label")
}

The problem is that, depending on how I attach the orange view, the test doesn't find the label. If I do a:

    self.tableView.tableHeaderView = container

Test pass without any problem at all but attaching it with:

    self.tableView.addSubview(container)

Probokes the next error:

After digging a little in the forums I've already tested all recommended settings like setting to false the container "isAccessibilityElement" property and such, but nothing seems to work.

Long story made short. ¿Anyone have tried to get an element attached to other UIView with the add "addSubview" method?


回答1:


Hello I craete a sample project with tableView and your container

and add container to tableView

Try this example :)

app.tables.staticTexts["labelIdentifier"]




回答2:


Have you trying Xcode recording to get that view ? Just record and press on your view it should works

Also make sure that this UILabel is not outbounds "you can inspect using Xcode"



来源:https://stackoverflow.com/questions/37972615/xcode-ui-testing-uiviews-added-with-addsubview-are-completely-invisible-to-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!