jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

Accessing page or driver to accept a confirmation box. #334

Closed TrangPham closed 9 years ago

TrangPham commented 9 years ago

This is a bit of a question.

Currently I am seeing this error, because I'm using a confirm("Are you sure?") dialog.

[remote server] file:///var/folders/b5/_nsj8gdj0rb3qkvkhmxcvylc0000gn/T/webdriver-profile20150316-35190-3rm1h6/extensions/fxdriver@googlecode.com/components/command-processor.js:9580:in `fxdriver.modals.closeUnhandledAlert/<': Unexpected modal dialog (text: Are you sure?): "Are you sure?" (Selenium::WebDriver::Error::UnhandledAlertError)

I am using selenium web driver and am not sure how to access the page or the driver so that I could do:

page.driver.browser.switch_to.alert.accept

I got this from: https://robots.thoughtbot.com/interacting-with-a-javascript-confirmation-from

I see in one of the tests for teaspoon that there is one that visits '/teaspoon' and then does an expect(page). But I am also using magic_lamp for fixtures. Could this be why the page variable is not accessible for me?

jejacks0n commented 9 years ago

I'm a little unsure what you're asking. The test you're looking at in teaspoon is a capybara test, and is just testing teaspoon usage and has nothing to do with what you may be trying to do.

If I were to take a stab at what you're trying to do, you're calling a method in javascript that in turn calls alert -- what you probably want to do is to to stub (spy, whatever) window.alert -- then assert that the value of the alert is what you expect to see -- or don't that's up to you.

Teaspoon has nothing to do with capybara, which is what that thoughtbot article is about, and runs strictly in the browser.

Maybe a better example of spying on window.alert can be found here? https://github.com/jejacks0n/mercury/blob/1cc637b0bccea19085f824d2881c6513ed5ee8ae/spec/javascripts/mercury/mercury_spec.js.coffee#L35

TrangPham commented 9 years ago

@jejacks0n Ah thank you! Your advice helped me find the correct answer, which was to use sinon to stub out the confirm dialog as follows:

sinon.stub(window, "confirm").returns(true);