2

At one point in my tests I need to interact with views that are inside Main Window. When I do a po app.windows, I get this:

Find: Target Application 0x1c40d7680
  Output: {
    Application, 0x1c4389170, pid: 4765, {{0.0, 0.0}, {375.0, 667.0}}, label: 'Mercedes PRO [testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x1c43890a0, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438dc30, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438dea0, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438e6c0, Main Window, {{0.0, 0.5}, {375.0, 667.0}}
    }

I need to query Main Window since I have almost the same views in the first Window from this list, so I want to separate them. So, I tried querying it with app.windows["Main Window"], but it seems like Main Window is not an identifier for a window view.

Printing all XCUIElementAttributes (like title, value, identifier and so on) didn't give me much information (they are mainly empty strings). Also, I don't want to query it by position (like this: app.windows.element(boundBy: 3)) since I'm not sure if this Window will always be at this position.

Is there another way to query Main Window?

Mladen
  • 2,070
  • 1
  • 21
  • 37

2 Answers2

1

What I was trying to achieve is to put an identifier to the window object. I tried adding it in the ViewController that shows views I'm trying to query with this line of code (in viewDidLoad()):

UIApplication.shared.delegate?.window??.accessibilityIdentifier = "Main Window"

And I kept getting identifier in the wrong place:

Find: Target Application 0x60c0000c1260
  Output: {
    Application, 0x60c00018c640, pid: 33496, {{0.0, 0.0}, {414.0, 736.0}}, label: '[testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x60c00018c710, {{0.0, 0.0}, {414.0, 736.0}}, identifier: 'Main Window'
      Window, 0x60c000191370, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c0001915e0, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191c60, Main Window, {{0.0, 0.5}, {414.0, 736.0}}
    }

Then I found a place in the code where developers created a new window that they used to hold that VC.

let actionVC = UIStoryboard.findViewController(withIdentifier: "ActionViewController")
if let appWindow = UIApplication.shared.delegate?.window {
     let window = UIWindow.init(frame: appWindow.frame)
     window.rootViewController = actionVC
     window.accessibilityIdentifier = "Main Window"
}

This allowed me to write a query like this: app.windows["Main Window"] and to be sure that I'm targeting the real Main Window.

Find: Target Application 0x60c0000c1260
  Output: {
    Application, 0x60c00018c640, pid: 33496, {{0.0, 0.0}, {414.0, 736.0}}, label: '[testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x60c00018c710, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191370, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c0001915e0, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191c60, Main Window, {{0.0, 0.5}, {414.0, 736.0}}, identifier: 'Main Window'
    }
Mladen
  • 2,070
  • 1
  • 21
  • 37
0

Best way I found was filtering the windows using their frame size.

let window = app.windows.allElementsBoundByIndex.first { element in
                element.frame.width > 10
            }!

I will edit this if I find a better way.

osrl
  • 8,168
  • 8
  • 36
  • 57
  • This solution won't work. In this case (since the width of all 4 windows in the list is always greater than 10; you can see it in the logs), this is essentially the same as writing this `app.windows.element(boundBy: 0)`. And I want to avoid using that method. What would work in this situation is this condition (since `Main Window`'s origins are `{0.0, 0.5}`): `element.frame.origin.y > 0`. I still didn't have problems with relying on finding window element by index, so I may leave my solution untouched. – Mladen Aug 28 '18 at 11:17
  • Yes you are right. My windows' sizes were 0x0 or 2x2. So that worked for me. I know that's not a good solution thoug – osrl Aug 28 '18 at 11:39