简体   繁体   English

Android上的Appium - SendKeys到EditText也键入默认文本

[英]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 ). 我正在尝试使用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. 我要做的第一件事就是在主登录界面输入用户名,以便能够登录我的Wordpress网站,我在“用户名”字段中遇到SendKeys问题。

Here is how the element is seen in the uiautomatorviewer: 以下是uiautomatorviewer中元素的显示方式:

在此输入图像描述

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" 尝试发送“用户名”作为用户名的所有3个版本,当我运行测试时,该字段中实际键入的内容是:“编辑。用户名或电子邮件。用户名”

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. 请注意,当我使用appium发送用户名时添加的文本首先不在那里(参见屏幕截图),但在UI树视图中,它似乎是EditText中的文本。 When Appium is running the test, it is actually writing the "Editing. Username or email" text before adding my own username. 当Appium运行测试时,它实际上是在添加我自己的用户名之前编写 “编辑。用户名或电子邮件”文本。

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: 我也尝试过,正如其中一个答案的建议在这里提出一个不同的问题: Appium:清除以下代码的字段 ,其中sendKeyEvent(67)应该清除该字段:

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. 使用.clear()崩溃,我注意到其他人建议尽可能避免它。

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. 我也可以使用driver.sendKeyEvent()函数并发送我的一个字符,但我想发送用户名作为参数,并能够在字段中键入任何用户名。

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. 因为每次我尝试输入用户名时都会输入额外的文本,要解决此问题,我必须首先输入“用户名” - 在应用程序中,输入的实际文本是“编辑。用户名或电子邮件。用户名” - 然后移动光标留在单词“username”前面并开始删除其余部分 - 但这非常慢。 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. 我试图理解为什么额外的文本被输入。

After a lot of searching, I found that this is really a bug in Appium v 1.2.2: 经过大量的搜索,我发现这真的是Appium v​​ 1.2.2中的一个错误:

https://discuss.appium.io/t/android-appium-1-2-2-sendkeys-issue-with-hinted-edit-text/309 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. 希望正如在那里所说的那样,它将在版本1.2.3中修复。

Thanks for all your help! 感谢你的帮助!

Problem is you are using sendKeyEvent(67) only one time and that too at no proper place. 问题是你只使用了一次sendKeyEvent(67)而且没有适当的地方。 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.. 希望能帮助到你..

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. 我没有在Android上测试过这个功能,但它是因为textfield.sendKeys在iOS / Instruments上不可靠而构建的。

You could also try textfield.click() , textfield.clear() , and then textfield.sendKeys('foo') 你也可以尝试textfield.click()textfield.clear() ,然后是textfield.sendKeys('foo')

Example usage 用法示例

You'll need the Appium java-client 你需要Appium java-client

您可能希望降级一个版本,因为我认为上一版本中不存在此错误。

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? 我看到你使用了一个List ,屏幕截图上有3个文本字段 - 为什么不使用那些传递你的价值的索引? 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setValue和sendKeys引发错误,以使用Appium和Android在EditText中设置文本 - setValue and sendKeys throw error to set a text in EditText with Appium and Android appium 1.6 sendKeys到EditText失败 - appium 1.6 sendKeys to EditText Failed sendKeys 不是 function Appium javascript Android - sendKeys is not a function Appium javascript Android Appium sendKeys在android上真的很慢 - Appium sendKeys really slow on android EditText-如何在Android EditText中设置默认文本 - EditText - How to set default text in android edittext Sendkeys()不会在使用Appium的Android App中释放Element - Sendkeys() not release the Element in Android App using Appium 使用TestNG,AVD Simulator,Appium Server,SendKeys需要花费大量时间在Android App Automation中输入文本 - SendKeys taking to much time to enter text in Android App Automation Using TestNG,AVD Simulator,Appium Server edittext中禁用文本的android默认颜色是什么? - What is android default color for disabled text in edittext? Appium + Android + WebDriver findElement():sendKeys()之后找不到元素? - Appium + Android + WebDriver findElement() : cannot find element after sendKeys()? 如何使用Appium中的Android资源ID从EditText或TextView或按钮获取文本 - How to get text from edittext or textview or button using android resource id in appium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM