Replicate pull to refresh in XCTest UI testing

戏子无情 提交于 2019-12-03 05:29:12
Joe Masilotti

You can use the XCUICoordinate API to interact with specific points on the screen, not necessarily tied to any particular element.

  1. Grab a reference to the first cell in your table. Then create a coordinate with zero offset, CGVectorMake(0, 0). This will normalize a point right on top of the first cell.
  2. Create an imaginary coordinate farther down the screen. (I've found that a dy of six is the smallest amount needed to trigger the pull-to-refresh gesture.)
  3. Execute the gesture by grabbing the first coordinate and dragging it to the second one.

let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
start.pressForDuration(0, thenDragToCoordinate: finish)

More information along with a working sample app is also available.

I managed to do such tasks with coordinates like Joe suggested. But i went a step further using coordinateWithOffset()

let startPosition = CGPointMake(200, 300)
let endPosition = CGPointMake(200, 0)
let start = elementToSwipeOn.coordinateWithNormalizedOffset(CGVectorMake(0, 0)).coordinateWithOffset(CGVector(dx: startPosition.x, dy: startPosition.y))
let finish = elementToSwipeOn.coordinateWithNormalizedOffset(CGVector(dx: 0, dy: 0)).coordinateWithOffset(CGVector(dx: endPosition.x, dy: endPosition.y))
start.pressForDuration(0, thenDragToCoordinate: finish)

Now i am able to drag from a specific point to another specific point. I implemented a custom refresh on some of my views. While implementing this, i also discovered that i can even use this to access the control center or the top menu.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!