XCUITest and Today Widget

I have an application with Today Widget. Therefore, I would like to perform some user interface tests.

I found a way to open the Today / Notifications panel. Seems easy:

let statusBar = XCUIApplication().statusBars.elementBoundByIndex(0) statusBar.swipeDown() 

But then I can’t find a way to do something useful. You can register user interface interaction in the Today / Notifications panel, but such a code cannot reproduce my actions.

+5
source share
2 answers

There are similar test extensions. I found that what you need to do is click on the element where it is on the screen, and not the element itself, to control the interaction. I have not tested this with my script, but have not yet found anything impossible with this method.

Here is an example of a Swift pushing the β€œX” button on a Springboard for an application icon that cannot be used in a similar way with a typical interaction:

 let iconFrame = icon.frame // App icon on the springboard let springboardFrame = springboard.frame // The springboard (homescreen) icon.pressForDuration(1.3) // tap and hold // Tap the little "X" button at approximately where it is. The X is not exposed directly springboard.coordinateWithNormalizedOffset(CGVectorMake((iconFrame.minX + 3) / springboardFrame.maxX, (iconFrame.minY + 3) / springboardFrame.maxY)).tap() 

Having received a supervisor frame and a preview, you can calculate where the item should be on the screen. Note that coordinateWithNormalizedOffset accepts a vector in the range [0,1], not a frame or pixel offset. Clicking the element itself on the coordinate does not work either, so you must click on the add-in level / XCUIApplication ().

A more generalized example:

 let myElementFrame = myElement.frame let appFrame = XCUIApplication().frame let middleOfElementVector = CGVectorMake(iconFrame.midX / appFrame.maxX, iconFrame.midY / appFrame.maxY) // Tap element from the app-level at the given coordinate XCUIApplication().coordinateWithNormalizedOffset(middleOfElementVector).tap() 

If you need to access the Springboard layer and go beyond the application, you can do this with

 let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard") springboard.resolve() 

But you will need to open some private XCUITest methods with Objective-C:

 @interface XCUIApplication (Private) { - (id)initPrivateWithPath:(id)arg1 bundleID:(id)arg2; } @interface XCUIElement (Private) { - (void) resolve; } 
+3
source

First you need to open Today View, you can use this method:

  let app = XCUIApplication() // Open Notification Center let bottomPoint = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 2)) app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)).press(forDuration: 0.1, thenDragTo: bottomPoint) // Open Today View let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") springboard.scrollViews.firstMatch.swipeRight() 

Then, to access everything you need, just use springboard , for example:

 let editButton = springboard.buttons["Edit"] 
0
source

Source: https://habr.com/ru/post/1246069/


All Articles