How do I get cursor position which is placed in EditText Appium automation (Android and iOS)

Question:-

How do I get current cursor position through Appium automation? Cursor is placed in EditText control and I want to get its position at Runtime.

I am working with mobile automation framework which is built in Java - Appium - Gradle.

Our automation framework supports both Android and iOS.

I do not see any method available directly with Appium to get Cursor position.

Any help would be appreciated. Thanks!!

I don’t think this can be done. Could you expand on the use case? For example, when you say “position at runttime”, do you mean position in existing text?

@wreed So I have text field, which is called as EditText field in mobile. And I am looking for mechanism to get current cursor position in that EditText box.

Basically my test case is to check if cursor position remains intact wherever user leaves it before leaving the page. if I switch to some different pages in app and return back to this page, cursor should maintain its position as earlier in this EditText…

Here is what I would do:

  1. Get existing text in EditText field, let’s say the text is, ‘Nuance’
  2. Hit delete key
  3. Get new text, let’s say it’s now, ‘Nuanc’
  4. We now know where cursor is. Move to other screen, etc. and move back
  5. Hit delete key. Existing text should now be ‘Nuan’. If so, pass test.

@wreed Is there not any direct solution available to achieve it? Using some cursor movements methods if available in Appium ?

The only thing I can think of is one of the move commands, but you’d have to do some fancy math to scale it on different devices:

https://appium.io/docs/en/commands/interactions/mouse/moveto/

Even so, that won’t necessarily get you where you want to go. It can put you on a point, which is not the cursor position you asked for. I certainly wouldn’t discourage you from trying, but you may end up with a brittle test that is unreliable.

Thank you @wreed for your valuable quick input on this. Appreciated!!