Selenium-chromedriver:无法从不可输入的键构造 KeyEvent

29 ruby-on-rails cucumber capybara selenium-chromedriver

我昨天将 Chrome 和 Chromedriver 更新到了最新版本,从那时起,我在运行 Cucumber 功能时收到以下错误消息:

....
unknown error: Cannot construct KeyEvent from non-typeable key
        (Session info: chrome=98.0.4758.80) (Selenium::WebDriver::Error::UnknownError)
      #0 0x55e9ce6a4093 <unknown>
      #1 0x55e9ce16a648 <unknown>
      #2 0x55e9ce1a9866 <unknown>
      #3 0x55e9ce1cbd29 <unknown>
      .....
Run Code Online (Sandbox Code Playgroud)

我尝试用水豚的fill_in方法填充文本字段。在调试时,我注意到 Capybara 有问题,尤其是符号@\。其他所有字符都可以毫无问题地写入文本字段。

触发错误的代码如下所示

def sign_in(user)
  visit new_sign_in_path
  fill_in 'Email', with: user.email
  fill_in 'Password', with: user.password
  click_button 'Sign in'
end
Run Code Online (Sandbox Code Playgroud)

user.email包含一个类似 的字符串"example1@mail.com"

我使用 Rails 6.1.3.1、Cucumber 5.3.0、Chromedriver 98.0.4758.48、capybara 3.35.3

该错误仅发生在标记为@javascript

您有什么想法导致此错误或如何修复它吗?

Rol*_*der 12

目前最简单的方法是固定到早期版本的 chrome 驱动程序,因此将其添加到您的水豚配置中

在红宝石中

# /test/support/system/capybara_config.rb
require 'webdrivers/chromedriver'
Webdrivers::Chromedriver.required_version = '97.0.4692.71'
Run Code Online (Sandbox Code Playgroud)

希望这个问题能够在未来的 chromedriver 版本中得到解决,它已经被提出并在这里讨论

我还尝试了重写该fill_in方法。

这不太理想,并且实际上取决于操作系统,因此请提供更好的解决方案或更新此答案。随着我的研究进展,我会尝试更新。


class ApplicationSystemTestCase < ActionDispatch::SystemTestCase

  # overriding the `fill_in` helper for filling in strings with an `@` symbol
  def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)
    return super unless with.include? "@"

    find_options[:with] = currently_with if currently_with
    find_options[:allow_self] = true if locator.nil?
    element = find(:fillable_field, locator, **find_options)
    email_front, email_back = with.split("@")

    element.send_keys(email_front)
    page.driver.browser.action
        .key_down(Selenium::WebDriver::Keys[:alt])
        .send_keys('g')
        .key_up(Selenium::WebDriver::Keys[:alt])
        .perform
    element.send_keys(email_back)
  end
end
Run Code Online (Sandbox Code Playgroud)