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

2

4

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?

Landscape answered 22/6, 2016 at 15:37 Comment(3)
I have the same problem. Only seems to happen when you add elements in a container view to a table view. Thinking the solution is to swap from a container view to a cell...Eliezer
Yeah, but unfortunately some cool animation libraries make use of table header to their stuff and you don't have any other option....Finale
Why are you adding the container as a subview of tableView? Why not add container as a subview of the parent view of tableView?Subaxillary
S
2

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

Selwyn answered 22/6, 2016 at 17:0 Comment(6)
I've already tried that and it didn't work for me. What version on Xcode are you using for the test? I have uploaded a test project to GitHub (GitHub project) that is using that code and It doesn't pass the test...Finale
@Landscape my Xcode version is 7.3.1 please changelet final = XCUIApplication().staticTexts["I am a label"] to let final = XCUIApplication().staticTexts["labelIdentifier"] it work :)Selwyn
I had already tested that option @SwiftyCruz, but I've updated Xcode to 7.3.1 versión (I had 7.3.0) and tested again just in case, but the results are the same. Are you sure you are adding the view with the "addSubView" method in your tests? Have you tested my GitHub respository test project?Finale
I test one more your GitHub repository , if set "self.tableView.tableHeaderView = container" then app.staticTexts["labelIdentifier"] it works but if set "self.tableView.addSubview(container)" then this not work. so I printed app's tables descendants then output isSelwyn
" Output: { Table 0x7fa86a680b90: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}, label: 'I am a label' }" that means if you addSubView label to tableView then table view has that label like a description of tableView because if add one more label text "I am a label2" to tableView then printed "Output: { Table 0x7f8b53092320: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}, label: 'I am a label2, I am a label' }" if test one label "let final = app.tables.elementBoundByIndex(0) , final.label is equal to 'I am a label' "Selwyn
That's it @SwiftyCruz, when you add the label with an "addSubview(container)" the label is unreachable through its identifier as it becomes more a property of the table than a label itself and that's the reason of my question... Is this behaviour a bug? If it’s not, how can I reach a label added to a table with an "addSubview" to check its label value? (that's why I want to access through its identifier and not its value)Finale
H
0

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"

Hispanicism answered 22/2, 2017 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.