【问题标题】:Appium hidekeyboard() methodAppium hidekeyboard() 方法
【发布时间】:2019-07-26 11:04:06
【问题描述】:

我正在尝试自动化一个邮政编码输入字段,该字段会弹出一个数字键盘供用户输入。 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");

但不管我是否导入了必要的类,它仍然显示“HidesKeyboard 类型中的方法 hideKeyboard() 不适用于参数 (String, String)”

任何人都可以帮助解决上述 2 个问题中的任何一个吗?

非常感谢!

【问题讨论】:

    标签: appium appium-ios


    【解决方案1】:

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

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

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

    【讨论】:

    • 我发现,使用 Appium for JavaScript,以下命令对我有用 await browser.sendKeys(['\n']);
    【解决方案2】:

    已知 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);
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      你也可以简单地使用

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

      【讨论】:

        【解决方案4】:

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

        cap.setCapability("connectHardwareKeyboard", false);
        

        这将默认隐藏键盘。

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

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

        一次通话

        appDriver.hideKeyboard();
        

        键盘消失了。希望这会有所帮助

        【讨论】:

        • 这不适用于真实设备。它仅适用于模拟器
        【解决方案5】:

        您可以这样定义功能。

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

        【讨论】:

          【解决方案6】:

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

          adb shell input keyevent 111
          

          【讨论】:

            【解决方案7】:

            如果您使用的是android,请使用以下方法。

            如果键盘可见(按 id)

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

            其他

             logger keyboard is not active
            

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

            【讨论】:

              【解决方案8】:

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

              capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
              

              【讨论】:

                【解决方案9】:
                    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));
                            }
                        }
                    }
                
                

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

                【讨论】:

                  猜你喜欢
                  • 2020-01-30
                  • 1970-01-01
                  • 2019-12-11
                  • 1970-01-01
                  • 2015-07-08
                  • 2022-12-22
                  • 2019-09-14
                  • 2023-03-29
                  • 2015-03-27
                  相关资源
                  最近更新 更多