LoginSignup
1
0

More than 3 years have passed since last update.

RSpec のsystem spec 実行時にselenium でEnter キーの押下をシュミレートする

Last updated at Posted at 2021-04-28

RSpec のsystem spec 実行時にselenium でEnter キーの押下をシュミレートする

環境

  • macOS 10.15.6
  • Ruby 2.5.7
  • Rails 5.2.3
  • rspec-rails 4.0.1
  • capybara 3.32.2
  • selenium-webdriver 3.142.7

参照

Keyboard :: Documentation for Selenium

send_keys(special)-Ruby

ruby on rails - How to simulate pressing Enter in Rspec - Stack Overflow

使えるRSpec入門・その4「どんなブラウザ操作も自由自在!逆引きCapybara大辞典」 - Qiita

目標

qiita.png

上記のような検索実行ボタンの無い検索フォームでテキスト入力後にEnter キーを押して検索を実行する動作をシュミレートしたい

解決策

send_keys メソッドを使う。

下記のような検索フォームに検索テキストとしてkeyword を入力後にEnter キー押下した際の検索結果のテストを想定しています。

<form>
  <input placeholder='Search' id="keyword_search">
</form>
it '検索結果のテスト', js: true do
  #検索フォームにkeyword を入力
  fill_in 'Search , with: 'keyword'
  #テキスト入力後のEnter キー押下
  find("#keyword_search").send_keys :return
  #検索結果の検証
  expect(page).to have_content 'keyword'
end

ポイント

selenium 以外のヘッドレスブラウザとの情報が色々と混ざってしまったので整理

  • :return:enter でも可
  • :return は小文字でないとNG
  • js: true でJavaScript を有効にする
  • find("#keyword_search").native.send_keys :return ではNG, .native はseleniumでは不要

学び

Poltergeist, capybara-webkit 等の他のヘッドレスブラウザ毎に微妙に記述が異なるので使用する環境できちんと調べる事が重要

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0