Replicate pull to refresh in XCTest UI testing - ios

I am trying to replicate a pull to refresh on a UITableView using the new Xcode UI testing framework in Xcode 7 (beta 3)
My current approach is dragging from the table to whatever element below the table I can find. This works when there is a fixed item below the table like a UIToolbar or UITabBar I would rather not rely on having a UITabBar or UIToolbar but I can't figure out a way to do the pull to refresh/drag action without using the method in XCUIElement.
func pressForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)
But it fails when I don't have a toolbar/tabbar and try to drag using the cells
This is the relevant portion of my code:
func testRefresh() {
//For testing cell
for _ in 0...9 {
addCell()
}
refreshTable()
}
func refreshTable(var tbl: XCUIElement? = nil) {
let app = XCUIApplication()
if tbl == nil {
let tables = app.tables
if tables.count > 0 {
tbl = tables.elementAtIndex(0)
}
}
guard let table = tbl else {
XCTFail("Cannot find a table to refresh, you can provide on explicitly if you do have a table")
return
}
var topElement = table
let bottomElement: XCUIElement?
//Try to drag to a tab bar then to a toolbar then to the last cell
if app.tabBars.count > 0 {
bottomElement = app.tabBars.elementAtIndex(0)
}
else if app.toolbars.count > 0 {
bottomElement = app.toolbars.elementAtIndex(0)
}
else {
let cells = app.cells
if cells.count > 0 {
topElement = cells.elementAtIndex(0)
bottomElement = cells.elementAtIndex(cells.count - 1)
}
else {
bottomElement = nil
}
}
if let dragTo = bottomElement {
topElement.pressForDuration(0.1, thenDragToElement: dragTo)
}
}
func addCell() {
let app = XCUIApplication()
app.navigationBars["Master"].buttons["Add"].tap()
}
Additional failed attempts:
swipeDown() (multiples times as well)
scrollByDeltaX/deltaY (OS X only)

You can use the XCUICoordinate API to interact with specific points on the screen, not necessarily tied to any particular element.
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.
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.)
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.

Here is a Swift 5.1 version
let firstCell = staticTexts["Id"]
let start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
let finish = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 100))
start.press(forDuration: 0, thenDragTo: finish)

Related

Finish action running on a SpriteNode without disabling touches

