development

Capybara에서 자바 스크립트 팝업을 어떻게 확인합니까?

big-blog 2020. 11. 14. 10:51
반응형

Capybara에서 자바 스크립트 팝업을 어떻게 확인합니까?


온라인에서 찾은 몇 가지 예를 시도했지만 운이 없습니다. 삭제 링크의 확인 메시지를 확인하려고합니다. 마지막 시도는 아래 코드 였지만 그 결과 Capybara :: NotSupportedByDriverError 오류가 발생했습니다.

def confirm_dialog
  page.evaluate_script('window.confirm = function() { return true; }')
end

우선 시나리오 앞에 @javascript 태그를 추가하여 Selenium을 드라이버로 사용하도록 전환하십시오.

오이 단계의 다음 코드는 대화를 확인합니다.

page.driver.browser.switch_to.alert.accept
# or
page.driver.browser.switch_to.alert.dismiss
# or
page.driver.browser.switch_to.alert.text

@NobbZ가 말했듯이이 질문은 여기 이전에 묻고 답 했습니다. Cucumber로 확인 대화를 테스트하는 방법? .

더 많은 셀레늄 문서도 여기에서 사용할 수 있습니다. http://code.google.com/p/selenium/wiki/RubyBindings#JavaScript_dialogs


2016 년 이후에이 문제에 대한 답을 추가합니다. 이제 Capybara를 직접 사용하여 확인 상자를 수락 할 수 있습니다. 이것은 accept_confirm 함수 에 확인 상자를 표시하는 코드를 래핑하여 수행 합니다.

accept_confirm do
  click_link 'Destroy'
end

카피 바라 웹킷의 경우 :

page.driver.browser.accept_js_confirms
page.driver.browser.reject_js_confirms

여전히 작동하지만 문서에도 다음과 같이 나와 있습니다.

page.driver.accept_js_confirms!
page.driver.accept_js_confirms!

참조 https://github.com/thoughtbot/capybara-webkit을 "accept_js_confirms"를 검색,


CI 환경에서 브라우저 대화 상자에 타이밍 문제가있어 수락하기 전에 대화 상자를 폴링합니다.

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

나는 웹킷 테스트에서 수면을 사용해야했다. 매번 실패 할 것이기 때문이다.

모든 사람의 게시물을 읽은 후 생각해 낸 것은 다음과 같습니다.

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

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.


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

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

참고URL : https://stackoverflow.com/questions/6930927/how-do-i-confirm-a-javascript-popup-with-capybara

반응형