Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Action Extension with UI Tests

Is it possible to perform UI tests on Action Extension targets? I am unable to create a UI testing target with the Action Extension as the "Target to be Tested." I am trying to load the Action Extension from within Safari (or Photos, although Safari/both is prefered)

If I record my interactions I can get as far as:

app.icons["Safari"].tap()

I can then manually add:

XCUIDevice.sharedDevice().pressButton(.Home)

before the generated code, but it doesn't work as expected (the simulator is left on the home screen).

I have also tried:

UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!)

but that also doesn't open Safari.

I'm not even sure if I'll be able to interact in an automated with with the Action Extension if it does get launched, but hopefully it'll be possible.

like image 697
Joseph Duffy Avatar asked Dec 21 '15 12:12

Joseph Duffy


1 Answers

So, it's possible to switch apps with XCUITest, but it's undocumented. If you check out Facebook's WebDriverAgent, they did a header dump and made a helper for launching from the springboard. You can call:

XCUIApplication* safari = [[XCUIApplication alloc] initWithPrivatePath:nil bundleID:@"com.apple.safari"];

[safari launch];

And then interact with Safari just like you do your app. However, I've run into a similar problem where XCUITest won't actually launch the extension itself. Once open (i.e. you tap physically on the extension button while the test is running), the test runner works perfectly, and you can interact with your extension in the same context as your app. However, having the test runner tap to launch the extension does nothing. I've also got an Apple Dev Forum question going on this topic.

Update:

It turns out if you use the app to press the screen at the location of the button, the extension will load and you can interact with it! Note that the API for tapping a coordinate is very wonky. The x, y are multipliers of the frame of the thing that you created the coordinate from. Relevant sample code:

// app is your XCUIApplication
// In this case we are tapping in the horizontal middle and at the y coordinate 603 (this is for a 6+ screen size)
XCUICoordinate* coordinateOfRowThatLaunchesYourExtension = [app coordinateWithNormalizedOffset:CGVectorMake(0.5, 603.0 / 736.0)];
[coordinateOfRowThatLaunchesYourExtension tap];

This will press the button for your extension in the action sheet, after Apple's extension picker has been invoked. For whatever reason / bug in XCUITest simply pressing your app in the action sheet doesn't work:

[app.sheets.staticTexts[@"MyApp"] tap];
like image 197
Chase Holland Avatar answered Sep 28 '22 05:09

Chase Holland