How to clear a pre-filled text field (Android)

willosser, which version of appium do you use?
I have “gem -v appium: 1.8.29” and I haven’t got press_keycode method in driver class (\Ruby193\lib\ruby\gems\1.9.1\gems\appium_lib-3.0.3\lib\appium_lib\driver.rb) - or my IDE didn’t show it.

That might explain it. I’m using 2.4.3.

I’m sorry, i’m quite new in Appium, where can I find this version? When I go to this link: https://github.com/appium/appium/releases - there is only 1.3.6 version. Or you talk about something other.

1 Like

Well, that seems wrong to me as well.
gem -v returns the version of gem, whether you include a parameter after -v or not

I’m using Appium 1.3.5. I get that version by pulling the git repo at that tag. How are you installing appium, through git or npm or something else?

@czarnykwarc @willosser You can just open the UI console of appium and check in the about section to know which version you are running!

@willosser Sorry about the late reply on this! Can you just paste the java code which you have written. For me my code was working seamlessly ,have to update it if broke for your usecase.

@Abhishek_Gupta, I think you meant to direct that last comment at @czarnykwarc

@Abhishek_Gupta I use Ruby, not Java.
My Appium version is 1.3.6.

@willosser If you have the Java code handy for the same it would be really helpful for me.

@Abhishek_Guptal We are using Ruby as well. The use case was a particular phone which, unfortunately, I didn’t record.

Hey there,
I still see inconsistent results with the stock .clear appium method on android - clear does not always clear the text field all the way.
I am using 1.4.8 (i’m updating soon) and my teammate is up to date with 1.4.10.

I essentially solved this by using the Mar 3 post that Abhishek_Gupta wrote, with the addition of pressing the android keycode to reach the end of the field. Here’s what I have. It seems to work with one or multi line textfields, spaces and no spaces. I haven’t tried this using any localized text.

#I work at blackboard so bb makes sure I don't get confused 
def bb_clear(my_element)   
        system("adb shell input keyevent 123") #puts the cursor at the end of the text field
        for i in 1..my_element.text.length
          system("adb shell input keyevent KEYCODE_DEL")
        end
end

Here’s my ruby version info and my Gemfile info:

10:09 $ ruby --version
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

10:14 $ more Gemfile
source 'https://www.rubygems.org'

gem 'appium_lib',         '~> 7.0.0'
gem 'rest-client',        '~> 1.6.7'
gem 'rspec',              '~> 2.14.1'
gem 'cucumber',           '~> 1.3.15'
gem 'rspec-expectations', '~> 2.14.5'
gem 'spec',               '~> 5.3.4'
gem 'sauce_whisk',        '~> 0.0.13'
gem 'test-unit',          '~> 2.5.5' # required for bundle exec ruby xunit_android.rb
gem 'nokogiri',           '~> 1.6.6'
gem 'awesome_print',      '~> 1.6'
gem 'json',               '~> 1.8'
gem 'selenium-webdriver', '~> 2.45'
gem 'toml',               '~> 0.0'
gem 'rake-hooks',         '~>1.2.3'
gem 'eyes_selenium',      '~> 2.23.0'

Hope this is helpful.
Thanks,
Eric

After doing so much investigation i found solution for this issue. clear and type was not working in Android 6.0 if the context is webview.

MobileElement element = (MobileElement) appDriver.findElement(By.xpath(expression));
el.clear();
if(null!=el&&null!=el.getText() && el.getText().length()>0){
el.click();
Runtime.getRuntime().exec(“adb shell input keyevent 123”);
int leng=el.getText().length();
logger.debug(“Lenght of the text is” + leng);
for(int i=0;i<leng;i++){
appDriver.sendKeyEvent(AndroidKeyCode.BACKSPACE);
}
}
el.sendKeys(text);

@automationuser why not replace it?

 el.replaceValue(text);

((PressesKeyCode) driver).pressKeyCode(67);

Keycode 67 gives me special characters appended at the end
Snapshot attached.