![](/img/trans.png)
[英]How to hide keyboard without closing the dialog box using Appium for IOS?
[英]How to hide keyboard in iOS mobile automation using Appium
我使用的是 10.2 的 iOS 版本,而 xcode 版本是 8.3。
誰能告訴我如何使用 Appium 在 iOS 移動自動化中隱藏鍵盤?
使用的編程語言:Java。
我試過driver.hideKeyboard()
,但它對我不起作用。
所以,我嘗試了以下方式:
// way 1
driver.findElementByXPath(String.format("//XCUIElementTypeButton[@name='%s']", "Done")).click();
// way 2
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(new PointOption().withCoordinates(345, 343)).perform();
您可以使用java_client 庫方法:
driver.findElementByAccessibilityId("Hide keyboard").click();
driver.hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);
driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");
我注意到“完成”不是鍵盤組的一部分。 所以我嘗試使用名稱“Done”作為獲取元素的參考。 我最終嘗試了這個並且它有效。
driver.findElementByName("Done").click();
聲明為 IOSDriver 的“驅動程序”集。
您可以使用下面的代碼片段來隱藏鍵盤:
driver.getKeyboard().pressKey(Keys.RETURN);
Python - 2020 的解決方案:
@staticmethod
def hide_keyboard(platform):
"""
Hides the software keyboard on the device.
"""
if platform == "Android":
driver.hide_keyboard()
elif platform == "iOS":
driver.find_element_by_name("Done").click()
我更喜歡點擊 iOS 鍵盤上的最后一個鍵而不是隱藏:
@HowToUseLocators(iOSXCUITAutomation = LocatorGroupStrategy.CHAIN)
@iOSXCUITFindBy(className = "XCUIElementTypeKeyboard")
@iOSXCUITFindBy(className = "XCUIElementTypeButton")
private List<IOSElement> last_iOSKeyboardKey;
@HowToUseLocators(iOSXCUITAutomation = LocatorGroupStrategy.CHAIN)
@iOSXCUITFindBy(className = "XCUIElementTypeKeyboard")
@iOSXCUITFindBy(iOSNsPredicate = "type == 'XCUIElementTypeButton' AND " +
"(name CONTAINS[cd] 'Done' OR name CONTAINS[cd] 'return' " +
"OR name CONTAINS[cd] 'Next' OR name CONTAINS[cd] 'Go')")
private IOSElement last_iOSKeyboardKey_real;
public boolean tapLastKeyboardKey_iOS() {
System.out.println(" tapLastKeyboardKey_iOS()");
boolean bool = false;
setLookTiming(3);
try {
// one way
//bool = tapElement_XCTest(last_iOSKeyboardKey.get(last_iOSKeyboardKey.size()-1));
// slightly faster way
bool = tapElement_XCTest(last_iOSKeyboardKey_real);
} catch (Exception e) {
System.out.println(" tapLastKeyboardKey_iOS(): looks like keyboard closed!");
System.out.println(driver.getPageSource());
}
setDefaultTiming();
return bool;
}
我嘗試使用上述所有方法。 在某些情況下,它不能完美地工作。 以我的方式,它將點擊鍵盤的左上角。
public void hideKeyboard() { if (isAndroid()) { driver.hideKeyboard(); } else { IOSDriver iosDriver = (IOSDriver) driver; // TODO: Just work for Text Field // iosDriver.hideKeyboard(); // TODO: Tap outside of Keyboard IOSElement element = (IOSElement) iosDriver.findElementByClassName("XCUIElementTypeKeyboard"); Point keyboardPoint = element.getLocation(); TouchAction touchAction = new TouchAction(driver); touchAction.tap(keyboardPoint.getX() + 2, keyboardPoint.getY() - 2).perform(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
由於 IOS 設備鍵盤不再有任何“完成”或“回車”按鈕,所以我們不能使用任何 Appium 服務器實用程序界面,如 HideKeyboardStrategy。
我基本上使用了 TouchAction 類的點擊方法來點擊屏幕頂部並關閉鍵盤。
TouchAction touchAction = new TouchAction(driver);
int topY = driver.manage().window().getSize().height / 8;
int pressX = driver.manage().window().getSize().width / 2;
touchAction.tap(new PointOption().withCoordinates(pressX, topY)).perform();
快速簡單的解決方案:
我總是嘗試點擊屏幕上的任何地方,可能是
輸入后隱藏鍵盤,除非我明確要求與鍵盤交互。 這對我來說效果很好。 試試看 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.