GeeCON Prague 2014*
The moose likes HTML, CSS and JavaScript and the fly likes Capturing dialogs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Capturing dialogs" Watch "Capturing dialogs" New topic
Author

Capturing dialogs

Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Christian,

In your book, do you cover examples on how to capture javascript popups such as alert dialogs and how to test that they did/did not appear?

thanks,
Jeff
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
Hi Jeff,

No, there are no direct tests for using alert and other browser popups. The alert function could possibly be stubbed/mocked in order to verify that it's being called, but as a host function, that might prove difficult in some browsers. Alternatively, you can define your own function, e.g. mylib.dialog("Hello") which by default uses alert, and then test that this function is used as expected.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Does js-test-driver provide a way to mock this behavior? Or would I need to look into another framework for that?

thanks,
Jeff
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
JsTestDriver does not have any mocking/stubbing capabilities. I use Sinon.JS (disclaimer: I wrote it), which works great with JsTestDriver.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Excellent, thanks Christian.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
That's the easy way to do it If you use a library, like Sinon.JS, you can do



The upshot of using a library is

1) You get a richer interface for behavior verification (i.e. was it called? how many times? what arguments did it receive? and so on)
2) Sinon sandboxes the test and reverts the original method after the test has run

Beware that IE not always allow you to override host methods like this. I don't remember if it screams when overriding alert, but you'll notice soon enough
 
Consider Paul's rocket mass heater.
 
subject: Capturing dialogs