Coding, productivity, and Mac automation tutorials for beginners and beyond.
A Calabash Custom Step to Swipe to Delete All Cells in a Table View
While testing a “swipe to delete” feature in an app, I wrote a custom script for Calabash iOS that automates the deletion of all the cells in the Table View. This step definition is generic enough that it can be used in most apps. I hope you find it useful.
This step makes 2 assumptions:
1) The table view contains at least one cell that can be deleted via a swipe. It’s up to you to create a step that populates the table.
2) When all cells are deleted, your app displays helpful text, such as “No Saved Articles,” within a UITableViewCell. This requires additional logic since verifying that all cells were deleted is not as simple as checking that the total number of cells equals zero.
defcheck_cell(prefix)if@final_count[0]==1final_cell=query"TableViewCell index:0"iffinal_cell.first["class"]=="UITableViewCell"sleep(STEP_PAUSE)elsescreenshot_and_raise"#{prefix} of the cells were deleted!"endelsescreenshot_and_raise"#{prefix} of the cells were deleted!"endendThen/^I swipe to delete all cells in "([^\"]*)"$/do|orientation|total_rows=query("tableView index:0",numberOfRowsInSection:0)end_of_range=total_rows[0]-1(0..end_of_range).eachdoscroll_to_row"tableView index:0",0sleep(STEP_PAUSE)macro%Q[I swipe on cell number 1 in "#{orientation}"]sleep(STEP_PAUSE)touch"TableViewCellDeleteConfirmationControl"end@final_count=query("tableView index:0",numberOfRowsInSection:0)if@final_count[0]==total_rows[0]check_cell("None")elsecheck_cell("Only some")endend
If your app doesn’t display any cells in the Table View after swiping to delete all cells (i.e. if you get [0] after running query("tableView index:0",numberOfRowsInSection:0)), then you can use this step definition instead:
Then/^I swipe to delete all cells in "([^\"]*)"$/do|orientation|total_rows=query("tableView index:0",numberOfRowsInSection:0)#this returns an arrayend_of_range=total_rows[0]-1(0..end_of_range).eachdoscroll_to_row"tableView index:0",0sleep(STEP_PAUSE)macro%Q[I swipe on cell number 1 in "#{orientation}"]#requires this custom step definition: https://gist.github.com/3298026sleep(STEP_PAUSE)touch"TableViewCellDeleteConfirmationControl"endfinal_count=query("tableView index:0",numberOfRowsInSection:0)iffinal_count[0]==total_rows[0]screenshot_and_raise"None of the cells were deleted!"elsiffinal_count[0]!=0screenshot_and_raise"Some of the cells were not deleted!"endend
Both of the steps above use a macro that is defined here:
Swipe on a cell regardless of device orientation Gist
12345678910
Then/^I swipe on cell number (\d+) in "([^\"]*)"$/do|index,orientation|index=index.to_iscreenshot_and_raise"Index should be positive (was: #{index})"if(index<=0)iforientation=="landscape"swipe("up",{:query=>"tableViewCell index:#{index-1}"})elsiforientation=="portrait"swipe("right",{:query=>"tableViewCell index:#{index-1}"})endsleep(STEP_PAUSE)end