質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

767閲覧

[Rails]新規ユーザー登録画面のRSpecテストで"Capybara::ElementNotFound: Unable to find field ..."が解消できない。

hajsu00

総合スコア151

Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2022/05/29 09:43

編集2022/05/29 09:44

Docker上でRailsアプリのRSpecテスト実行を試みています。
新規ユーザー登録画面のsystem specで"Capybara::ElementNotFound: Unable to find field ..."が長時間解決できません。
ご助言をいただけると幸いです。

開発環境

  • ruby 3.0.1
  • Rails 6.1.4.4
  • puma 5.5.2
  • nginx 1.21.6
  • docker 20.10.12
  • docker-compos 1.29.2

前提事項

ユーザー認証にはdeviseを使用しています。

# ディレクトリ構造 myapp ├── app │ ├── assets │ ├── ... │ └── views │ ├── ... │ ├── ... │ └── users │ ├── ... │ ├── registrations │ │ ├── edit.html.erb │ │ └── new.html.erb <= ここでエラー発生 │ ├── sessions │ │ └── new.html.erb │ ├── sessions │ ├── ... │ ├── index.html.erb │ └── show.html.erb ├── ... ├── Dockerfile ├── tailwind.config.js └── ...

起きている問題

docker-compose exec app bashでコンテナ内に入りbundle exec rspecを実行すると、新規登録フォームの「アカウント名 *」のみ表題のエラーが出る。
イメージ説明

bash

1$ bundle exec rspec 2 3Running via Spring preloader in process 841 4 5User 6 User CRUD 7 ログイン前 8 ユーザー新規登録 9 フォームの入力値が正常 10 ユーザーの新規作成が成功 (FAILED - 1) 11 メールアドレス未記入 12 ユーザーの新規作成が失敗 (FAILED - 2) 13 登録済メールアドレス 14 ユーザーの新規作成が失敗 (FAILED - 3) 15 パスワードが未記入 16 ユーザーの新規作成が失敗 (FAILED - 4) 17 パスワードが6文字未満かつパスワード(確認)の入力値が異常 18 ユーザーの新規作成が失敗 (FAILED - 5) 19 20Failures: 21 22 1) User User CRUD ログイン前 ユーザー新規登録 フォームの入力値が正常 ユーザーの新規作成が成功 23 Failure/Error: fill_in 'アカウント名 *', with: "test" 24 25 Capybara::ElementNotFound: 26 Unable to find field "アカウント名 *" that is not disabled 27 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:303:in `block in synced_resolve' 28 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/base.rb:83:in `synchronize' 29 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:292:in `synced_resolve' 30 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/finders.rb:53:in `find' 31 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/node/actions.rb:91:in `fill_in' 32 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/session.rb:772:in `fill_in' 33 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `call' 34 # /usr/local/bundle/gems/capybara-3.36.0/lib/capybara/dsl.rb:53:in `fill_in' 35 # ./spec/systems/users_spec.rb:13:in `block (6 levels) in <main>' 36 37 2) User User CRUD ログイン前 ユーザー新規登録 メールアドレス未記入 ユーザーの新規作成が失敗 38 Failure/Error: fill_in 'アカウント名 *', with: 'test' 39 40 Capybara::ElementNotFound: 41 Unable to find field "アカウント名 *" that is not disabled 42...

関連するコード

html.erb

1# myapp/views/users/registrations/new.html.erb 2... 3<div class="bg-white py-6 sm:py-8 lg:py-12"> 4 <div class="max-w-screen-2xl px-4 md:px-8 mx-auto justify-center"> 5 <h2 class="text-gray-800 text-2xl lg:text-3xl font-bold text-center mb-4 md:mb-8">アカウント登録</h2> 6 <%= form_with(model: @user, url: registration_path(@user)) do |f| %> 7 <div class="flex justify-center items-center p-4"> 8 <%= render "devise/shared/error_messages", resource: resource %> 9 </div> 10 <div class="max-w-lg border border-gray-300 shadow rounded-lg mx-auto"> 11 <div class="flex flex-col gap-4 p-4 md:p-8"> 12 <div> 13 <%= f.label :name, "アカウント名 *", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 14 <%= f.text_field :name, autofocus: true, autocomplete: "name", class: 'w-full bg-gray-50 text-gray-800 border border-gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2' %> 15 </div> 16 <div> 17 <%= f.label :email, "メールアドレス *", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 18 <%= f.text_field :email, autofocus: true, autocomplete: "email", class: 'w-full bg-gray-50 text-gray-800 border border-gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2' %> 19 </div> 20...

試したこと

よくあるエラーの様で、対象となるfieldのname属性の値を調べてfill inの要素にするなどの方法を試しましたが結果は変わりませんでした。
https://qiita.com/pooooon/items/4fbc429d07e4b65ed928
https://stackoverflow.com/questions/70671277/capybara-with-rails-6-1-4-4-element-not-found

html

1<!-- Chromeブラウザ --> 2 3<div> 4 <label class="inline-block text-gray-800 text-sm sm:text-base mb-2" for="user_name">アカウント名 *</label> 5 <input autofocus="autofocus" autocomplete="name" class="w-full bg-gray-50 text-gray-800 border border- 6 gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2" type="text" 7 value="" name="user[name]" id="user_name"> 8</div> 9<div> 10 <label class="inline-block text-gray-800 text-sm sm:text-base mb-2" for="user_email">メールアドレス *</label> 11 <input autofocus="autofocus" autocomplete="email" class="w-full bg-gray-50 text-gray-800 border border- 12 gray-300 focus:ring ring-indigo-500 rounded outline-none transition duration-100 px-3 py-2" type="text" 13 value="" name="user[email]" id="user_email"> 14</div> 15...

ruby

1# myapp/spec/systems/users_spec.rb 2 3require 'rails_helper' 4 5RSpec.describe User, type: :system do 6 describe 'User CRUD' do 7 describe 'ログイン前' do 8 let(:user) { create(:user) } 9 let(:other_user) { create(:user) } 10 11 describe 'ユーザー新規登録' do 12 context 'フォームの入力値が正常' do 13 it 'ユーザーの新規作成が成功' do 14 visit new_user_registration_path 15 fill_in "user[name]", with: "test" 16 fill_in 'メールアドレス *', with: 'test@example.com' 17 fill_in 'パスワード', with: 'password' 18 fill_in 'パスワード(確認) *', with: 'password' 19 click_button '認証メールを送信する' 20 expect(current_path).to eq new_user_session_path 21 expect(page).to have_content '本人確認用のメールを送信しました。メール内のリンクからアカウントを有効化させてください。' 22 end 23 end 24 context 'メールアドレス未記入' do 25 it 'ユーザーの新規作成が失敗' do 26 visit new_user_registration_path 27 fill_in "user[name]", with: 'test' 28 fill_in 'メールアドレス *', with: nil 29 fill_in 'パスワード', with: 'password' 30 fill_in 'パスワード(確認) *', with: 'password' 31 click_button '認証メールを送信する' 32 expect(current_path).to eq users_path 33 expect(page).to have_content 'Eメールを入力してください' 34 end 35 end 36...

※"user_name"と"user[name]"の両方を試しました。


解決につながるヒントをいただけないでしょうか。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

satoru3106

2023/07/01 05:27

コメント失礼致します。 こちらのようなエラーで私自身も行き詰まっており、その後の状況についてお伺いしたくご連絡させて頂きました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問