Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appium on Android - SendKeys to EditText also types default text

I am trying to write some automated tests with Appium for Android for Wordpress Mobile (https://github.com/wordpress-mobile/WordPress-Android).

The first thing I'm trying to do is type the username in the main login screen to be able to login to my Wordpress site, and I have a problem with SendKeys on the "username" field.

Here is how the element is seen in the uiautomatorviewer:

enter image description here

Here is what I have tried so far:

List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);       
login.sendKeys("username");

And:

driver.findElementsByClassName("android.widget.EditText").get(0).sendKeys("username");

And:

driver.findElement(By.xpath("//android.widget.EditText[@text='Editing. Username or email. ']")).sendKeys("username");

With all 3 versions of trying to send "username" as the username, when I run the test, what is actually typed in the field is: "Editing. Username or email. username"

So, it seems that the placeholder text is also kept, then my username is added.

NOTE that the text that is added when I send the username with appium is not there in the first place (see screenshot), but in the UI tree view, that appears to be the text in the EditText. When Appium is running the test, it is actually writing the "Editing. Username or email" text before adding my own username.

I have also tried, as suggested in one of the answers for a different question here: Appium : Clear a field the following code, where the sendKeyEvent(67) should clear the field:

List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);
login.click();
driver.sendKeyEvent(67);
login.sendKeys("username");

Using .clear() crashes and I have noticed that others suggested avoiding it if possible.

Of course, if I try to do this manually, the placeholder text is not added, I can just add my username in the field by typing it.

I can also use the driver.sendKeyEvent() function and send the characters one my one, but I would like to send the username as a parameter and be able to type any username in the field.

Because the extra text is typed every time I try to type the username, to workaround this I have to first type "username" - in the app, the actual text that is typed is "Editing. Username or email. username" - then move the cursor left in front of the word "username" and start deleting the rest - but this is EXTREMELY slow. Here is the code that does work this way:

        String setUsername = "username";
        login.click();
        login.sendKeys(setUsername);

        // hack to delete extra text that gets typed
        int stringLength = login.getText().length() - setUsername.length();
        for (int i = 0; i < setUsername.length(); i++) {
            driver.sendKeyEvent(21); //KEYCODE_DPAD_LEFT
        }
        for (int i = 0; i < stringLength; i++) {
            driver.sendKeyEvent(67); // "KEYCODE_DEL
        }

What am I missing? Any help would be greatly appreciated. I am trying to understand why the extra text gets typed.

like image 595
Ru Cindrea Avatar asked Sep 29 '22 21:09

Ru Cindrea


1 Answers

After a lot of searching, I found that this is really a bug in Appium v 1.2.2:

https://discuss.appium.io/t/android-appium-1-2-2-sendkeys-issue-with-hinted-edit-text/309

Hopefully, as it is said there, it will be fixed in version 1.2.3.

Thanks for all your help!

like image 55
Ru Cindrea Avatar answered Oct 25 '22 05:10

Ru Cindrea