From e74bfa092e17bc8d53c3c7a41f246d7745a59365 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 9 Oct 2017 01:37:45 -0700 Subject: [PATCH] handle default prompt value for headless chrome --- lib/capybara/selenium/driver.rb | 14 +++++++----- lib/capybara/spec/public/test.js | 8 +++++++ .../spec/session/accept_prompt_spec.rb | 22 +++++++++++++++++++ lib/capybara/spec/views/with_js.erb | 4 ++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 436c6c0a..6f5052ac 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -252,7 +252,9 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base else yield if block_given? modal = find_modal(options) + modal.send_keys options[:with] if options[:with] + message = modal.text modal.accept message @@ -391,13 +393,13 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base } window.capybara.add_handler(modal_handler); - window.alert = window.confirm = function(str) { - window.capybara.handler_called(modal_handler, str); + window.alert = window.confirm = function(str = "") { + window.capybara.handler_called(modal_handler, str.toString()); return #{accept ? 'true' : 'false'}; - }; - window.prompt = function(str) { - window.capybara.handler_called(modal_handler, str); - return #{accept ? "'#{response_text}'" : 'null'}; + } + window.prompt = function(str = "", default_text = "") { + window.capybara.handler_called(modal_handler, str.toString()); + return #{accept ? (response_text.nil? ? "default_text" : "'#{response_text}'") : 'null'}; } JS execute_script script diff --git a/lib/capybara/spec/public/test.js b/lib/capybara/spec/public/test.js index 84ed47a3..9f1c9233 100644 --- a/lib/capybara/spec/public/test.js +++ b/lib/capybara/spec/public/test.js @@ -109,6 +109,14 @@ $(function() { $(this).attr('response', response); } }); + $('#open-prompt-with-default').click(function() { + var response = prompt('Prompt opened', 'Default value!'); + if(response === null) { + $(this).attr('response', 'dismissed'); + } else { + $(this).attr('response', response); + } + }); $('#open-twice').click(function() { if (confirm('Are you sure?')) { if (!confirm('Are you really sure?')) { diff --git a/lib/capybara/spec/session/accept_prompt_spec.rb b/lib/capybara/spec/session/accept_prompt_spec.rb index 24e29d5c..286ed95f 100644 --- a/lib/capybara/spec/session/accept_prompt_spec.rb +++ b/lib/capybara/spec/session/accept_prompt_spec.rb @@ -11,6 +11,13 @@ Capybara::SpecHelper.spec '#accept_prompt', requires: [:modals] do expect(@session).to have_xpath("//a[@id='open-prompt' and @response='']") end + it "should accept the prompt with no message when there is a default" do + @session.accept_prompt do + @session.click_link('Open defaulted prompt') + end + expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='Default value!']") + end + it "should return the message presented" do message = @session.accept_prompt do @session.click_link('Open prompt') @@ -25,6 +32,21 @@ Capybara::SpecHelper.spec '#accept_prompt', requires: [:modals] do expect(@session).to have_xpath("//a[@id='open-prompt' and @response='the response']") end + it "should accept the prompt with a response when there is a default" do + @session.accept_prompt with: 'the response' do + @session.click_link('Open defaulted prompt') + end + expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='the response']") + end + + it "should accept the prompt with a blank response when there is a default", :focus_ do + pending "Geckodriver doesn't set a blank response currently" if @session.respond_to?(:mode) && @session.mode.to_s == "selenium_marionette" + @session.accept_prompt with: '' do + @session.click_link('Open defaulted prompt') + end + expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='']") + end + it "should accept the prompt if the message matches" do @session.accept_prompt 'Prompt opened', with: 'matched' do @session.click_link('Open prompt') diff --git a/lib/capybara/spec/views/with_js.erb b/lib/capybara/spec/views/with_js.erb index 7de24afe..05ec6d50 100644 --- a/lib/capybara/spec/views/with_js.erb +++ b/lib/capybara/spec/views/with_js.erb @@ -96,6 +96,10 @@ Open prompt

+

+ Open defaulted prompt +

+