5

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.

Community
  • 1
  • 1
Ru Cindrea
  • 703
  • 2
  • 5
  • 16

5 Answers5

4

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!

Ru Cindrea
  • 703
  • 2
  • 5
  • 16
1

I see that you have used a List and there are 3 text fields on your screenshot - why not use an index of those to pass your value? This way it won't add extra text to it.

List<WebElement> tfl=driver.findElements(By.className("android.widget.EditText"));
     tfl.get(0).sendKeys("username"); // tfl=text field list
     tfl.get(1).sendKeys("password");
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Star
  • 73
  • 7
0

Problem is you are using sendKeyEvent(67) only one time and that too at no proper place. What you have to do is first set your cursor at the end of the Text in Login field and then keep deleting each alphabet one by one until no more text is left in the field.

Eg: Try something like this

   WebElement login = textFieldsList.get(0);// Get the webelement where text is to be entered
      string str = login.Text;// it will store the Text already written in login field
        int textLength = str.Length;// find the lenght of the text that is to be removed
        int x = login.Location.X +(textLength * 40); //
        int y = login.Location.Y;//
        TouchAction action = new TouchAction(driver);// TouchAction class is defined in appium library
        action.Tap(testObject, x, y);// 
        for (int delete = 0; delete < str.Length; delete++)
        {
           rm.KeyEvent(67);
        }
    }

Hope it helps..

jain28
  • 137
  • 5
  • Thanks for you answer, but this doesn't help. As I mentioned, the text is NOT already written in the text field, and it is typed when Appium types the user name that I am sending. So there's nothing to delete in the first place, but when I type, it types the "Editing.... " + the username that I actually ask it to type. – Ru Cindrea Sep 15 '14 at 12:42
  • I can work around it only by first typing the username, then moving the cursor back until the username I want to be typed is past the cursor, then start deleting the rest. But this takes soooo long. I will edit the question to reflect this as well. – Ru Cindrea Sep 15 '14 at 12:43
  • You should first clear the field and then enter the username. – jain28 Sep 16 '14 at 03:54
  • Thanks for the comment, but as I mentioned the extra text is being typed every time I use "sendKeys", so clearing before doesn't help. If I clear, then type, the extra text is typed again. – Ru Cindrea Sep 16 '14 at 15:33
0

You're looking for

MobileElement textfield = driver.findElement(By.however(value))

textfield.setValue('foo')

I haven't tested that function on Android, but it was built because textfield.sendKeys is unreliable on iOS/Instruments.

You could also try textfield.click(), textfield.clear(), and then textfield.sendKeys('foo')

Example usage

You'll need the Appium java-client

Jess
  • 3,097
  • 2
  • 16
  • 42
  • 1
    I've tried using textfield.setValue('foo') but I get an error saying: "Not yet implemented. Please help us: http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information)(..)" My pom.xml has all versions set to "LATEST" - so I think I have the latest version of appium java-client and selenium webdriver... – Ru Cindrea Sep 16 '14 at 09:05
  • 1
    The other solution with textfield.clear() will not work because, as I said, the extra text is being typed every time I do sendKeys(), so it's not there in the first place to be cleared... – Ru Cindrea Sep 16 '14 at 09:07
  • Ah. I found [this](https://github.com/appium/java-client/issues/89). Try `setValueImmediate` (and I thought maybe the text was getting populated when you `.click()`ed) – Jess Sep 16 '14 at 20:10
  • Also if you ask on Appium's discourse help group, bootstraponline an jlipps are pretty active on there. – Jess Sep 16 '14 at 20:12
  • 1
    I checked the discussion group there and noticed that this is a bug that will be fixed in a future version. https://discuss.appium.io/t/android-appium-1-2-2-sendkeys-issue-with-hinted-edit-text/309 – Ru Cindrea Sep 18 '14 at 04:35
  • @RuCindrea I dug around and apparently `setValue` needs to be cast to a WebElement before you can call it (since it's an Appium-specific method). That should suffice as a workaround until `sendKeys` is fixed. [Source](https://discuss.appium.io/t/how-to-make-appium-tests-run-faster-on-ios/445/9?u=jessicasachs) – Jess Sep 20 '14 at 03:15
  • I tried the setValue and I could only use it if I cast it to MobileElement (instead of WebElement). I still get the same "Not yet implemented" error. I couldn't find any info or documentation on setValueImmediate() - do you have any more info on that? – Ru Cindrea Sep 22 '14 at 04:58
  • This question is outdated, and things with Appium Java are very different now. – Jess Feb 22 '17 at 21:25
0

You might want to downgrade one version as I think this bug doesn't exist in the last version.

qazimusab
  • 1,031
  • 1
  • 7
  • 14