UIAutomation iOS - delete a table cell - ios

I am working on deleting a table cell. I thought it will be done this way.
var myCell = cells["abc"];
var deleteSwitch = myCell.switches()[0];
deleteSwitch.tap();
but, when I tried to logElementTree in myCell, it did not show me any UIASwitch.
It displayed UIAStaticText which has the name of the cell and toggle button in order to enter edit mode. When I record my actions to capture script, I tried o tap on delete button to delete text and it recorded as a tap on that cell but not on switch/button.
Please let me know how to delete a cell.

In order for any element to appear in element tree, you should have accessibility enabled on that element. Do you have accessibility enabled on UISwitch inside iOS code? If your switch name in iOS code is say 'mySwitch', then try doing:
[mySwitch setIsAccessibilityElement:YES];

Related

Clicking UIButton after Disabling its user interaction causes collection view cell being selected

I have a collection view cell and a button as its subview. If you click the cell it goes to the detail page. And if you click the button it adds the item to the basket. I need to block the button from clicking more than 1. So I am disabling the button for a few seconds. But this time if I click the button before the delay ends, it goes to the detail page from the button too. Is there a way to solve this without disabling the cell itself?
Use custom delegate as call back in cell that will let the ViewController know that button is disable when user will tap on button. Store that disable state in some store property of ViewController.
let say flag = false
After that when user will tap cell didSelect delegate will get trigger. Then add there a check if flag == false do nothing and vice versa.
After few seconds change the stat of flag i.e. flag = true.
In this way you will not need to disable the cell and you can perform other events there.
Just giving you an idea as I cannot see you code I hope this will help.

iOS UITest : How to find UITableViewCell's AccessoryView?

Hello I'm learning UITests now
I have a question
how can detect accessoryView's tap on the tableViewCell?? in UITest
below is my tableViewCell
I want detect detail closure accesorry view's tap
like this
app.tables.cells.buttons["FTCellAccesoryIdentifier"].tap()
but accesorry is a subclass of UIView
so i allocate accisibility identifier key in cellForRows function
cell.accessoryView?.accessibilityIdentifier ="FTCellAccesoryIdentifier"
and my test function is
i try
app.tables.cells.elementBoundByIndex(lastCellIndex).otherElements.elementMatchingType(.Any, identifier: "FTCellAccesoryIdentifier").tap()
but not work
is possible to tap cell's accesoryView in UITests?
TL;DR
Regarding...
is possible to tap cell's accesoryView in UITests?
... Yes.
But you have to use the default accessibility identifier for that.
For example, for a cell like this one:
Tapping the Detail Disclosure button during a UI test ("More Info" is the key here):
XCUIApplication().tables.buttons["More Info, Title"].tap()
Tapping the cell itself during a UI test:
XCUIApplication().tables.staticTexts["Title"].tap()
Long story:
There is no way to change the default accessibility identifier of the Detail Disclosure. From Apple:
Note: If your table cells contain any of the standard table-view elements, such as the disclosure indicator, detail disclosure button,
or delete control, you do not have to do anything to make these
elements accessible. If, however, your table cells include other types
of controls, such as a switch or a slider, you need to make sure that
these elements are appropriately accessible.
At the time you try to set the accessibility identifier/label of the Detail Disclosure in cellForRow, cell.accessoryView is nil. Why? Again, from Apple:
accessoryView
Discussion
If the value of this property is not nil, the UITableViewCell class
uses the given view for the accessory view in the table view’s normal
(default) state; it ignores the value of the accessoryType property.
The provided accessory view can be a framework-provided control or
label or a custom view. The accessory view appears in the the right
side of the cell.
The accessory view will be nil until you define them. :/
You would need to provide your "own" Detail Disclosure (e.g.: a customized UIView) if you really want to set a custom identifier for it. For example (to illustrate):
cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20))
cell.accessoryView?.backgroundColor = UIColor.blueColor()
cell.accessoryView?.accessibilityIdentifier = "FTCellAccesoryIdentifier"
You should use accessoryButtonTappedForRowWithIndexPath delegate method to handle click on accessoryView.
Then by indexPath parameter of method you can know which cell it is!
Hope this will help :)

how to write UI test to test whether image existing when click the first cell in an UITableView?

