Xcode UI testing - Uiviews added with addSubview a

2020-06-05 00:59发布

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:

enter image description here

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:

enter image description here

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?

2条回答
女痞
2楼-- · 2020-06-05 01:55

Hello I craete a sample project with tableView and your container

and add container to tableView

Try this example :)

app.tables.staticTexts["labelIdentifier"]

enter image description here

查看更多
▲ chillily
3楼-- · 2020-06-05 02:01

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"

查看更多
登录 后发表回答