So with the new Xcode update Apple has revamped the way we do UI testing. In instruments we used java script function "isVisible" to determine if our targeted element is visible.
I'm trying to replicate this in Objective-C but I can't seem to find the equivalent of this. I have a table view, a prototype cell with two labels on it. This prototype cell is reused 50 times lets say.
I'm trying to scroll until the last cell is visible, I did this by doing this:
if (![[[[[[XCUIApplication alloc] init].tables childrenMatchingType:XCUIElementTypeCell] matchingIdentifier:@"cell"] elementBoundByIndex:49].staticTexts[@"text"] exists]) {
[[[[[[XCUIApplication alloc] init].tables childrenMatchingType:XCUIElementTypeCell] matchingIdentifier:@"cell"] elementBoundByIndex:0].staticTexts[@"text"] swipeUp];
}
But this won't swipe since the element exists when the view is loaded. Please help because this is driving me crazy.
You should extend the XCUIElement's method list. The first method (scrollToElement:
) will be called on the tableView, the second extension method helps you decide if the element is on the main window.
extension XCUIElement {
func scrollToElement(element: XCUIElement) {
while !element.visible() {
swipeUp()
}
}
func visible() -> Bool {
guard self.exists && !CGRectIsEmpty(self.frame) else { return false }
return CGRectContainsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
}
}
The scrolling code should look like this (e.g. scrolling to last cell):
func testScrollTable() {
let app = XCUIApplication()
let table = app.tables.elementBoundByIndex(0)
let lastCell = table.cells.elementBoundByIndex(table.cells.count-1)
table.scrollToElement(lastCell)
}
Swift 3:
extension XCUIElement {
func scrollToElement(element: XCUIElement) {
while !element.visible() {
swipeUp()
}
}
func visible() -> Bool {
guard self.exists && !self.frame.isEmpty else { return false }
return XCUIApplication().windows.element(boundBy: 0).frame.contains(self.frame)
}
}
All the previous answers are not 100% fail proof. The problem I was facing is that swipeUp() has a larger offset and I couldn't find a way to stop the scrolling when I have the element in view port. Sometimes the element gets scrolled away because of the excessive scroll and as a result test case fails. However I managed to control the scroll using the following piece of code.
/**
Scrolls to a particular element until it is rendered in the visible rect
- Parameter elememt: the element we want to scroll to
*/
func scrollToElement(element: XCUIElement)
{
while element.visible() == false
{
let app = XCUIApplication()
let startCoord = app.collectionViews.element.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -262));
startCoord.pressForDuration(0.01, thenDragToCoordinate: endCoord)
}
}
func visible() -> Bool
{
guard self.exists && self.hittable && !CGRectIsEmpty(self.frame) else
{
return false
}
return CGRectContainsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
}
Note : Please use app.tables if your view is tableview based
Solutions using swipeUp()
and swipeDown()
are not ideal because they can potentially scroll past the target element due to the momentum of the swipe. After much searching and frustration I found a magical method on XCUICoordinate
:
func press(forDuration duration: TimeInterval, thenDragTo otherCoordinate: XCUICoordinate)
So we can do something like:
let topCoordinate = XCUIApplication().statusBars.firstMatch.coordinate(withNormalizedOffset: .zero)
let myElement = XCUIApplication().staticTexts["My Element"].coordinate(withNormalizedOffset: .zero)
// drag from element to top of screen (status bar)
myElement.press(forDuration: 0.1, thenDragTo: topCoordinate)
As far as checking whether something is visible goes, you want to use isHittable
in conjunction with exists
. see scrollDownToElement
in the extension below
Here's a handy extension that will scroll until an element is on screen and then scroll that element to the top of the screen :)
extension XCUIApplication {
private struct Constants {
// Half way accross the screen and 10% from top
static let topOffset = CGVector(dx: 0.5, dy: 0.1)
// Half way accross the screen and 90% from top
static let bottomOffset = CGVector(dx: 0.5, dy: 0.9)
}
var screenTopCoordinate: XCUICoordinate {
return windows.firstMatch.coordinate(withNormalizedOffset: Constants.topOffset)
}
var screenBottomCoordinate: XCUICoordinate {
return windows.firstMatch.coordinate(withNormalizedOffset: Constants.bottomOffset)
}
func scrollDownToElement(element: XCUIElement, maxScrolls: Int = 5) {
for _ in 0..<maxScrolls {
if element.exists && element.isHittable { element.scrollToTop(); break }
scrollDown()
}
}
func scrollDown() {
screenBottomCoordinate.press(forDuration: 0.1, thenDragTo: screenTopCoordinate)
}
}
extension XCUIElement {
func scrollToTop() {
let topCoordinate = XCUIApplication().screenTopCoordinate
let elementCoordinate = coordinate(withNormalizedOffset: .zero)
// Adjust coordinate so that the drag is straight up, otherwise
// an embedded horizontal scrolling element will get scrolled instead
let delta = topCoordinate.screenPoint.x - elementCoordinate.screenPoint.x
let deltaVector = CGVector(dx: delta, dy: 0.0)
elementCoordinate.withOffset(deltaVector).press(forDuration: 0.1, thenDragTo: topCoordinate)
}
}
Gist over here with added scrollUp
methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With