31

How are people writing angular e2e tests that involve triggering a sequence of UI events? The async nature of scenario seems to make it difficult.

details: I'm writing tests for an app that has a lot of key handling to help speed editing of a specialised form. I've pulled together a keyboard extension to the scenario dsl (see below) but only the first key event of a test has any effect. i.e.

keyboard().keydown(null, 'keydown', 40, false, true); // ctrl-down
expect(element('*:focus').text()).toEqual('00:04');
keyboard().keydown(null, 'keydown', 40, false, true); // ctrl-down
expect(element('*:focus').text()).toEqual('');  // but equals 00:04

The second keydown doesn't do anything, because it doesn't find a *:focus to route the key to (although there is one on the screen). Confusing.

angular.scenario.dsl('keyboard', function() { 
    var chain  = {}; 
    chain.keydown = function(selector, keyEvent, keyCode, shift, ctrl) { 
        return this.addFutureAction("keyEvent", function($window, $document, done) { 
            var jQuery = $window.$; 
            var e = jQuery.Event(keyEvent);
            e.keyCode = keyCode; // # Some key code value
            e.altKey = false;
            e.ctrlKey = ctrl;
            e.shiftKey = shift;
            if (selector == null) selector = '*:focus';
            var j = jQuery(selector);
            if (j == null) j = jQuery('body');
            j.trigger(e);
            done(); 
        }); 
    }; 
    return function() { 
        return chain; 
    }; 
}); 
OptimizedQuery
  • 1,262
  • 11
  • 21
hemul
  • 311
  • 2
  • 3
  • So the problem is probably on your code, not your test. Would you please post what your 'keydown' event is doing on your app? I copied your scenario test and, added a `browser.navigateTo`, added a single `div` on my HTML (`tabindex="0"`), and added a simple event that toggle the content between '' and '00:40' on keydown. Everything worked fine. – Caio Cunha Apr 12 '13 at 11:44

1 Answers1

1

User seem to have abandoned this question, but as I said, this is probably an issue with his keydown event handler.

Made a Plnker here with exactly the same code and everything work as expected.

Caio Cunha
  • 23,326
  • 6
  • 78
  • 74