i have a player who's physicsBody is divided to parts (torso, legs, head, ect), now i have a button on screen that control the movement of the player, if the button is pressed to the right the player moves to the right, and that part works fine. the problem is every time the movement changes the walk() method is called and what it does is animate the player legs to look like its walking, but if the movement doesn't stop then is keeps calling the walk() without it finishing the animation, so it looks like its stuck back and forth. what i am trying to achieve is for the player to be able to walk constantly but for the walk() (animation method) to be called once, finish, then called again(as long as the button to walk is still pressed). here what i have so far:
func walk(){
let leftFootWalk = SKAction.run {
let action = SKAction.moveBy(x: 1, y: 0.1, duration: 0.1)
let actionReversed = action.reversed()
let seq = SKAction.sequence([action, actionReversed])
self.leftUpperLeg.run(seq)
self.leftLowerLeg.run(seq)
}
let rightFootWalk = SKAction.run {
let action = SKAction.moveBy(x: 0.4, y: 0.1, duration: 0.1)
let actionReversed = action.reversed()
let seq = SKAction.sequence([action, actionReversed])
self.rightUpperLeg.run(seq)
self.rightLowerLeg.run(seq)
}
let group = SKAction.sequence([leftFootWalk, SKAction.wait(forDuration: 0.2), rightFootWalk])
run(group)
}
extension GameScene: InputControlProtocol {
func directionChangedWithMagnitude(position: CGPoint) {
if isPaused {
return
}
if let fighter = self.childNode(withName: "fighter") as? SKSpriteNode, let fighterPhysicsBody = fighter.physicsBody {
fighterPhysicsBody.velocity.dx = position.x * CGFloat(300)
walk()
if position.y > 0{
fighterPhysicsBody.applyImpulse(CGVector(dx: position.x * CGFloat(1200),dy: position.y * CGFloat(1200)))
}else{
print("DUCK") //TODO: duck animation
}
}
}
First of all you can use SKAction.runBlock({ self.doSomething() })
as the last action in your actions sequence instead of using completion.
About your question, you can use hasAction to determine if your SKNode is currently running any action, and you can use the key mechanism for better actions management.
See the next Q&A
checking if an SKNode is running a SKAction
I was able to solve this, however i'm still sure there is a better approach out there so if you know it be sure to tell. here is what i did:
first i added a Boolean called isWalking to know if the walking animation is on(true) or off(false) and i set it to false. then i added the if statement to call walk() only if the animation is off(false) and it sets the boolean to on(true).
func directionChangedWithMagnitude(position: CGPoint) {
if isPaused {
return
}
if let fighter = self.childNode(withName: "fighter") as? SKSpriteNode, let fighterPhysicsBody = fighter.physicsBody {
fighterPhysicsBody.velocity.dx = position.x * CGFloat(300)
print(fighterPhysicsBody.velocity.dx)
if !isWalking{
isWalking = true
walk()
}
}
}
and i changed walk() to use completions blocks and set the Boolean to off(false) again
func walk(){
let walkAction = SKAction.moveBy(x: 5, y: 5, duration: 0.05)
let walk = SKAction.sequence([walkAction, walkAction.reversed()])
leftUpperLeg.run(walk) {
self.rightUpperLeg.run(walk)
}
leftLowerLeg.run(walk) {
self.rightLowerLeg.run(walk, completion: {
self.isWalking = false
})
}
}

Swift 3 - How to remove all Subviews from Superview as well as Array

I currently have a double for-loop that creates an X by Y grid of UIView CGRect squares. The loop also adds each UIView/Square of the grid to a 2D array allowing me to access each Cell of the grid and alter color/positioning by the index values of the loop.
The loop seems to work fine and displays the Cells/Squares perfectly, however after a while I want to remove all of the squares and also empty the array (but not entirely delete) to make room for a new next grid (which may be of a different size). I created a function to remove all the views from the superview.
This is how I am creating each "Cell" of the grid and placing each into the 2D array:
let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
cell.tag = 100
self.view?.addSubview(cell)
gridArray[xPos][yPos] = cell
The 2D array is being created on application load like so:
gridArray = Array(repeating: Array(repeating: nil, count: gridSizeY), count: gridSizeX)
I have tried to search for a solution to remove the UIViews from the superview however all answers from other Questions don't seem to work for me. I have tried to add cell.tag = 100 to each UIView and then remove all UIViews with the Tag Value of 100 like:
for subview in (self.view?.subviews)! {
if (subview.tag == 100) {
subview.removeFromSuperview()
}
}
However no luck. I have also tried to use this method:
self.view?.subviews.forEach({
$0.removeConstraints($0.constraints)
$0.removeFromSuperview()
})
The main differences I notice in my code compared to other peoples answers is that I have "?" and "!" at places in the code. I researched about what they mean and understood most of it however I still do not know how to fix my code because if it, and feel as though that is the issue. All I know is that the attempts to remove the UIViews from the superview does not work, and without the "?" and "!"s the code doesn't run at all.
How about to create tag for each cell you are using for example:
//Init value for for the tag.
var n = 0
func prepareCell() -> UIView {
let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
cell.tag = n
//Each cell should have new value
n += 1
return cell
}
And now remove required views.
func removeViews() {
for z in 0...n {
if let viewWithTag = self.view.viewWithTag(z) {
viewWithTag.removeFromSuperview()
}
else {
print("tag not found")
}
}
}
Example that is working in the playground:
var n = 0
let mainView = UIView()
func createView() -> UIView {
let view = UIView()
view.tag = n
n += 1
return view
}
for i in 0...16 {
mainView.addSubview(createView())
}
func removeViews() {
for z in 0...n {
if let viewWithTag = mainView.viewWithTag(z) {
viewWithTag.removeFromSuperview()
print("removed")
}
else {
print("tag not found")
}
}
}
You might be overlooking a much, much simpler way to do this...
You have built a 2D Array containing references to the "cells" as you populated your view. So, just use that Array to remove them.
// when you're ready to remove them
for subArray in gridArray {
for cell in subArray {
cell.removeFromSuperview()
}
}
// clear out the Array
gridArray = Array<Array<UIView>>()

how to scroll till particular position using xctest

I am trying to swipe up to a particular element in my app, if i use "swipeup" it is going to bottom of the view, which i don't want
Here is my code :
XCUIElement *staticText = [[[tablesQuery2 childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:2] childrenMatchingType:XCUIElementTypeStaticText].element;
[staticText swipeUp];
Here is my app screen before using swipe up
Here is my app screen after using swipe up
If you know which element you want to select and the height of each picker item, you can make an extension of XCUIElement to select the right value.
/// Move up/down the picker options until the given `selectionPosition` is reached.
func changePickerSelection(pickerWheel: XCUIElement, selectionPosition: UInt) {
// Select the new value
var valueSelected = false
while !valueSelected {
// Get the picker wheel's current position
if let pickerValue = pickerWheel.value {
let currentPosition = UInt(getPickerState(String(pickerValue)).currentPosition)
switch currentPosition.compared(to: selectionPosition) {
case .GreaterThan:
pickerWheel.selectPreviousOption()
case .LessThan:
pickerWheel.selectNextOption()
case .Equal:
valueSelected = true
}
}
}
}
/// Extend XCUIElement to contain methods for moving to the next/previous value of a picker.
extension XCUIElement {
/// Scrolls a picker wheel up by one option.
func selectNextOption() {
let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: 30.0)) // 30pts = height of picker item
endCoord.tap()
}
/// Scrolls a picker wheel down by one option.
func selectPreviousOption() {
let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -30.0))
endCoord.tap()
}
}
let pickerWheel = app.pickerWheels.element(boundBy: 0)
changePickerSelection(pickerWheel, selectionPosition: 2)

