【问题标题】:Dismissing keyboard in appium test on iOS在 iOS 上的 appium 测试中关闭键盘
【发布时间】:2014-09-29 05:49:34
【问题描述】:

如何在 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

当我使用“完成”作为键盘按钮返回时,它可以工作。问题是我的应用没有使用“完成”。

【问题讨论】:

    标签: appium


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2014-11-12
      • 1970-01-01
      • 2018-11-26
      相关资源
      最近更新 更多