keyboard-shortcutsnightwatch.js

Unable to use short cut keys (hotKeys)


I am writing e2e tests in NightWatch v2.1.3 using page objects. There are list of items, and an item can be selected either by click or by hotKey of its index.

Example: second element can be selected by click or shift+2.

Following code i have written, taking reference from the docs, but it is not working

browser.perform(function () {
    const actions = this.actions({async: true});
    console.log('performing hotKeys');
    actions
        .click('@option1')
        .keyDown(Keys.SHIFT)
        .keyDown(Keys.NUMPAD2)
        .keyUp(Keys.NUMPAD2)
        .keyUp(Keys.SHIFT);
});

Console is happening but the click and keyUp, keyDown is not working, when kept inside the .perform method.

What need to be fixed here?


Solution

    1. return keyword is important. (silly mistake here)
    2. Use 'a', 'b', '1', '2' for normal keys (single quotes are important, even for numbers)
    3. click is not working, inside actions api. Better use the api click instead of userActions click. No idea why this click is added under new user-actions. The documentations does not have enough examples, one need to find out through hit and trial method.

    Example:

    browser
      .pause(3000)
      .perform(function () {
        const actions = this.actions({ async: true });
    
        return actions
          .keyDown(Keys.SHIFT)
          .keyDown('1')
          .keyUp('1')
          .keyUp(Keys.SHIFT);
      })
      .pause(2000);
    

    (This depends on the way feature is developed, if app need both 1 and 0 to be pressed or not)

    browser
      .pause(3000)
      .perform(function () {
        const actions = this.actions({ async: true });
    
        return actions
          .keyDown(Keys.SHIFT)
          .keyDown('1')
          .keyUp('1')
          .keyDown('0')
          .keyUp('0')
          .keyUp(Keys.SHIFT);
      })
      .pause(2000);
    
    // keyUp('10') wont work, it is not a valid key in your keyboard
    

    (This depends on the way feature is developed, if app need both 1 and 0 to be pressed or not)

    browser
      .pause(3000)
      .perform(function () {
        const actions = this.actions({ async: true });
    
        return actions
          .keyDown('a')
          .keyUp('a')
      })
      .pause(2000);