Can not access the programatically added UIView in XCTest

2k views Asked by At

Below is my recorded XCTest

    let app = XCUIApplication()
    let tablesQuery = app.tables
    tablesQuery.staticTexts["Video"].tap()
    tablesQuery.staticTexts["\tWindowed"].tap()
    app.buttons["Launch"].tap()
    app.buttons["Popout Video"].tap()
    app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()

When I am trying to run the test the last part that is:

    app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()

is not accessible. It does not throw any error but the last line of code is not executed.

I have tried solving the issue by referring to the following stackoverflow question : Xcode UI Tests can't find views that are added programatically

Also , I have referred to the following Apple Documentations: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW2

https://developer.apple.com/reference/uikit/uiview

But all these doesn't seem to solve the issue. Any help will be greatly appreciated.

1

There are 1 answers

2
Oletha On BEST ANSWER

The line in question is very brittle. It's possible that your app's views are not always available in the same order every time the app launches, depending on race conditions, so what was recorded in your recording session does not necessarily work for all launches of the app. You'll probably find that the code is actually running, but isn't tapping the element you expected.

To make your code less brittle, add an accessibility identifier to the UIView in your app code, and use the otherElements query to find the UIView.

// app code
let view: UIView!
view.accessibilityIdentifier = "myAccessibilityIdentifier"

// UI test code
app.otherElements["myAccessibilityIdentifier"].tap()