patrol icon indicating copy to clipboard operation
patrol copied to clipboard

Send "Key Pressed" events to native views

Open AlexanderFarkas opened this issue 1 year ago • 2 comments

Use case

I wanted to enter text to a Stripe native card field. I tried several methods:

  1. $.native.enterText
  2. $.enterText
  3. $.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");

AlexanderFarkas avatar Nov 21 '23 17:11 AlexanderFarkas

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 avatar Nov 27 '23 12:11 jBorkowska

@jBorkowska I will try to PR this week with Android version included

AlexanderFarkas avatar Nov 28 '23 15:11 AlexanderFarkas