Appium hidekeyboard() 方法

Appium hidekeyboard() method

我正在尝试自动化一个邮政编码输入字段,它会弹出一个数字键盘供用户输入。 Appium 可以正确输入邮政编码,但在折叠键盘时遇到问题,我注意到有 appium menthod driver.hidekeyboard()

但不幸的是,当我使用它时它给出了错误。

网络驱动程序错误:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: [object Object]

Appium 服务器日志:

debug] [JSONWP Proxy] Matched '/element/undefined/click' to command name 'click'
[debug] [JSONWP Proxy] Proxying [POST /element/undefined/click] to [POST http://localhost:8100/session/83293E4F-4B7A-462A-B5B1-1D729D54E36B/element/undefined/click] with body: {}
[debug] [XCUITest] Connection to WDA timed out
[debug] [iProxy] recv failed: Operation not permitted
[debug] [JSONWP Proxy] Got response with status 200: {"value":{},"sessionId":"83293E4F-4B7A-462A-B5B1-1D729D54E36B","status":13}
[JSONWP Proxy] Got an unexpected response: {"value":{},"sessionId":"83293E4F-4B7A-462A-B5B1-1D729D54E36B","status":13}
[debug] [MJSONWP] Matched JSONWP error code 13 to UnknownError
[debug] [XCUITest] Connection to WDA timed out
[debug] [iProxy] recv failed: Operation not permitted
[debug] [W3C (bac0efb8)] Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command. Original error: [object Object]
[debug] [W3C (bac0efb8)]     at errorFromMJSONWPStatusCode (/usr/local/lib/node_modules/appium/node_modules/_appium-base-driver@3.14.0@appium-base-driver/lib/protocol/errors.js:789:10)
[debug] [W3C (bac0efb8)]     at ProxyRequestError.getActualError (/usr/local/lib/node_modules/appium/node_modules/_appium-base-driver@3.14.0@appium-base-driver/lib/protocol/errors.js:683:14)
[debug] [W3C (bac0efb8)]     at JWProxy.command (/usr/local/lib/node_modules/appium/node_modules/_appium-base-driver@3.14.0@appium-base-driver/lib/jsonwp-proxy/proxy.js:234:19)
[HTTP] <-- POST /wd/hub/session/bac0efb8-601a-4558-a50c-f909f2ccb25a/appium/device/hide_keyboard 500 1

我还注意到有一个覆盖键盘的方法,参数如下: https://github.com/appium/java-client/blob/1991a8a0f9e4a3ff467dbb713cb5c51c8edc060f/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java

driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");

但不管我导入了必要的 class,它仍然显示 'The method hideKeyboard() in the type HidesKeyboard is not applicable for the arguments (String, String)'

有人可以帮助解决以上两个问题吗?

非常感谢!

Appium 方法 hideKeyboard() 已知在 iPhone 设备上使用时不稳定,如 Appium 当前已知的未解决问题中所列。对 iOS 设备使用此方法可能会导致 Appium 脚本挂起。 Appium 确定问题是因为 - “没有用于隐藏键盘的自动化挂钩,......而不是使用这种方法来考虑用户如何在你的应用程序中隐藏键盘,并告诉 Appium 这样做(滑动,点击某个坐标等..

如果你想隐藏键盘,你可以像下面这样写一个函数

public void typeAndEnter(MobileElement mobileElement, String keyword) {
        LOGGER.info(String.format("Typing %s ...",keyword));
        mobileElement.sendKeys(keyword, Keys.ENTER);
    }

希望对您有所帮助

Appium 方法 hideKeyboard() 已知在 iPhone 设备上使用时不稳定,如 Appium 当前已知的未解决问题中所列。对 iOS 设备使用此方法可能会导致 Appium 脚本挂起。 Appium 确定问题是因为 - "There is no automation hook for hiding the keyboard,...rather than using this method, to think about how a user would hide the keyboard in your app, and tell Appium to do that instead (swipe, tap on a certain coordinate, etc…)"

解决方法:按照 Appium 文档的建议 - 使用 Appium 自动执行用户隐藏键盘的操作。例如,如果应用程序定义了此操作,则使用滑动方法隐藏键盘,或者如果应用程序定义了 "hide-KB" 按钮,则自动单击此按钮。

另一种解决方法是使用 sendkey() 而无需单击文本输入字段。

您也可以简单地使用

driver.navigate().back();(旧版本的appium)

问题是首先试图隐藏键盘。 将 DesiredCapabilities 设置为

cap.setCapability("connectHardwareKeyboard", false);

这将使键盘默认隐藏。

通过sendKeys()做你输入数据的操作

appDriver.findElementByXPath("//XCUIElementTypeOther[@name=\"Confirm password\"]/XCUIElementTypeSecureTextField").sendKeys(confirmPassword);

完成调用后

appDriver.hideKeyboard();

然后键盘消失了。希望这有帮助

你这样定义能力。

desiredCapabilities.setCapability("unicodeKeyboard", true);
desiredCapabilities.setCapability("resetKeyboard", true);

如果您使用 Android,您可以使用 adb 隐藏键盘,从您的代码发送 adb 命令

adb shell input keyevent 111

如果您正在使用 android,请使用以下方法。

如果键盘可见(通过 id)

 driver.pressKeyCode(4);  //Android back button

其他

 logger keyboard is not active

以上方法将通过调用系统后退按钮关闭键盘。

这个问题的最佳解决方案是,只需在您的程序中添加 capability

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
    public void clickAfterFindingElement(By by) {
        try {
            getDriver().waitForCondition(ExpectedConditions.elementToBeClickable(by));
            getDriver().findElement(by).click();
        } catch (NoSuchElementException | TimeoutException e) {
            swipeUp();
            getDriver().findElement(by).click();
        }
    }


    public void hideKeyBoard() {
        if (isKeyboardShown()) {
            if (isConfigurationIOS()) {
                try {
                    clickAfterFindingElement(By.id("keyboard_done_btn"));
                } catch (Exception e) {
                    try {
                        getDriver().click(By.id("Done"));
                    } catch (Exception e1) {
                        //noop
                    }
                }
            } else {
                ((AndroidDriver) getDriver().getAppiumDriver()).pressKey(new KeyEvent(AndroidKey.BACK));
            }
        }
    }

这个解决方案对于我们在模拟器和仿真器上隐藏键盘来说非常稳定