Accessibility Voice Over loses focus on Segmented SubView

I'm working on an Accessibility project where I have a segmentedController in the NavigationBar. Almost everything is working fine until the focus comes at the middle (2/3) SegmentedController. It won't speak the the accessibilityLabel..
See my code.
I'm using NSNotifications to let the 'UIAccessibilityPostNotification' know when to focus:
func chatLijst() {
let subViews = customSC.subviews
let lijstView = subViews.last as UIView
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, lijstView)
}
func berichtenLijst() {
let subViews = customSC.subviews
let messageView = subViews[1] as UIView
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, messageView)
}
func contactenLijst() {
let subViews = customSC.subviews
let contactenView = subViews.first as UIView
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, contactenView)
}
func setupSegmentedController(){
let lijst:NSString = "Lijst"
lijst.isAccessibilityElement = false
lijst.accessibilityLabel = "Lijst met gesprekken"
let bericht:NSString = "Bericht"
bericht.isAccessibilityElement = false
bericht.accessibilityLabel = "Bericht schrijven"
let contacten:NSString = "Contacten"
contacten.isAccessibilityElement = false
contacten.accessibilityLabel = "Contacten opzoeken"
let midden:CGFloat = (self.view.frame.size.width - 233) / 2
customSC.frame = CGRectMake(midden, 7, 233, 30)
customSC.insertSegmentWithTitle(lijst, atIndex: 0, animated: true)
customSC.insertSegmentWithTitle(bericht, atIndex: 1, animated: true)
customSC.insertSegmentWithTitle(contacten, atIndex: 2, animated: true)
customSC.selectedSegmentIndex = 0
customSC.tintColor = UIColor.yellowColor()
customSC.isAccessibilityElement = true
self.navigationController?.navigationBar.addSubview(customSC)
}
Fix
Strange enough I had to restructure the subViews array in the setup func and replace UIAccessibilityPostNotification object with the new segmentsView array.
func chatLijst() {
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, segmentsViews[0])
}
// Restructure subviews....
segmentsViews = [customSC.subviews[2], customSC.subviews[1], customSC.subviews[0]]
I'm using NSNotifications to let the 'UIAccessibilityPostNotification' know when to focus
Don't. That's a poor way to build a custom accessible control, and more importantly it can be confusing to the user. The screen changed notification doesn't just change focus, it also plays a specific sound that indicates to the user that the contents of the screen has changed.
Instead, I would recommend that you either make the subviews that you want appear as accessibility elements be accessibility elements with their own labels and traits and then rely on the OS to focus and activate them, or that you implement the UIAccessibilityContainer protocol in your custom control and then rely on the OS to focus and activate them.

Swift - Drag And Drop TableViewCell with Long Gesture Recognizer

