问题
I'm using KIF to automate my app, and I'm trying to automate typing something into a UISearchBar
and searching for it. Unfortunately I couldn't find a satisfactory way to do that by programmatically clicking on the search button after I've entered my search text.
So instead, I considered simply using KIF's tapScreenAtPoint method. The problem with that method is that it does the following:
CGPoint windowPoint = [window convertPoint:screenPoint fromView:nil];
view = [window hitTest:windowPoint withEvent:nil];
It's expecting the tap to actually touch a view.. and since the keyboard is obviously not a view this test won't be able to trigger the search button on the keyboard.
How can one programmatically tap a button on the keyboard? I think this would be very useful for any automation-test suite.
回答1:
It sounds like you're trying to tap the "Search" button in the keyboard. The accessibility labels on return keys don't follow any sort of sane pattern. Looking in the accessibility inspector, you'll see "SEARCH" but that may not be consistent on all OS versions.
Update
You can trigger the search button by adding \n
to your enterText...
[tester enterTextIntoCurrentFirstResponder:@"query\n"];
回答2:
The correct way is to use:
[tester clearTextFromAndThenEnterText:@"sample test" intoViewWithAccessibilityLabel:@"SearchField"];
[tester waitForViewWithAccessibilityLabel:@"search"];
[tester tapViewWithAccessibilityLabel:@"search"];
Accessibility label for the return key is "search" in this case, you can verify it by using accessibility inspector app.
But be careful, the 'return key' (search/done/return/whatever) won't be pressed if you are adding a text that ends with cap letters, for example:
"sample TEST"
The 'shift' button will be pressed and the 'return' button won't, even if you try to use the "tapScreenAtPoint" method.
It happened to me and got me crazy for a long time until I figured out what was actually going on.
来源:https://stackoverflow.com/questions/18993379/how-to-programmatically-tap-a-point-on-the-ios-keyboard