New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API for operation with modal dialogs #1322
Changes from all commits
505c0ce
cda05a6
64b8ca6
acaa674
973fc4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,11 @@ class Session | |
:save_and_open_screenshot, :reset_session!, :response_headers, | ||
:status_code, :title, :has_title?, :has_no_title?, :current_scope | ||
] | ||
DSL_METHODS = NODE_METHODS + SESSION_METHODS | ||
MODAL_METHODS = [ | ||
:accept_alert, :accept_confirm, :dismiss_confirm, :accept_prompt, | ||
:dismiss_prompt | ||
] | ||
DSL_METHODS = NODE_METHODS + SESSION_METHODS + MODAL_METHODS | ||
|
||
attr_reader :mode, :app, :server | ||
attr_accessor :synchronized | ||
|
@@ -523,6 +527,112 @@ def evaluate_script(script) | |
driver.evaluate_script(script) | ||
end | ||
|
||
## | ||
# | ||
# Execute the block, accepting a alert. | ||
# | ||
# @overload accept_alert(text, options = {}, &blk) | ||
# @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any prompt modal is matched | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writing overloads in such way doesn't produce correct documentation -http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#accept_alert-instance_method ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm -- generating the docs locally (yard 0.8.7.4) produces the correct output for me -
Execute the block, accepting a confirm. Overloads:
Parameters:
Options Hash (options):
Returns:
Raises:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its correct here too - http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session:accept_alert |
||
# @overload accept_alert(options = {}, &blk) | ||
# @option options [Numeric] :wait How long to wait for the modal to appear after executing the block. | ||
# @return [String] the message shown in the modal | ||
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found | ||
# | ||
def accept_alert(text_or_options=nil, options={}, &blk) | ||
if text_or_options.is_a? Hash | ||
options=text_or_options | ||
else | ||
options[:text]=text_or_options | ||
end | ||
|
||
driver.accept_modal(:alert, options, &blk) | ||
end | ||
|
||
## | ||
# | ||
# Execute the block, accepting a confirm. | ||
# | ||
# @overload accept_confirm(text, options = {}, &blk) | ||
# @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any prompt modal is matched | ||
# @overload accept_confirm(options = {}, &blk) | ||
# @option options [Numeric] :wait How long to wait for the modal to appear after executing the block. | ||
# @return [String] the message shown in the modal | ||
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found | ||
# | ||
def accept_confirm(text_or_options=nil, options={}, &blk) | ||
if text_or_options.is_a? Hash | ||
options=text_or_options | ||
else | ||
options[:text]=text_or_options | ||
end | ||
|
||
driver.accept_modal(:confirm, options, &blk) | ||
end | ||
|
||
## | ||
# | ||
# Execute the block, dismissing a confirm. | ||
# | ||
# @overload dismiss_confirm(text, options = {}, &blk) | ||
# @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any prompt modal is matched | ||
# @overload dismiss_confirm(options = {}, &blk) | ||
# @option options [Numeric] :wait How long to wait for the modal to appear after executing the block. | ||
# @return [String] the message shown in the modal | ||
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found | ||
# | ||
def dismiss_confirm(text_or_options=nil, options={}, &blk) | ||
if text_or_options.is_a? Hash | ||
options=text_or_options | ||
else | ||
options[:text]=text_or_options | ||
end | ||
|
||
driver.dismiss_modal(:confirm, options, &blk) | ||
end | ||
|
||
## | ||
# | ||
# Execute the block, accepting a prompt, optionally responding to the prompt. | ||
# | ||
# @overload accept_prompt(text, options = {}, &blk) | ||
# @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any prompt modal is matched | ||
# @overload accept_prompt(options = {}, &blk) | ||
# @option options [String] :with Response to provide to the prompt | ||
# @option options [Numeric] :wait How long to wait for the prompt to appear after executing the block. | ||
# @return [String] the message shown in the modal | ||
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found | ||
# | ||
def accept_prompt(text_or_options=nil, options={}, &blk) | ||
if text_or_options.is_a? Hash | ||
options=text_or_options | ||
else | ||
options[:text]=text_or_options | ||
end | ||
|
||
driver.accept_modal(:prompt, options, &blk) | ||
end | ||
|
||
## | ||
# | ||
# Execute the block, dismissing a prompt. | ||
# | ||
# @overload dismiss_prompt(text, options = {}, &blk) | ||
# @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any prompt modal is matched | ||
# @overload dismiss_prompt(options = {}, &blk) | ||
# @option options [Numeric] :wait How long to wait for the prompt to appear after executing the block. | ||
# @return [String] the message shown in the modal | ||
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found | ||
# | ||
def dismiss_prompt(text_or_options=nil, options={}, &blk) | ||
if text_or_options.is_a? Hash | ||
options=text_or_options | ||
else | ||
options[:text]=text_or_options | ||
end | ||
|
||
driver.dismiss_modal(:prompt, options, &blk) | ||
end | ||
|
||
## | ||
# | ||
# Save a snapshot of the page. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
Capybara::SpecHelper.spec '#accept_alert', :requires => [:modals] do | ||
before do | ||
@session.visit('/with_js') | ||
end | ||
|
||
it "should accept the alert" do | ||
@session.accept_alert do | ||
@session.click_link('Open alert') | ||
end | ||
expect(@session).to have_xpath("//a[@id='open-alert' and @opened='true']") | ||
end | ||
|
||
it "should accept the alert if the text matches" do | ||
@session.accept_alert 'Alert opened' do | ||
@session.click_link('Open alert') | ||
end | ||
expect(@session).to have_xpath("//a[@id='open-alert' and @opened='true']") | ||
end | ||
|
||
it "should not accept the alert if the text doesnt match" do | ||
expect do | ||
@session.accept_alert 'Incorrect Text' do | ||
@session.click_link('Open alert') | ||
end | ||
end.to raise_error(Capybara::ModalNotFound) | ||
# @session.accept_alert {} # clear the alert so browser continues to function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be commented? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes - its handled in reset now, so the line can eventually go away |
||
end | ||
|
||
it "should return the message presented" do | ||
message = @session.accept_alert do | ||
@session.click_link('Open alert') | ||
end | ||
expect(message).to eq('Alert opened') | ||
end | ||
|
||
context "with an asynchronous alert" do | ||
it "should accept the alert" do | ||
@session.accept_alert do | ||
@session.click_link('Open delayed alert') | ||
end | ||
expect(@session).to have_xpath("//a[@id='open-delayed-alert' and @opened='true']") | ||
end | ||
|
||
it "should return the message presented" do | ||
message = @session.accept_alert do | ||
@session.click_link('Open delayed alert') | ||
end | ||
expect(message).to eq('Delayed alert opened') | ||
end | ||
|
||
it "should allow to adjust the delay" do | ||
@session.accept_alert wait: 4 do | ||
@session.click_link('Open slow alert') | ||
end | ||
expect(@session).to have_xpath("//a[@id='open-slow-alert' and @opened='true']") | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options = {}