nightwatch testing: .sendKeys and .keys not sending key clicks

14,808

Solution 1

The test you have shared should not pass. You should be seeing an error:

 ✖ ReferenceError: ShowText is not defined

Unless you have defined ShowText somewhere in your code ...?
What are you expecting ShowText(browser) to do?

Try:

.sendKeys('div[id=columns]', browser.Keys.DOWN_ARROW)

or, if you just want to send the keypress in the general window context:

.keys(browser.Keys.DOWN_ARROW)

Working Example: .sendKeys('div[id=container]', browser.Keys.DOWN_ARROW)

Note: sendKeys is an alias for setValue see: /lib/api/element-commands.js#L385 so it accepts the same params.

Solution 2

You can try following way to press any key in nightwatch.js, i am pressing T and it is working superb!!

client.keys("t", function(done) {
    client.pause(5000);
    client.expect.element('#carousel_container').to.have.css('display').which.equals('block');
});

we are using above way because nightwatch.js Keys does not have any alphabet command in it's array, i have consoled and i haven't found any alphabet to press it.

Keys:
{ NULL: '',
  CANCEL: '',
  HELP: '',
  BACK_SPACE: '',
  TAB: '',
  CLEAR: '',
  RETURN: '',
  ENTER: '',
  SHIFT: '',
  CONTROL: '',
  ALT: '',
  PAUSE: '',
  ESCAPE: '',
  SPACE: '',
  PAGEUP: '',
  PAGEDOWN: '',
  END: '',
  HOME: '',
  LEFT_ARROW: '',
  UP_ARROW: '',
  RIGHT_ARROW: '',
  DOWN_ARROW: '',
  ARROW_LEFT: '',
  ARROW_UP: '',
  ARROW_RIGHT: '',
  ARROW_DOWN: '',
  INSERT: '',
  DELETE: '',
  SEMICOLON: '',
  EQUALS: '',
  NUMPAD0: '',
  NUMPAD1: '',
  NUMPAD2: '',
  NUMPAD3: '',
  NUMPAD4: '',
  NUMPAD5: '',
  NUMPAD6: '',
  NUMPAD7: '',
  NUMPAD8: '',
  NUMPAD9: '',
  MULTIPLY: '',
  ADD: '',
  SEPARATOR: '',
  SUBTRACT: '',
  DECIMAL: '',
  DIVIDE: '',
  F1: '',
  F2: '',
  F3: '',
  F4: '',
  F5: '',
  F6: '',
  F7: '',
  F8: '',
  F9: '',
  F10: '',
  F11: '',
  F12: '',
  COMMAND: '',
  META: '' 
},

You can press any key in above array easily like "client.keys(client.Keys.ENTER);".

Share:
14,808
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using node.js with nightwatch. It works great, my only issue is that my current application needs to be tested by 'pressing' all the arrow keys, and making sure the class name on the elements changes. I CANNOT get the sendKeys function to actually send a key press, and this test passes, when it should not :P. Any ideas?

    module.exports = {
      'chosenTest.html' : function (browser) {
          browser
          .url(path)
          .waitForelementVisible('div[id=columns]', 1000)
          .assert.containsText('div[class="choosable chosen"], 'Test 1-0')
          .sendKeys('div[id=columns]', ShowText(browser), browser.Keys.DOWN_ARROW)
          .assert.containsText('div[class="choosable chosen"]', 'Test 1-0')
          .pause(1000)
          .end()
      }
    }