How do I confirm a javascript popup with Capybara?

前端 未结 8 960
梦谈多话
梦谈多话 2020-12-08 01:55

I\'ve tried several examples found online, but with no luck. I am looking to confirm the confirm message of a delete link. The last attempt was the code below, but that re

相关标签:
8条回答
  • 2020-12-08 02:02

    I had to use a sleep in the webkit test since it would fail everynow and then otherwise.

    Here is what I came up with after reading everyones posts:

    if page.driver.class == Capybara::Selenium::Driver
      page.driver.browser.switch_to.alert.accept
    elsif page.driver.class == Capybara::Webkit::Driver
      sleep 1 # prevent test from failing by waiting for popup
      page.driver.browser.accept_js_confirms
    else
      raise "Unsupported driver"
    end
    
    0 讨论(0)
  • 2020-12-08 02:06

    I've had timing issues with browser dialogs in a CI environment so I'm polling for a dialog before accepting it:

    def accept_browser_dialog
      wait = Selenium::WebDriver::Wait.new(:timeout => 30)
      wait.until {
        begin
          page.driver.browser.switch_to.alert
          true
        rescue Selenium::WebDriver::Error::NoAlertPresentError
          false
        end
      }
      page.driver.browser.switch_to.alert.accept
    end
    
    0 讨论(0)
  • 2020-12-08 02:12

    try to add :js => true to your test.

    RSpec’s metadata feature can be used to switch to a different driver. Use :js => true to switch to the javascript driver, or provide a :driver option to switch to one specific driver. For example:

    it 'will use the default js driver' :js => true do
      ...
    end
    
    0 讨论(0)
  • 2020-12-08 02:13

    Adding an answer for those hitting this in 2016 and beyond. You can now use Capybara directly to accept a confirmation box. You do this by wrapping the code that cause the confirmation box to appear in the accept_confirm function.

    accept_confirm do
      click_link 'Destroy'
    end
    
    0 讨论(0)
  • 2020-12-08 02:14

    I would guess that you have to add selenium to your gem-file and configure it and capybara that capybara uses selenium as the driver.

    I think also that How to test a confirm dialog with Cucumber? is very similar to your question, especially the accepted answer.

    0 讨论(0)
  • 2020-12-08 02:15

    In Capybara its very simple to accept the model window. Even we can do the same in selenium but its little tough for people who are not aware about selenium.

    page.accept_modal #This will accept the modal window

    page.dismiss_modal #This will Reject/Dismiss the modal window

    0 讨论(0)
提交回复
热议问题