There is an UITableView in my storyboard. It contains 5 Table view cells.
When I click the first cell, it will go to the second UIView and show the corresponding images. How could I code the UITest part to test whether the image show up when I click the first cell?
There is a magic part of XCode 7- UIRecording
First selected the testCase,and start recording like the Gif
Then you will see,UIRecording create UITest code for you,In my case,I get
let app = XCUIApplication()
app.tables/*#START_MENU_TOKEN#*/.staticTexts["Hello"]/*[[".cells.staticTexts[\"Hello\"]",".staticTexts[\"Hello\"]"],[[[-1,1],[-1,0]]],[0]]#END_MENU_TOKEN#*/.tap()
app.otherElements.containingType(.NavigationBar, identifier:"UIView").childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Image).element.tap()
Then,I just need a little change
let app = XCUIApplication()
app.tables.cells.elementBoundByIndex(0).tap()
let imageView = app.otherElements.containingType(.NavigationBar, identifier:"UIView").childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Image)
let count = imageView.images.count
XCTAssert(count != 0)
So,the there are mainly two step
Let UIRecording Create codes for you
Make a little changes,such as add Assert,query by index,and so on.
Then run

Combine UISwitch and UITableViewCell for VoiceOver interaction

In Calendar, when you create a new event, if you tap on the All Day cell with VoiceOver enabled, Siri says "All Day switch button on/off, double tap to change setting". And indeed double tapping will toggle the switch. Also, it's not possible to tap on just the toggle switch itself - you have to interact with the cell itself to toggle the switch, the switch itself is not an accessible element.
In my app I have the exact same setup with a label and a switch. But when I tap the cell with VoiceOver enabled it only reads the label so the blind user has no idea there's a toggle switch in that cell. If they tap the switch itself then they can interact with it, so it's the opposite of the setup in the Calendar app.
How can I obtain the same behavior that Apple implemented? I need some way to combine the switch into the cell so VoiceOver reads both upon highlighting the cell, then when they double tap it should toggle the switch, and I'm not sure how that setup can be accomplished. Thanks!
To implement the desired behavior, instead of placing the UISwitch in the contentView of the cell, add it as the accessoryView programmatically. Then the cell and switch will behave exactly as expected when using VoiceOver, exactly as it does in Calendar.
You should be able to set a custom accessibility description on the cell using
cell.accessibilityLabel = #"Double tap to toggle setting";
You can set up custom gestures for when VoiceOver is running according to this answer:
https://stackoverflow.com/a/12337128/567511
But here you would not need custom gestures, instead your didSelectRowAtIndexPath would flip the switch only when UIAccessibilityIsVoiceOverRunning is true.
I would like to elaborate on the answer of Joey, and clarify how a solution to this question can be achieved in code.
In the tableView:cellForRowAtIndexPath:, create a custom UISwitch view and append it to the accessoryView of the cell. This could look something like the following.
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
[switchView setOn:NO];
[switchView addTarget:self action:#selector(selector:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchView;
return cell;
The cell will now behave like any native iOS switch known from e.g. Settings or Calendar. Double tapping on the cell with VoiceOver enabled, will now toggle the UISwitch (on/off), and VoiceOver will automatically notify the user about the state of the switch, together with an accessibility hint.

How to get highlighted text of UITextView in UITableViewCell?

I have a custom cell (subclass of UITableViewCell) with a textView inside of it. It works great! Now, when I tap on a cell and highlight some text, the default UIMenuController appears and I can choose to copy the highlighted text. Also this function works perfectly. Now, I would like to add a custom button to the UIMenuController, which I actually did, but to perform the menu item action I need to know what the selected text is. How can I get it?
To explain this better, there is no method in UITextField that allows us to know what the currently selected text is. But we can leverage the copy action on the text field that is associated with the menu controller. The copy action copies the text onto the pasteboard which we will need to retrieve. I was able to implement a Log function in my custom subclass of UITextField like this –
- (void)log:(id)sender {
[self copy:sender];
NSString *highlightedText = [UIPasteboard generalPasteboard].string;
NSLog(#"%#", highlightedText);
}
This logs the selected text onto the console. Doesn't do much but gives you the basic idea.

Resources