How to Open Url in Safari and the Get Back to the App Under Uitests in Xcode 7

How to open url in Safari and the get back to the app under UITests in Xcode 7?

Starting in iOS 11 you can interact with other applications using the XCUIApplication(bundleIdentifier:) initializer.

To get back to your app you'd do something like:

let myApp = XCUIApplication(bundleIdentifier: "my.app.bundle.id")
let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")

// Perform action in your app that opens Safari

safari.wait(for: .runningForeground, timeout: 30)
myApp.activate() // <--- Go back to your app

Is it possible to testing uitest (XCUI) when open url external browser?

Absolutely. Safari is an XCUIApplication just like any other with a bundle identifier of com.apple.mobilesafari.

To check the URL you'll tap() the URL field (it's a button when a page loads) and read the value of the URL field (at this point it's a textField). Once you're done asserting that, activate() your app and you'll be back in it (note: my tests finish after asserting so I don't have to do this, but it's the published method - you could always enter debug and find how to tap the button to return to your application in the top-left of the screen if this doesn't work).

I'm happy to provide exact code if you show me you've tried this and can't get it working, but it's pretty straightforward XCUI automation.

XCUITest get URL value

I created a small project that loading google.com in an SFSafariViewController on launch. When I run UI tests against this app and prints the debug description of the window, I get the following:

