1

I'm trying to test deep linking of my app using iOS XCTests. To do so I've used the code detailed in several webs:

    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
    safari.launch()
    safari.textFields["Address"].tap()
    safari.textFields["Address"].typeText("https://web-to-test-deep-link")
    safari.keyboards.buttons["Go"].tap()

With this code:

  • Safari opens (ok)
  • My website is written in the address bar (ok)
  • The Go button is pressed (ok)
  • Then, above Safari it appears the Open with app banner. This banner mentions MyApp, a text saying "Open in the MyApp app" and the OPEN button".

Then I've tried to add the code to click on this button so my app is open, but I can't do it. I've tried:

safari.buttons["Open"].tap()

without success, the open button is not found and the test fails.

I've used the Accessibility inspector and taking into account the attributes of this banner I've tried this:

safari.buttons["MyApp, Open in the MyApp app, OPEN"].tap()

without success too.

How could I know the code to click on this banner and open my app with the deep link?

EDIT: I've tried this too (without success):

    safari.scrollViews
            .otherElements
            .webViews["WebView"]
            .children(matching: .other)
            .element
            .children(matching: .other)
            .element(boundBy: 1)
            .buttons["Open"]
            .tap()

EDIT 2: Answering drunkencheetah question, safari.debugDescription is very large, but the part corresponding to the Open banner is this:

→Application, 0x104f175b0, pid: 8168, label: 'Safari'
    Window (Main), 0x104f117e0, {{0.0, 0.0}, {375.0, 812.0}}
      Other, 0x104f28010, {{0.0, 0.0}, {375.0, 812.0}}
        Other, 0x104f11c90, {{0.0, 0.0}, {375.0, 812.0}}
          Other, 0x104f11b40, {{0.0, 0.0}, {375.0, 812.0}}, identifier: 'SafariWindow?View=Narrow&BarsKeptMinimized=false&UUID=.....&SupportsTabBar=false&UsingLoweredBar=true&UsingUnifiedBar=false'
            Other, 0x104f28120, {{0.0, 0.0}, {375.0, 812.0}}
              Other, 0x104f10190, {{0.0, 0.0}, {375.0, 812.0}}
                Other, 0x104f102a0, {{0.0, 0.0}, {375.0, 44.0}}
                  Other, 0x104f31b70, {{0.0, 0.0}, {375.0, 44.0}}
                    Other, 0x104f31c80, {{0.0, 0.0}, {375.0, 44.0}}
                    Other, 0x104f31d90, {{0.0, 0.0}, {375.0, 44.0}}
                      Other, 0x104f31ea0, {{0.0, 43.7}, {375.0, 0.3}}
                ScrollView, 0x104f31fb0, {{0.0, 0.0}, {375.0, 812.0}}
                  Other, 0x104f320c0, {{0.0, 0.0}, {375.0, 812.0}}, identifier: 'TabDocument?IsLoadedUsingDesktopUserAgent=false&InReaderMode=false&UUID=.....&IsPrivate=false&IsPageLoaded=true&WebViewProcessID=8169'
                    WebView, 0x104f321d0, {{0.0, 0.0}, {375.0, 812.0}}, identifier: 'WebView'
                      WebView, 0x104f322e0, {{0.0, 0.0}, {375.0, 812.0}}
                        Button, 0x104f323f0, {{0.0, 44.0}, {375.0, 45.7}}, label: 'MyApp, Open in the MyApp app, OPEN'
                        Other, 0x104f32500, {{342.0, 89.7}, {30.0, 589.3}}, label: 'Vertical scroll bar, 15 pages', value: 0%
                        Other, 0x104f32610, {{0.0, 646.0}, {375.0, 30.0}}, label: 'Horizontal scroll bar, 1 page', value: 0%
                  Other, 0x104f32720, {{31.3, 779.0}, {312.3, 30.0}}, label: 'Horizontal scroll bar, 1 page', value: 0%

I can see there is a button "Button, 0x104f323f0, {{0.0, 44.0}, {375.0, 45.7}}, label: 'MyApp, Open in the MyApp app, OPEN'", maybe I'm accessing it with a wrong path?

EDIT 3: After drunkencheetah suggestion of check safari.debugDescription I've finally managed to detect the button with this:

safari.webViews.buttons["MyApp, Open in the FASHIONALIA app, MyApp"].tap()

Now the test detect the button, perform the tap but I expected my app to open (as it happens if I manually tap the button). But the app is not opened, should I do it programmatically in my test code or something like that? I though it would be automatic.

Wonton
  • 1,033
  • 16
  • 33
  • For me your code worked flawlessly with `safari.buttons["Open"].tap()` although I had to change `safari.textFields["Address"].tap()` on my iOS version to `safari.otherElements["Address"].tap()`. But the alert button was found the first time. Can you provide the Safari elements when the alert is on screen? `safari.debugDescription` – drunkencheetah Jun 08 '22 at 09:16
  • Thanks @drunkencheetah, This confuse me a lot. I've added to my post the safari.debugDescription. – Wonton Jun 08 '22 at 13:06
  • Is this a simulator or a real device? Which iOS version? The debug description definitely looks weird to me, first time I see a button reported like this in the hierarchy. On my 14.5 sim it is completely different. Also you assume correct, tapping Open should directly open the app without you handling it(you need to have it installed already). But in your hierarchy you don't even have Cancel which is wrong. I haven't tested deep links in a while but for our app they definitely don't start with 'https', you sure this is correct? – drunkencheetah Jun 08 '22 at 13:43
  • I'm testing with a real device, an iPhone X with iOS 15.3.1. I'm not shure if I shuold see a Cancel button. When I try to open the link it doesn't show a popup, it only shows a banner, just below the status bar, with my app name and the Open button. I'm going to try with an emulator. – Wonton Jun 08 '22 at 14:16

0 Answers0