问题
Along with UITests
and UITableView
I need to reorder cells. Is it possible? I tried myTable.swipeDown()
but it is called in that place of cell which do not respond for reordering. How can I do this? Is it possible at all?
回答1:
If you have set the accessibility properties of the custom cell set correctly then the reorder control should be accessible. Let's assume the cell's set their accessibility labels/identifiers to "LondonStreet" and "BakerStreet", respectively.
You can reorder the cells by tapping and dragging from one reorder control to the next. The accessibility identifier for these controls is set automatically from the cell's information.
let app = XCUIApplication()
app.launch()
let topButton = app.buttons["Reorder LondonStreet"]
let bottomButton = app.buttons["Reorder BakerStreet"]
bottomButton.pressForDuration(0.5, thenDragToElement: topButton)
The "Reorder "
prefix was set by the OS. Try using the Accessibility Inspector to see what your reorder control's values are.
I've added an example to my UI Testing Cheat Sheet with some working sample code if you would like to try it on your machine.
回答2:
To enable Accessibility Inspector
first you have to go to:
System Preferences > Security & Privacy:
And then Xcode > Open Developer Tool > Accessibility Inspector
回答3:
A more generic solution: (if you don't know the titles)
guard let firstCell = app.cells.allElementsBoundByIndex.first, let topButton = firstCell.buttons.allElementsBoundByIndex.last else { XCTFail("no reoder btn for first cell"); return }
guard let bottomCell = app.cells.allElementsBoundByIndex.last, let bottomButton = bottomCell.buttons.allElementsBoundByIndex.last else { XCTFail("no reoder btn for last cell"); return }
topButton.press(forDuration: 0.5, thenDragTo: bottomButton)
来源:https://stackoverflow.com/questions/32502633/how-to-reorder-cells-under-uitests