Does XCUIElement represent a query rather than a specific element?

This example is pretty contrived, but it illustrates the behavior. I know you can use .accessibilityIdentifier to uniquely identify a control, but I'm just trying to better understand the interplay between XCUIElement and XCUIElementQuery.

Let's say you have an app like this:

import SwiftUI

struct ContentView: View {
    @State var showRedButton = true

    var body: some View {
        VStack {
            if showRedButton {
                Button("Click me") {
                    showRedButton = false
                }
                .background(.red)
            }
            else {
                HStack {
                    Button("Click me") {
                        showRedButton = true
                    }
                    .background(.blue)
                    Spacer()
                }
            }
        }
    }
}

And you are UI testing like this:

import XCTest

final class MyAppUITests: XCTestCase {
    func testExample() throws {
        let app = XCUIApplication()
        app.launch()
        
        print(app.debugDescription)
        
        // At this point, the Element subtree shows 
        // a single Button:
        // Button, 0x14e40d290, 
        // {{162.3, 418.3}, {65.3, 20.3}}, label: 'Click me'
        
        let btn = app.buttons["Click me"]

        // This tap makes the red button disappear 
        // and shows the blue button
        btn.tap()

        print(app.debugDescription)
        
        // Now, the Element subtree shows a single Button 
        // that has a different ID
        // and different x-y coordinates:
        // Button, 0x15dc12e50, 
        // {{0.0, 418.3}, {65.3, 20.3}}, label: 'Click me'
        
        // This tap now works on the blue button?? 
        // Without requerying?
        btn.tap() 
        
        print(app.debugDescription)

        // The red button reappears, 
        // but with a different ID (which makes sense).
    }
}

Why does the second tap work, even though it's a different control? This must mean that SwiftUI is automatically re-running the XCUIElementQuery to find the button that matches "Click me". Apparently the variable btn isn't linked to the control with the ID 0x14e40d290. Does this mean XCUIElement actually represents an XCUIElementQuery?

I expected it to require me to explicitly re-run the query like this,

btn = app.buttons["Click me"]

prior to running the 2nd tap, or the tap would've said that btn was no longer available.

The final print of the Element subtree shows that the red button has a different ID now. This makes sense, because when SwiftUI redraws the red button, it's not the same instance as the last time. This is explained well in the WWDC videos. Nevertheless, at the moment I connected the variable "btn" to the control, I thought there was a tighter affiliation. Maybe UI testing has to behave this way because SwiftUI redraws controls so frequently?

Does XCUIElement represent a query rather than a specific element?
 
 
Q