3
votes

With RSpec and Capybara I'm trying to test a delete function on a webpage.

The code of the button in the view is:

<%= link_to "&times".html_safe, material, method: :delete, data: { confirm: 'Are you sure?' }

I want to write a RSpec test for it to be able to click the OK button in the confirm box. So far I got the following

feature 'Delete materials' do
  before do
    @user = FactoryGirl.create(:user)
    @group = FactoryGirl.create(:group)
    @user.groups << @group
    @material_1 = FactoryGirl.create(:material, user: @user, group: @group)
    login_as(@user, scope: :user)
  end

  scenario "in the index should give a confirmation box", js: true do
    visit materials_path
    expect(Material.count).to eq(1)

    accept_confirm do
      find('a[data-method="delete"]').click
      click_link "OK"
    end

    expect(Material.count).to eq(0)
  end
end

The error that I keep getting is:

Selenium::WebDriver::Error::UnhandledAlertError:
       unexpected alert open: {Alert text : Are you sure?}
         (Session info: chrome=56.0.2924.87)
         (Driver info: chromedriver=2.27.440175(9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.8.0-39-generic x86_64)

I googled and search around and one solution was that The chromedriver might not be up-to-date. As far as I can see I'm using the latest drivers. Another solution was to use the accept_confirm block but this didn't help yet.

For the record I'm using the Selenium-webdriver + chromedriver-helper gem for testing with Google Chrome.

1

1 Answers

10
votes

Alerts are typically client-side JavaScript. Try this above your click delete action:

page.driver.browser.switch_to.alert.accept

or .dismiss if that's what you need.