So I have seen a lot of posts about reordering cells that pertain to using "edit mode", but none for the problem I have. (Excuse me if I am wrong).
I am building a ranking app, and looking for a way to use a long gesture recognizer to reorder the cells in my UITableView. Essentially a user will be able to reorder and "Rank" the cells full of strings in a group with their friends.
I would go the standard route of using an "edit" bar button item in the nav bar, but I am using the top right of the nav bar for adding new strings to the tableview already. (The following image depicts what I mean).
So far, I have added `
var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressDetected:")
lpgr.minimumPressDuration = 1.0;
tableView.addGestureRecognizer(lpgr)`
to my viewDidLoad method, and started creating the following function:
func longPressDetected(sender: AnyObject) {
var longPress:UILongPressGestureRecognizer = sender as UILongPressGestureRecognizer
var state:UIGestureRecognizerState = longPress.state
let location:CGPoint = longPress.locationInView(self.tableView) as CGPoint
var indexPath = self.tableView.indexPathForRowAtPoint(location)?
var snapshot:UIView!
var sourceIndexPath:NSIndexPath!
}
All of the resources I have scowered for on the internet end up showing me a HUGE, LONG list of additives to that function in order to get the desired result, but those examples involve core data. It seems to me that there must be a far easier way to simply reorder tableview cells with a long press?
Dave's answer is great.
Here is the swift 4 version of this tutorial:
WayPointCell is your CustomUITableViewCell and wayPoints is the dataSource array for the UITableView
First, put this in your viewDidLoad, like Alfi mentionend:
override func viewDidLoad() {
super.viewDidLoad()
let longpress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(gestureRecognizer:)))
self.tableView.addGestureRecognizer(longpress)
}
Then implement the method:
func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longpress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longpress.state
let locationInView = longpress.location(in: self.tableView)
var indexPath = self.tableView.indexPathForRow(at: locationInView)
switch state {
case .began:
if indexPath != nil {
Path.initialIndexPath = indexPath
let cell = self.tableView.cellForRow(at: indexPath!) as! WayPointCell
My.cellSnapShot = snapshopOfCell(inputView: cell)
var center = cell.center
My.cellSnapShot?.center = center
My.cellSnapShot?.alpha = 0.0
self.tableView.addSubview(My.cellSnapShot!)
UIView.animate(withDuration: 0.25, animations: {
center.y = locationInView.y
My.cellSnapShot?.center = center
My.cellSnapShot?.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
My.cellSnapShot?.alpha = 0.98
cell.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
cell.isHidden = true
}
})
}
case .changed:
var center = My.cellSnapShot?.center
center?.y = locationInView.y
My.cellSnapShot?.center = center!
if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
self.wayPoints.swapAt((indexPath?.row)!, (Path.initialIndexPath?.row)!)
//swap(&self.wayPoints[(indexPath?.row)!], &self.wayPoints[(Path.initialIndexPath?.row)!])
self.tableView.moveRow(at: Path.initialIndexPath!, to: indexPath!)
Path.initialIndexPath = indexPath
}
default:
let cell = self.tableView.cellForRow(at: Path.initialIndexPath!) as! WayPointCell
cell.isHidden = false
cell.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
My.cellSnapShot?.center = cell.center
My.cellSnapShot?.transform = .identity
My.cellSnapShot?.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapShot?.removeFromSuperview()
My.cellSnapShot = nil
}
})
}
}
func snapshopOfCell(inputView: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
inputView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let cellSnapshot : UIView = UIImageView(image: image)
cellSnapshot.layer.masksToBounds = false
cellSnapshot.layer.cornerRadius = 0.0
cellSnapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
cellSnapshot.layer.shadowRadius = 5.0
cellSnapshot.layer.shadowOpacity = 0.4
return cellSnapshot
}
struct My {
static var cellSnapShot: UIView? = nil
}
struct Path {
static var initialIndexPath: IndexPath? = nil
}
Give this tutorial a shot, you'll likely be up and running within 20 minutes:
Great Swift Drag & Drop tutorial
It's easy. I've only been developing for 3 months and I was able to implement this. I also tried several others and this was the one I could understand.
It's written in Swift and it's practically cut and paste. You add the longPress code to your viewDidLoad and then paste the function into the 'body' of your class. The tutorial will guide you but there's not much more to it.
Quick explanation of the code: This method uses a switch statement to detect whether the longPress just began, changed, or is in default. Different code runs for each case. It takes a snapshot/picture of your long-pressed cell, hides your cell, and moves the snapshot around. When you finished, it unhides your cell and removes the snapshot from the view.
Warning: My one word of caution is that although this drag/drop looks great and works close to perfectly, there does seem to be an issue where it crashes upon dragging the cell below the lowest/bottom cell.
Drag & Drop Crash Problem
Since iOS 11 this can be achieved by implementing the built in UITableView drag and drop delegates.
You will find a detailed description of how to implement them in this answer to a similar question:
https://stackoverflow.com/a/57225766/10060753

Resources