(lldb) po XCUIApplication().windows.descendants(matching: .any)
t = 29.26s Use cached accessibility hierarchy for com.tito.Safari
t = 29.27s Find: Descendants matching type Window
t = 29.27s Find: Descendants matching type Any
Find: Target Application 0x6000000afa20
Output: {
Application 0x6000001684c0: {{0.0, 0.0}, {375.0, 667.0}}, label: 'Safari'
}
↪︎Find: Descendants matching type Window
Output: {
Window 0x600000169a80: Main Window, {{0.0, 0.0}, {375.0, 667.0}}
Window 0x60000016a8c0: {{0.0, 0.0}, {375.0, 667.0}}
Window 0x60000016fb40: {{0.0, 0.0}, {375.0, 667.0}}
Window 0x60000016c780: {{0.0, 0.0}, {375.0, 667.0}}
Window 0x600000169e40: {{-0.0, -0.0}, {375.0, 667.0}}
}
↪︎Find: Descendants matching type Any
Output: {
Button 0x608000169900: traits: 8589934593, {{9.5, 22.0}, {43.0, 34.0}}, label: 'Done'
Other 0x608000165b80: traits: 8589935104, {{68.5, 24.5}, {295.0, 29.0}}, identifier: 'URL', label: 'Address', value: 10%
Button 0x608000169840: traits: 8589934593, {{336.5, 24.5}, {29.0, 29.0}}, identifier: 'StopButton', label: 'stop'
Button 0x600000168100: traits: 146028888065, {{66.5, 24.5}, {299.0, 29.0}}, identifier: 'URL', label: 'Address', value: ‎google.com
Other 0x6080001696c0: traits: 8589934592, {{0.0, 20.0}, {375.0, 44.0}}
WebView 0x608000167ec0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x608000167e00: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x608000169b40: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x608000167a40: traits: 8589934592, {{0.0, 622.5}, {375.0, 0.5}}
Button 0x6080001675c0: traits: 8589934849, {{0.5, 625.0}, {42.0, 40.0}}, label: 'Back'
Button 0x600000167bc0: traits: 8589934849, {{104.0, 625.0}, {42.0, 40.0}}, label: 'Forward'
Button 0x600000167c80: traits: 8589934593, {{207.5, 624.0}, {51.0, 40.0}}, label: 'Share'
Button 0x60000016fa80: traits: 8589934593, {{320.5, 625.5}, {55.0, 40.0}}, label: 'Open in Safari'
Toolbar 0x608000167b00: traits: 35192962023424, {{0.0, 623.0}, {375.0, 44.0}}
Other 0x608000165ac0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x608000169780: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x608000169480:
Other 0x608000169180: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x60000016cc00: traits: 8589934592, {{0.0, 64.0}, {24.0, 603.0}}
Other 0x6080001690c0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x600000168400: {{0.0, 0.0}, {375.0, 667.0}}
Other 0x600000169180: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x600000169f00:
Other 0x60000016a680: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x600000167b00: {{0.0, 0.0}, {375.0, 20.0}}
Other 0x60000016fd80: traits: 8388608, {{6.0, 0.0}, {39.0, 20.0}}
Other 0x60000016e340: traits: 8388608, {{50.0, 0.0}, {13.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
Other 0x60000016fe40: traits: 8389120, {{161.0, 0.0}, {57.0, 20.0}}, label: '10:33 AM'
Other 0x60000016e880: traits: 8388608, {{337.0, 0.0}, {33.0, 20.0}}, label: '-100% battery power'
Other 0x60000016dc80: {{0.0, 0.0}, {375.0, 20.0}}
StatusBar 0x600000169240: {{0.0, 0.0}, {375.0, 20.0}}
Other 0x60000016a440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
Other 0x600000170140:
Other 0x60000016da40: traits: 8589934592, {{-0.0, -0.0}, {375.0, 667.0}}
Other 0x60000016c600:
Other 0x60000016af80: traits: 8589934592, {{-0.0, -0.0}, {375.0, 667.0}}
}

You can see that the element that contain the value google.com is seen as a Button that the Accessibility framework.

Button 0x600000168100: traits: 146028888065, {{66.5, 24.5}, {299.0, 29.0}}, identifier: 'URL', label: 'Address', value:

It seems wrong but it is how it is and there's nothing you can do about that. The way to access this element's value would be the following

XCUIApplication().buttons['URL'].value!

The only problem is that it's returns the following

‎gen.xyz, secure and validated connection

So the last thing to do is to parse this and you'll get the address

let value = XCUIApplication().buttons["URL"].value as! String
let address = value.components(separatedBy: ",").first!

This is definitely hacky though and might change in the future if Apple decides to apply their own advices to their own products when it comes to Accessibility.

Open universal links (deep links) with XCUITest

  1. Set safari as app like this

    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")

  2. Open your email in safari

  3. Tap on the link
  4. Normally assert some element on app

XCTest UI Testing - How to close and open an app without relaunch?

As of Xcode 9 and iOS 11, XCUIApplication() has an activate() method that you can use to relaunch the app.

As brandenbyers suggested, you can "press" the home button to background your app, and then activate it again like this to avoid using Siri:

XCUIDevice.shared.press(.home)
XCUIApplication().activate()

Note that this only works with targets built using XCUITest, not XCTest. If you try this within a target built from XCTest, all XCUIApplication operations will crash.

How to find button with predicate under UITests in Xcode7?

When finding elements via predicates you must use the XCUIElementAttributes Protocol. For this example, I don't think title will actually work, but try using label (which should map to accessibilityLabel).

For some reason the %@ format option doesn't seem to work in Swift. Also note the extra single quotes around "Reorder 1".

let predicate = NSPredicate(format: "label BEGINSWITH[cd] 'Reorder 1'")
let button = app.buttons.elementMatchingPredicate(predicate)

UI Testing - wait for dialog then tap Ok button and check controller

waitforExpectationsWithTimeout() only calls the completion handler when the timeout is exceeded. Simply move your controller assertion beneath the asynchronous wait call.

let alert = app.alerts["Error"]
let exists = NSPredicate(format: "exists == 1")

expectation(for: exists, evaluatedWith: alert, handler: nil)
waitForExpectations(timeout: timeout, handler: nil)

alert.buttons["Ok"].tap()
XCTAssertEqual(app.navigationBars.element.identifier, "RegistrationViewController")


Related Topics



Leave a reply



Submit