appium之hideKeyboard

来源:互联网 发布:迈克尔.凯恩 知乎 编辑:程序博客网 时间:2024/05/16 08:33
raftheunis87 commented Feb 13, 2013

Hi,

I'm experiencing an issue with the sendKeys method on a textfield. I'm using selenium webdriver in Java.

This is the piece of code:

WebElement helloButton = driver.findElement(By.name("Hello"));
WebElement textField = driver.findElement(By.tagName("textField"));

textField.sendKeys("Testuser");
helloButton.click();

When the sendKeys method is called, the keyboard appears on my iPhone simulator. But then it wants to press the helloButton, which is hidden underneath the keyboard and is gives an error (and a failing test ofcourse).

Is there a way to hide the keyboard before clicking the button?

Kind regards,

Raf Theunis

@gaige
gaige commented Feb 13, 2013

Based on some digging around, it appears the keyboard's "Hide keyboard" button (even on an iphone) is located in UIAutomation at
keyboard().buttons()["Hide keyboard"].tap();

So, I did the following in python and it worked:

self.driver.execute_script('UIATarget.localTarget().frontMostApp().keyboard().buttons()["Hide keyboard"].tap();');

@jlipps
Owner
jlipps commented Feb 13, 2013

we should create a special mobile command for hiding the keyboard, I want to keep people from having to learn any UIAutomation for using Appium.

I propose (e.g. in Python):

self.driver.execute_script("mobile: hideKeyboard")

Will build it and update this issue when it's merged.

@jlippsjlipps was assigned  Feb 13, 2013
@gaige
gaige commented Feb 13, 2013

excellent!

@raftheunis87
raftheunis87 commented Feb 13, 2013

And how do I do this in Java (Selenium Webdriver)? Because I'm not using Python. Thanks for the help!

@gaige
gaige commented Feb 13, 2013

I think this will help, but I'm not familiar with the Java bindings:

http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_execute_Javascript_directly?

Either way, you need the Selenium WebDriver execute script command, whatever it is bound to in java.

@jlippsjlipps referenced this issue Feb 14, 2013
 Merged

hideKeyboard method #185

@jlipps
Owner
jlipps commented Feb 14, 2013

Ok, this will work now as described above! (But using the Java-appropriate method of course)

@jlippsjlipps closed this  Feb 14, 2013
@rossrowe
Collaborator
rossrowe commented Feb 14, 2013

I've added some Java examples on how to execute the script commands to the https://github.com/appium/appium/wiki/Automating-mobile-gestures page

@raftheunis87
raftheunis87 commented Feb 18, 2013

I've grabbed the latest version of appium and tried the following:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("mobile: hideKeyboard");

The following exception appears:

org.openqa.selenium.WebDriverException: Not implemented in this context, try switching into or out of a web view

Anyone got some suggestions for me?

@rossrowe
Collaborator
rossrowe commented Feb 18, 2013

@jlipps I'm also getting the following error when using the latest appium and executing the 'mobile: hideKeyboard' command:

org.openqa.selenium.NoSuchElementException: Could not find the 'Hide keyboard' button, you're on your own for closing it!

@gaige
gaige commented Feb 18, 2013

@jlipps @rossrowe @raftheunis87 Can we move this to a new issue or reopen? This doesn't seem to be re-opening of its own volition.

@rossrowe Does this happen on both iPad and iPhone? Are you using an English-language device (the button find is by AXTitle, so I'm not sure if it works if using localized systems) Also, can you try using the original applescript?

self.driver.execute_script('UIATarget.localTarget().frontMostApp().keyboard().buttons()["Hide keyboard"].tap();');

@rossrowe
Collaborator
rossrowe commented Feb 18, 2013

I get the NoSuchElementException when I try to run the hide keyboard command using the iPhone simulator (6.0) and an English keyboard. I'll try to setup my environment to use the ipad simulator to see if the command works.

When I try the original applescript, I get the following error:

org.openqa.selenium.WebDriverException: Cannot perform action on invalid element: UIAElementNil from target.frontMostApp().keyboard().buttons()["Hide keyboard"]
Command duration or timeout: 1.31 seconds
Build info: version: '2.29.1', revision: 'dfb1306b85be4934d23c123122e06e602a15e446', time: '2013-01-22 12:58:05'
System info: os.name: 'Mac OS X', os.arch: 'amd64', os.version: '10.8.2', java.version: '1.7.0_04-ea'
Session ID: 881e7d8a-54dd-4295-88f9-135263bcb008
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{platform=MAC, databaseEnabled=false, javascriptEnabled=true, browserName=iOS, webStorageEnabled=false, locationContextEnabled=false, takesScreenshot=true, version=6.0}]

@gaige
gaige commented Feb 18, 2013

Definitely sounds like the keyboard element isn't on that particular keyboard. What kind of input are you using? I.e. is it displaying the full text keyboard or one of the specialty keyboards, such at numeric, etc.?

On the full text keyboard, there's usually a Hide Keyboard button which is hidden, but active, which is how this mechanism works. I haven't tried it with other keyboards, so it may not be possible there, the button may have a different name, or there may be another issue.

@rossrowe
Collaborator
rossrowe commented Feb 18, 2013

I just added the code to the SimpleTest class to hide the keyboard that is displayed after the text fields are populated, and I'm not changing the keyboard from what appears. I've committed the code that shows what I've tried to rossrowe@11314f6

@gaige
gaige commented Feb 18, 2013

On the iPhone, I think you're really going to have to push the button in the lower-right corner of the keyboard. That's basically going to require knowing what that button is and pushing it by name. In many cases, that's "Done" or "Search".

