• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Capturing dialogs

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent, thanks Christian.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Christian Johansen
author
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic