-
-
Notifications
You must be signed in to change notification settings - Fork 428
Accept or Decline Javascript Confirmation #84
Comments
maybe stubbing javascript would help: page.execute_script 'window.confirm = function () { return true }' I didn't tested, I guess this should work |
def handle_js_confirm(accept=true)
page.evaluate_script "window.original_confirm_function = window.confirm"
page.evaluate_script "window.confirm = function(msg) { return #{!!accept}; }"
yield
page.evaluate_script "window.confirm = window.original_confirm_function"
end I had seen something like this around & I use it w/capybara-webkit in a few projects. |
That's awesome, works for me too. I just changed it slightly to use ensure to make sure the function is always replaced, even if the context of the block raises and error. def handle_js_confirm(accept=true)
page.evaluate_script "window.original_confirm_function = window.confirm"
page.evaluate_script "window.confirm = function(msg) { return #{!!accept}; }"
yield
ensure
page.evaluate_script "window.confirm = window.original_confirm_function"
end |
@metaskills good idea, will have to use ensure in the future. |
Thanks. This works fine. Although I wish I didn't have to wrap steps with this. Maybe in the future we'll have a way to access any dialogs that do popup in capybara-webkit; something like what can be done in selenium. |
Thanks for sharing these solutions. |
Awesome hack! Now i can say goodbye to selenium every time I want to test something a delete action |
What if you want to test for content inside the confirmation dialog (like how selenium provides dialog.text)? Any ideas on how that would work? Thanks in advance. |
@leesmith: You could try something like this:
Within the block, you can then call |
@niklasb Thank you very much...I'll give that a shot. |
Has there been any movement on this? Where would be the best place to put these methods? I was just getting empty strings for the text confirmMsg value. |
Lol just saw this in Ruby Weekly, very handy :) Thanks for the heads up :) On Fri, Feb 17, 2012 at 5:21 AM, Jesse Cooke <
|
Prickle just make a nice wrapper for the selenium approach, so no go in On Fri, Feb 17, 2012 at 2:54 PM, Brendon Muir brendon@spike.net.nz wrote:
|
@niklasb : I like your solution. I've applied in a cucumber step defined as:
Which would be pretty cool using steps like:
However, I've got two problems:
Does anyone have this approach working? |
@shakerlxxv: Sorry, I don't see why this isn't working right now. You should do some JS debugging to figure out why it doesn't :) Maybe set it to a value different from |
@niklasb Yup. I had the same thought, if I set |
@shakerlxxv: But the replaced confirm function doesn't change the value? That's strange. |
@shakerlxxv I know this is probably way too late to help you, but in case anyone else has the same problem and finds this (like I did), make sure you are using |
Just came across a "gotcha" with this webkit method after upgrading to Rails 4.2.1: it silently succeeds even if no confirmation popup is displayed. Rails 4 requires that the http://stackoverflow.com/a/19588187/550712 so my code wasn't working but the tests were succeeding. Is there a way to get the webkit to fail if no popup is present? For now I've switched back to Selenium, which displays "Selenium::WebDriver::Error::NoAlertPresentError:" if the popup is missing. |
@nritholtz Awesome, thanks very much for that tip! |
No problem. For future reference, all methods related to modal dialogs are documented in Capybara's README. |
It would be nice if there was a way to accept or deny confirmation or alert dialogs. For instance, in Rails, the default way to delete something is to hide the action as a JS call behind a link. This method requires the user to also accept a deletion confirmation dialog to actually execute the action.
Currently I have to fallback to selenium for those scenarios. This is done using the following code:
It's also possible that capybara-webkit does this already, I just couldn't find it documented anywhere and my quick scan through the code didn't reveal such functionality.
The text was updated successfully, but these errors were encountered: