概要
目標と課題を管理するWebアプリを開発中です。
作成された目標にコメントする機能があるのですが、
その部分のテストがうまく実装できていません。
問題
既存コメント付近の編集ボタンを押したときにAjaxでコメントを
フォームに入力されているような表示に切り替えるのですが、
Capybaraでテストを実行するとフォームの表示になりません。
ローカル環境で自分でクリックした場合は想定通りに表示が切り替わるので、
実装自体はできていると思われます。
実装
RSpec.describe "Comments", type: :system, js: true do scenario "コメントを編集できる" do goal = FactoryBot.create(:goal) comment = FactoryBot.create(:comment, goal: goal, user: goal.user) sign_in_as goal.user click_link goal.content find("a", id:"edit-comment").click fill_in "js-textarea-comment-#{comment.id}", with: "test comment modify" click_on "comment-update-button" expect(page).to have_content "test comment modify" end end
「js-edit-comment-button」クラスのaタグをクリックすると、
コメント部分がフォームに切り替わります
view
1 <div> 2 <a data-comment-id=<%= comment.id %> class="js-edit-comment-button" id="edit-comment"> 3 <i class="fas fa-edit text-primary"></i> 4 </a> 5 <%= link_to comment_path(comment.id), method: :delete, data: { confirm: "コメントを削除します。よろしいですか?"}, id: "remove-comment" do %> 6 <i class="fas fa-trash-alt text-primary"></i> 7 <% end %> 8 </div>
下記の処理で表示を切り替え
js
1 $(".js-edit-comment-button").on("click", function () { 2 console.log("called jquery") 3 const commentId = $(this).data('comment-id'); 4 const commentLabelArea = $('#js-comment-label-' + commentId); 5 const commentTextArea = $('#js-textarea-comment-' + commentId); 6 const commentButton = $('#js-comment-button-' + commentId); 7 8 commentLabelArea.hide(); 9 commentTextArea.show(); 10 commentButton.show(); 11 });
エラーログ
エラー箇所が「クリック後に表示されるフォームに対して入力する処理」であり、
「入力フォームが見つからない」というログが出ています。
そのため、編集ボタンはクリックできているが、表示が切り替わらず
フォームが表示されていないという状態だと考えています。
Failures: 1) Comments コメントを編集できる Failure/Error: fill_in "js-textarea-comment-#{comment.id}", with: "test comment modify" Capybara::ElementNotFound: Unable to find visible field "js-textarea-comment-175" that is not disabled [Screenshot]: tmp/screenshots/failures_r_spec_example_groups_comments_コメントを編集できる_762.png # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/node/finders.rb:303:in `block in synced_resolve' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/node/base.rb:83:in `synchronize' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/node/finders.rb:292:in `synced_resolve' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/node/finders.rb:53:in `find' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/node/actions.rb:91:in `fill_in' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:768:in `block (2 levels) in <class:Session>' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:58:in `block (2 levels) in <module:DSL>' # ./spec/system/comments_spec.rb:47:in `block (2 levels) in <top (required)>'
試したこと
・sleepでAjax処理が完了するまで待機
・aタグで実装している部分をbuttonタグに変更
・click_on,click_linkなどのクリック方法
・gem「poltergeist」の使用、phantomjsのインストール
あなたの回答
tips
プレビュー