在iOS上的appium测试中解除键盘

时间:2014-08-06 10:21:52

标签: appium

如何在iOS上的Appium测试中关闭键盘?输入文本后,我在控制台中输入了这个条目

[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

执行此操作

我使用以下代码尝试了这个:

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

尽管如此,仍然在寻找“完成”按钮。你如何覆盖Appium寻找的关键。我在这里使用我的代码上传了一个Git存储库:GitHub Repository

当我使用'完成'作为键盘按钮返回时它起作用。问题是我的应用程序没有使用'完成'。

1 个答案:

答案 0 :(得分:1)

我设法通过修补Appium中的键盘解雇代码来解决这个问题。

功能性/支撑性/ 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 

然后在我的步骤定义中,我可以在输入文本后手动关闭键盘,允许我指定在键盘上调用“返回”按钮的内容。

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