5

Use case is this: I want to unit test (in browser, QUnit or something of the kind) a code run in a page. One of the things a page can do is to navigate away to another page. I have problem catching this event, because:

  • beforeunload cannot stop the action (so the first navigation-away breaks my test)
  • trying to redefine window.location or window.location.href with getter returning old value and spying setter is prohibited, too

I understand there are security reasons that disallow possibility to stop navigating away, but for development it is really useful to be able to do so.

Is there any possibility to do this (I have no direct control over test runner, so I can't just load the code in the iframe and let it navigate and then examine location of the iframe)?

EDIT: To be a little more specific: I want to test, whether, based on the logged/connected status from facebook (I use facebook-stub from github to mock fb), there is the right handler is installed on the login button (say, $('#login-btn')), which by clicking navigates the page to facebook oauth dialog, server-flow (the details here are not important).

So I would like to be able to do this kind of thing:

// set up fb mock to not be connected
fbAsyncInit(); // simulate the startup of app
$('#login-btn').click();
equal(document.location.href, "http://www.facebook.com/oauth/...", "OAuth dialog not started.");

but if course, without actual navigation. How to do the test?

j0k
  • 22,600
  • 28
  • 79
  • 90

3 Answers3

5

What you are doing is really not unit testing, more like acceptance testing.

You should probably look into selenium web driver which can easily test stuff like page navigation.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Well, yes and no. I am not testing a page as a whole, I am really testing just the behaviour of one unit, at the specific condition doing specific things. In any "normal" harness, I would be able to mock the navigation part and it would be good old unit test. Or maybe no, if the thing I want to test is $('#btn').click() to check the right handler was installed there in a set circumstances... looks like st in between: "the handler was installed in a circumstances" is a unit-test thing, but "that handler does correct navigation" is more acceptance... :-/ and handler can choose url dynamically –  Nov 15 '11 at 12:23
1

I'm seeing now that you can't spy on a setter (which is what I'm recommending below). I've already written up the solution, so I think I'll post it in case someone without that restriction should visit this page...

Define a publicly-accessible function to navigate, for instance:

MyNameSpace.navigate = function( url ) {
    window.location.href = url;
};

Now, in your tests, you can stub out the call to MyNameSpace.navigate() and ensure it is invoked properly. Here's an example (not sure what testing framework you're using, so let me know if the implementation is unclear).

Set up and tear down:

setUp( function() {
    this._navigate = MyNameSpace.navigate;
    MyNameSpace.navigate = function( url ) {
        this.calledWith = url;
    };
});
tearDown( function() {
    MyNameSpace.navigate = this._navigate;
});

And your test, re-implemented:

test( function() {
    // set up fb mock to not be connected
    fbAsyncInit(); // simulate the startup of app
    $('#login-btn').click();
    equal(MyNameSpace.navigate.calledWith, "http://www.facebook.com/oauth/...", "OAuth dialog not started.");
});

Your idea to query window.location.href would not only test your application code, but the browser itself (i.e. does the browser redirect properly when my application sets window.location.href). A dedicated navigate method allows you to test that your application internals are operating as expected and simultaneously prevent the browser from responding.

Mike
  • 123
  • 1
  • 5
  • Well, I came up with that solution, too (I have `App` namespace there, where various config of application is put on the beginning and later is populated with some singletons, like socket.io stream to server), but it is not really a valid solution - I must change code _for the sake of the tests to work_ and that is a Bad Thing (tm). Maybe if I am not left with better solution, I would use this, but I would feel that I am a sinner. Code _must_ be independent of test _implementation details_, if should not _be changed just so tests could work_. Thanks anyway. –  Dec 30 '11 at 12:34
  • For now, I came with the idea of putting all "connectors to outside world" in its own hidden or put-off-the-viewport iframes and communicating with them through API exclusively, so I sort-of put this off one layer - and maybe I will be content not to have the facebook connector login part fully covered if the code itself will be short and comprehensible enough. –  Dec 30 '11 at 12:36
  • As for "Your idea to query window.location.href would not only test your application code, but the browser itself" - on the other hand, it tests the concern ("redirecting") well, even if it is done by means of any library or by plain assigment. The test is then less brittle, I need not to change it whenever I choose to use another code for redirecting. Even if the purist view would argue that such test then gets some acceptance flavour and cannot be spelled as pure unit test... –  Dec 30 '11 at 12:42
0

You could check this How far can you go with JavaScript testing? question.

Selenium is indeed good and well-known, but I personally find it quite old and not so usable. I'd advise using Ghostbuster (see introduction), a headless Webkit running tests written in Javascript or Coffeescript.

Your given test-case would be written as follows:

phantom.test.root = "http://localhost/"     # or whatever your testing env's root

phantom.test.add "Facebook login", ->       # this adds a test
  @get '/yourLoginPage', ->                 # relative to the previous root
    @body.click '#login-btn'
    @assertLocation "http://www.facebook.com/oauth/..."
    @succeed()                              # all tests must succeed

:)

