Rspec Does Not Test Confirm Dialog

Asked 2 years ago, Updated 2 years ago, 378 views

I am currently creating a test under spec/system as follows, but it does not work.
Only the following articles are up to date on Rspec confirm.
I don't know how to solve it.I would appreciate it if you could let me know.

This article is used as a reference to implement it.
[Rails] Test the confirm dialog with Selenium/RSpec - Qiita

The following error appears:

Capybara::NotSupportedByDriverError:Capybara::Driver::Base#dismiss_modal
Capybara::NotSupportedByDriverError: Capybara::Driver::Base#accept_modal

The code you write when deleting data

<%=link_to "Delete photo", post, method::delete, id:'delete_button', data:{confirm:"Do you want to delete it?" }%>

spec/system/aaa_spec.rb

require 'rails_helper'

RSpec.describe "AAA", type: :system, js:true do
  let(:user) {FactoryBot.create(:user)}
  let(:other_user) {FactoryBot.create(:user)}
  let(:img_path) {Rails.root.join("spec/img/aaa.jpg")}

  context "when a user posts aimg" do
    it "test1" do
      visit new_user_session_path
      fill_in "mail address", with:user.email
      fill_in "password", with:user.password
      find('.signin-btn').click_link_or_button "Login"
      expect(page).to have_current_path root_path

      expect{
        upload_img(img_path)
      }.to change {user.posts.count}.by(1)

      expect(page).to have_current_path user_path(user)
      expect(page).to have_text "Number of photos (1)"
      expect(page).to have_selector '#post-1'
      expect(page).to have_selector '#favorite-form-1'

      click_button "Register Favorites"
      expect(user.favorites.count).toeq1

      visit users_path
      expect(page).to have_selector '#post-1'
      expect(page).not_to have_selector'#favorite-form-1'

      visit user_path(user)
      expect(page).to have_selector '#favorite-form-1'
      click_button "Cancel Favorites"
      expect(user.favorites.count).toeq0

      visit root_path
      find('#post-1').find('img').click
      expect(page).to have_selector'#showImgModal-1'
      expect(page).to have_selector '.post-delete', text: 'Delete Photo'

      expect(page).to have_selector '# comment-form-1'
      without('#comment-form-1')do
        fill_in "Comments", with: "Hello"
        click_link_or_button "Comment"
      end
      expect(user.comments.count).toeq1
      expect(page).to have_selector '.comment', text: "Delete Comment"

      page.dismiss_confirm("Do you want to delete it?")do
        click_on —delete_button
      end
      page.accept_confirm do
        click_on —delete_button
      end
    end
  end
end

I changed the part of the code in question to the one below, and all the tests went through.
Is this really right?

 page.dismiss_confirm("Do you want to delete it?")do
  click_on —delete_button
end
page.accept_confirm do
  click_on —delete_button
end
↓↓
click_link "Delete Photo"
expect(page).to have_content "Photo Deleted"

support/capybara.rb

RSpec.configure do | config |
  config.before(:each, type::system)do
    drive_by —rack_test
  end
end
Capybara.javascript_driver=: selenium_chrome_headless

ruby-on-rails ruby rspec capybara

2022-09-30 21:49

1 Answers

https://github.com/teamcapybara/capybara#racktest
According to the official documentation, Rack::Test does not support JavaScript.Therefore, modals and so on will not work.

support/capybara.rbcontents

RSpec.configure do | config |
  config.before(:each, type::system)do
    drive_by —Selenium_chrome_headless
  end
end
Capybara.default_driver=:rack_test
Capybara.javascript_driver=: selenium_chrome_headless

Why don't you do that?This setting also allows JavaScript to be tested by using the selenium_chrome_headless driver for System Specs that may require JavaScript to run, while usually using the fast rack_test driver.

Incidentally, the error is
https://github.com/teamcapybara/capybara/blob/2ffbb3c6a55b9b3de13819b171c331ef9d125d4e/lib/capybara/driver/base.rb#L120-L145
It appears to be happening in .


2022-09-30 21:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.