patrol
patrol copied to clipboard
Send "Key Pressed" events to native views
Use case
I wanted to enter text to a Stripe native card field. I tried several methods:
-
$.native.enterText
-
$.enterText
-
$.tester.sendKeyEvent
for manual key typing.
Querying native views by Selector("card details")
does work and it finds the card view, but enterText
constantly failed.
Depending on what I tried it either failed with "Element doesn't have focus" or it failed to find element for enterText
and silently crashed without error message even in verbose
.
$.tester.sendKeyEvent
- was evident workaround, but, turns out, it operates on Flutter layer and doesn't send keys to native views (it definitely didn't work for me).
So I decided to fork and add this functionality.
Final code:
await $(Keys.newCardField).tap(); // newCardField - is a key passed to CardField widget from StripeSDK
await $.pump(const Duration(seconds: 1)); // wait for focus
// manually tap text char by char
for (int i = 0; i < 11; i++) {
await $.native.sendKeyEvent("4");
await $.native.sendKeyEvent("2");
}
await $.native.sendKeyEvent("4");
Proposal
I'm not aware of the ability of inputing arbitrary characters on Android, but I've come up with the following API for iOS:
await $.native.sendKeyEvent("2");
Hi @AlexanderFarkas! Thank you for this proposal, it would be awesome, if you opened a PR with your feature. Still, the Android part is needed, but you can open a PR anyway
@jBorkowska I will try to PR this week with Android version included