在iOS上的Appium测试中关闭键盘 [英] Dismissing keyboard in appium test on iOS

查看:586
本文介绍了在iOS上的Appium测试中关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS的Appium测试中关闭键盘?输入文字后,我会在控制台中看到此条目

How do you dismiss the keyboard in an Appium test on iOS? I get this entry in the console after entering text

[INSTSERVER] Sending command to instruments: if (!au.mainApp().keyboard().isNil()) {
  var key = au.mainApp().keyboard().buttons()['Done']
  if (key.isNil()) {
    var startY = au.mainApp().keyboard().rect().origin.y - 10;
    var endY = au.mainWindow().rect().size.height - 10;
    au.flickApp(0, startY, 0, endY);
  } else {
    key.tap();
  }
  au.delay(1000);
}

我可以看到它假设键盘上的按钮为完成",即为返回".查看文档,我应该可以通过以下方式做到这一点 https://github.com/appium/ruby_lib/blob/ef20cdd09cdb3dac32b789144b17ed2daf745a8d/docs/ios_docs.md#hide_keyboard

I can see it is assuming the button is 'Done' on my keyboard it is 'return'. Looking at the documentation I should be able to do this in the following way https://github.com/appium/ruby_lib/blob/ef20cdd09cdb3dac32b789144b17ed2daf745a8d/docs/ios_docs.md#hide_keyboard

我已经使用以下代码尝试过此操作:

I have tried this using the following code:

When(/^I enter the text "(.*?)"$/) do |user_text|
  textfield( "My Textbox" ).type user_text
  hide_keyboard( "Return" )
end

尽管如此,它仍然挂着寻找完成"按钮.您如何覆盖Appium查找的键.我已经在此处上传了带有代码的Git存储库: GitHub存储库

Despite this it still hangs looking for the 'Done' button. How do you override which key Appium looks for. I have uploaded a Git repository with my code here: GitHub Repository

当我使用完成"作为键盘按钮返回时,它可以正常工作.问题是我的应用程序未使用完成".

When I use 'Done' as the keyboard button to Return it works. Problem is my app doesn't use 'Done'.

推荐答案

我最终通过像这样在Appium中修补键盘解雇代码来设法解决了这个问题.

I managed to fix this issue in the end by resorting to patching the keyboard dismissal code in Appium like this.

功能/support/env.rb

module Appium
  module Ios
    def patch_webdriver_element
      Selenium::WebDriver::Element.class_eval do
        # Enable access to iOS accessibility label
        # accessibility identifier is supported as 'name'
        def label
          self.attribute('label')
        end

        # Cross platform way of entering text into a textfield
        def type text

          $driver.execute_script %(au.getElement('#{self.ref}').setValue('#{text}');)
        end 
      end 
    end 

    def hide_ios_keyboard close_key='Done'
      dismiss_keyboard = (<<-JS).strip
      if (!au.mainApp().keyboard().isNil()) {
        var key = au.mainApp().keyboard().buttons()['#{close_key}']
        if (key.isNil()) {
          var startY = au.mainApp().keyboard().rect().origin.y - 10;
          var endY = au.mainWindow().rect().size.height - 10;
          au.flickApp(0, startY, 0, endY);
        } else {
          key.tap();
        }
      }
      JS

      ignore do
        wait_true(5) do
          execute_script '!au.mainApp().keyboard().isNil()'
        end

        # dismiss keyboard
        execute_script dismiss_keyboard
      end

      wait_true(5) do
        execute_script 'au.mainApp().keyboard().isNil()'
      end
    end
  end 
end 

然后在我的步骤定义中,我可以在输入文本后手动关闭键盘,从而允许我指定在键盘上调用"Return"按钮的方式.

Then within my step definitions I can manually dismiss the keyboard after entering text allowing me to specify what the 'Return' button is called on the keyboard.

def enter_postcode( postcode )
  textfield( "postcode" ).type postcode
  hide_ios_keyboard('Return')
end

这篇关于在iOS上的Appium测试中关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