In the case of the TestApp, it's Done. It is accessible as a button in the root window. 

We may be able to find another solution to this in the future using another bit of custom javascript, but for the time being, we're limited to what we know about interaction with the keyboard.

@raftheunis87
raftheunis87 commented Feb 18, 2013

In other words: there will be no quick solution for this problem?
I'm currently working on a POC (proof of concept) of Appium for my company, so I need to know if this will be fixed soon.

Thanks again for the quick replies guys!

@gaige
gaige commented Feb 18, 2013

@raftheunis87 The solution at the moment is to do the same thing a user would do. Tap the button that will remove the keyboard from the screen, which is going to be in the lower-right corner of the keyboard. You should be able to locate it by the text string, which is exactly what we did with the iPad version.

You should be able to do that with the above execute_script command (see java examples previously in this thread if you don't know how to call this from java) and replace the name of your "Done", "Search", whatever button for the "Hide Keyboard" button in the example code. So, using python and a "Done" button, you'll get:

self.driver.execute_script('UIATarget.localTarget().frontMostApp().keyboard().buttons()["Done"].tap();');

@raftheunis87
raftheunis87 commented Feb 18, 2013

@gaige Ok, I'll give it a go!

@gaige
gaige commented Feb 18, 2013

I've been delving around in the UIA data for the iPhone keyboard, and I'm not finding any helpful hidden buttons there, and UIAKeyboard doesn't have an explicit mechanism for hiding the keyboard, so we may be stuck with doing what the user does.

@raftheunis87
raftheunis87 commented Feb 18, 2013

@gaige It's working for me in Java:

I've created a static final variable KEYBOARD_DONE with the following value: 
"UIATarget.localTarget().frontMostApp().keyboard().buttons()['Done'].tap();";

Then I used the driver like this:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(KEYBOARD_DONE);

Thanks for all the help guys!

@gaige
gaige commented Feb 18, 2013

@raftheunis87 Thanks for the update. We'll continue trying to find some more universal solution. Unfortunately, apps can change the title of that button, so we need to seek a better way to find it.

@raftheunis87
raftheunis87 commented Feb 18, 2013

@gaige Indeed. That's why I'd like to make a static final variable for every possible value of the "done" button e.g. hide, ok, done,...

@jlipps
Owner
 jlipps commented Feb 18, 2013
@q-aq-a referenced this issue Mar 11, 2013
 Closed

(null) could not be tapped #254

@amaanmemon
amaanmemon commented Nov 5, 2014

Can anyone please help me out..i need done button event from keyboard..i am using c# Appium version 1.2.4.1..please do the needful

@amaanmemon
amaanmemon commented Nov 5, 2014

@gaige @raftheunis87 @jlipps

@sebv
Owner
sebv commented Nov 5, 2014

@amaanmemon issues are for bug, for this kind of issue use http://discuss.appium.io/ or https://gitter.im/appium/appium.

Avoid commenting on closed issue, unless you were previously involved in the discussion, open a new issue explaining your problem and link to it.

@amaanmemon
amaanmemon commented Nov 5, 2014

ok @sebv

@imrankhan1810
imrankhan1810 commented Nov 7, 2014

I am trying to execute JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(KEYBOARD_DONE); code but getting error like : org.openqa.selenium.WebDriverException: Not implemented in this context, try switching into or out of a web view

@imrankhan1810
imrankhan1810 commented Nov 7, 2014

@jlipps :I am trying to hit Done button in my android real device but i am not getting any solution please could you help me on it.

@jlippsjlipps removed the NeedsTriage label Nov 19, 2014
@tarunHub
tarunHub commented Jan 7, 2015

You can simply do this by using the following command 
driver.navigate().back();

@Chan-Chan1
Chan-Chan1 commented Feb 10, 2015

Hi, Im using Cucumber+Page-object+selenium webdriver for creating mobile browser
caps = { :browserName => browser_name, :platform => "Android" }
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 500
@browser = Selenium::WebDriver.for(:remote, :url => "http://localhost:4723/wd/hub/", :http_client => client, :desired_capabilities => caps)
Now how to disable or hide android device key-pad during cucumber test run.

@naveenjammulanaveenjammula referenced this issue Mar 2, 2015
 Open

IOS Application hangs after tap using text #4624


Sign up for free
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去朝鲜的护照怎么办 朝鲜自由行签证怎么办 德国签证拒签怎么办 德签申诉不成功怎么办 出国工作找对象怎么办 白色轿车有刮痕怎么办 办签证手机钱包怎么办 怎么办美国10年签证 我没有出生证明怎么办 去台湾探亲怎么办签证 漏接澳大利亚签证电话怎么办 冰岛游公证认证怎么办 莲蓬头开关坏了怎么办 淋浴莲蓬头坏了怎么办 手机ld密码忘掉怎么办 脊椎压迫神经头晕怎么办 脊椎疼导致头晕怎么办 脊椎疼引起头晕怎么办 去英国探亲签证怎么办 地税国税合并人员怎么办 机场服务员老了怎么办 在国外没有钱了怎么办 被劫持为人质怎么办 在印度签证过期怎么办 办签证被拒怎么办 澳洲留学生怎么办新加坡签证 韩国交换生签证怎么办 没有钱还贷款怎么办 英国主动退学后怎么办 英国留学被劝退怎么办 英国留学签证被拒怎么办 签证纸丢了怎么办 日本的探亲签证怎么办 成都去港澳怎么办签证 在北京怎么办泰国签证 法院判决不准离婚怎么办 再婚小孩上户口怎么办 被供应商起诉了怎么办 离婚案原告撤诉怎么办 离婚起诉不到场怎么办 判决书判了败诉怎么办