[Android / Python] Workaround for Sending Keys to Pre-Filled InputBoxes

Well, its been a long time that i had to find a workaround for Android Automation. I cannot say that I missed it :)

You can find the tl:dr version here : https://discuss.appium.io/t/python-client-pre-filled-inputboxes-set-value-returns-not-yet-implemented-but-it-seems-that-it-is-implemented/483

The current situation with Appium is that there are certain problems while dealing with Prefilled inputboxes.If you are using let's say Appium Java Client, than you can probably do a clear() call for that WebElement and than send keys to it.
Or the direct approach with set_value works for almost all cases.

But! For Python Client it's unfortunately not the same, When you try to do a clear() call on a prefilled inputbox, you will receive a 500 response from Appium server. And if you try to send_keys to it it will append your keystrokes to the current pre-filled text.

And it seems set_value function is not implemented yet.

So in this current situation, i found the following workaround which is FAR FROM BEING OPTIMIZED, but it works :)

For this solution please take this android native keycode table as a reference: https://android.googlesource.com/platform/frameworks/native/+/jb-mr1-release/include/android/keycodes.h

def test_case_x(self):  
    '''Just an example created for this'''

    sleep(0.5)

    email_input_field = self.driver.find_element_by_accessibility_id('Email Input Field')

    # Assume that this field is with the prefilled text
    # You want to input lets say 'aaa@aaa.com'
    # Decode this string in android native keycode using the link i gave above
    # you will get the following list below:

    list_of_email_keycodes = [29, 29, 29, 77, 29, 29, 29, 56, 31, 43, 41]

    # Focus the field by clicking it
    # Than just loop in the list and send keycodes

    email_input_field.click()
    for code in list_of_email_keycodes:
        self.driver.press_keycode(code)

Its really time consuming this way of solving the problem.
But its a solution nonetheless.

Take Care !