오이로 확인 대화 상자를 테스트하는 방법은 무엇입니까?
-
20-09-2019 - |
문제
나는 오이와 Capybara와 함께 Ruby on Rails를 사용하고 있습니다.
간단한 확인 명령을 테스트하는 방법 ( "확실합니까?")?
또한이 문제에 대한 추가 문서를 어디에서 찾을 수 있습니까?
해결책
불행히도 Capybara에서 할 수있는 방법이없는 것 같습니다. 그러나 Selenium 드라이버 (및 JavaScript를 지원하는 다른 드라이버)와 함께 테스트를 실행하는 경우 해킹 할 수 있습니다. 확인 대화 상자를 일으키는 작업을 수행하기 직전에 confirm
항상 진실을 반환하는 방법. 이렇게하면 대화 상자가 표시되지 않으며 사용자가 OK 버튼을 누른 것처럼 테스트가 계속 될 수 있습니다. 반전을 시뮬레이션하려면 거짓을 반환하도록 간단히 변경하십시오.
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
다른 팁
셀레늄 드라이버 이제 이것을 지원합니다
Capybara에서 다음과 같이 액세스 할 수 있습니다.
page.driver.browser.switch_to.alert.accept
또는
page.driver.browser.switch_to.alert.dismiss
또는
page.driver.browser.switch_to.alert.text
이 두 웹 단계를 구현했습니다 /features/step_definitions/web_steps.rb
:
When /^I confirm popup$/ do
page.driver.browser.switch_to.alert.accept
end
When /^I dismiss popup$/ do
page.driver.browser.switch_to.alert.dismiss
end
표시되는 메시지를 구체적으로 테스트하려면 여기에 특히 해킹하는 방법이 있습니다. 나는 그것을 아름다운 코드로 보증하지는 않지만 작업을 완료합니다. 로드해야합니다 http://plugins.jquery.com/node/1386/release, jQuery를 원하지 않으면 기본적으로 쿠키를하도록 변경하십시오.
이런 종류의 이야기를 사용하십시오.
Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed
그리고이 단계
Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
@expected_message = message
end
Given /^I want to click "([^"]*)"$/ do |option|
retval = (option == "Ok") ? "true" : "false"
page.evaluate_script("window.confirm = function (msg) {
$.cookie('confirm_message', msg)
return #{retval}
}")
end
Then /^the confirmation box should have been displayed$/ do
page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
page.evaluate_script("$.cookie('confirm_message', null)")
end
Capybara의 현재 릴리스를 위해 이것을 업데이트합니다. 오늘날 대부분의 Capybara 드라이버는 모달 API를 지원합니다. 확인 모달을 수락하려면
accept_confirm do # dismiss_confirm if not accepting
click_link 'delete' # whatever action triggers the modal to appear
end
이것은 오이와 같은 오이로 사용할 수 있습니다
When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
accept_confirm msg do
click_button(button)
end
end
이름 지정된 버튼을 클릭 한 다음 텍스트 일치하는 MSG가있는 확인란을 수락합니다.
그만큼 Capybara-Webkit 드라이버도 이것을 지원합니다.
Scenario: Illustrate an example has dialog confirm with text
#
When I confirm the browser dialog with tile "Are you sure?"
#
=====================================================================
my step definition here:
And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
if page.driver.class == Capybara::Selenium::Driver
page.driver.browser.switch_to.alert.text.should eq(title)
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.confirm_messages.should eq(title)
page.driver.browser.accept_js_confirms
else
raise "Unsupported driver"
end
end
가시 Selenium 및 Webkit에서 팝업 작업을위한 편리한 편의 방법이 추가됩니다.
이 요점 Capybara 드라이버와 함께 Rails 2 및 3에서 JS 확인 대화 상자를 테스트하는 단계가 있습니다.
이전 답변의 적응이지만 jQuery 쿠키 플러그인이 필요하지 않습니다.
운이없는 위의 답변을 시도했습니다. 결국 이것은 나에게 효과적이었습니다.
@browser.alert.ok