Community
  • 1
  • 1
MattiSG
  • 3,796
  • 1
  • 21
  • 32
  • Since it's a headless webkit, it would be nice if it could work with JS Test Driver, in the role of the browser. That would solve my concerns nicely. –  Nov 21 '11 at 19:06
  • @herby I'm not sure I understand what you mean by “work with JS Test Driver in the role of the browser”. You mean replace the Webkit engine with calls to [JS Test Driver](http://code.google.com/p/js-test-driver/)? – MattiSG Nov 21 '11 at 19:12
  • No, use Ghostbuster as one of the browsers for JS Test Driver. But, it's a lot more complicated - something it should load, something not... This is is fact also testing from outside, not mocking the location and stopping it from navigating inside... –  Nov 21 '11 at 21:11
  • @herby Ghostbuster provides tests parsing and execution. If you simply want a headless Webkit, use [PhantomJS](http://www.phantomjs.org/), on which Ghostbuster is built. However, it [does not seem](http://groups.google.com/group/phantomjs/browse_thread/thread/c627bc1486c488fc) to have bindings with JS Test Driver… Maybe you could try [this](https://bitbucket.org/pagles/jstest/overview)? – MattiSG Nov 21 '11 at 21:20
  • headless (or even headful) whatever that lets me to mock location = and the likes (that is, just letting them configurable, I can do the mocking on my own). Or, of course, to see other solution that are not ideal. Maybe I should rethink the idea of testing my browser code inside a browser and look up for some browser-like but specifically good for testing... (node.js+jsdom+glue code?) –  Nov 21 '11 at 21:31
  • @herby I'm sorry, but I really don't understand :-S How is the answer I gave not a valid one for what you're asking?! And BTW, if you want Node+JSDOM, use [Zombie](http://zombie.labnotes.org/) ;) – MattiSG Nov 21 '11 at 22:13
  • I am looking for possibilties and than maybe changing my testing strategy... for now, I have unit tests for browser code design to run in the browser (QUnit). I hit the wall hard with inability to mock page navigation. Your answer may lead to usable solution, so I thank for all your links, but probably not usable with the QUnit set of tests... the problem is harder than it looks. –  Nov 21 '11 at 22:30
  • @herby IMHO, you should rephrase your question: “I want to unit test (in browser, QUnit or something of the kind)” isn't the same as “I have unit tests for browser code design to run in the browser (QUnit)”! – MattiSG Nov 21 '11 at 22:32
  • Now _I_ did not understand. Anyway, zombie looks promising. It is on github, so I can easily make my own changes and it is very similar to what I searched for. –  Nov 21 '11 at 22:40
  • @herby That's getting complicated ;) I was saying: in the question, you sounded like you were *looking for ways to unit-test in the browser, for example with QUnit*, while in the comments you gave the crucial info that you *already had QUnit tests that you wanted to make work with page navigation*. It is not the same problem! :) – MattiSG Nov 22 '11 at 05:48
  • I see. Yes. Luckily, I hit the wall rather soon so I may port them if they could not be used. –  Nov 22 '11 at 11:07