appium之hideKeyboard

13 篇文章 0 订阅
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.

@jlipps jlipps 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.

@jlipps jlipps 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)

@jlipps jlipps 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-a q-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

@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.

@jlipps jlipps 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值