Using URL Schema in XCTestCase

I want to run a test that is awaiting a browser response.

The test target has an Info.plist where I can register my own URL schemes. But they are never called. I know that the test target is not a real application.

Is there any way?

EDIT (for generosity): I want to write an integration test for a class that calls openUrl("tel://" + some phone number) . How to subscribe to this URL scheme in XCTestCase?

+8
objective-c xcode swift macos xctest
source share
2 answers

The question has been changed to OS X, so my answer is no longer valid

Original answer

It's impossible. The main reason is that the tests are associated with one running instance of the application, they have no control over the device, they work as part of the application. You can probably open the URL, but you will not be able to return to the application and complete the approval.

Also note that XCUnit is a unit . You cannot write an advanced integration test into it. You may have done better in UI Automation, but this particular test case will be very difficult even for him. For example, to open a link with your application in the simulator, you can create an HTML page that will be redirected to the link, and then use the shell for exec open testLink.html -a "iOS Simulator" . However, in my humble opinion, such tests are unreliable and very difficult to write and debug.

My advice:

  • Write a unit test that mocks [UIApplication openURL:] and make sure you pass it the correct URL. Or if you want a different direction, write unit test, which will call the UIApplicationDelegate methods in the called order, simulating the opening of a link by your application. This is as far as possible with XCUnit.
  • Check the rest manually.
+1
source share

I'm not sure what exactly you are trying to verify. I guess this is more than opening or not a URL. The test case below checks to see if FaceTime will open and can open the URL. If the application you are testing registers a user schema, you just need to change pathToTestApplication .

enter image description here

 class NSURL_SchemeTests: XCTestCase { func testWhoHandlesSchemeTel() { let testAppURL = NSURL(string: pathToTestApplication) // create a NSURL for the test let tel = "tel:5555555555" let url = NSURL(string: tel) XCTAssert(url != nil, "Could not create a URL from \(tel)") // determine whether any application is registered to handle the URL let workspace = NSWorkspace.sharedWorkspace() let urlHandler = workspace.URLForApplicationToOpenURL(url!) XCTAssert(urlHandler != nil, "No application found to open URL \(tel)") // determine whether the expected test application is registered to handle the URL XCTAssert(urlHandler! == testAppURL, "Application \(pathToTestApplication) was not registered to handle URL \(tel)") // attempt to open the URL XCTAssert(workspace.openURL(url!), "Could not open URL \(tel)") } } 
-one
source share

All Articles