8

I know how to send click events with nightwatch:

browser.click('#my-control');

But I have been unable to find a way to send key events. How is this done in nightwatch?

nelsonic
  • 31,111
  • 21
  • 89
  • 120
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 3
    Which keypress do you want to _send_? e.g. if you want to send the `Enter` key you would write `.keys(browser.Keys.ENTER)` – nelsonic Jun 21 '16 at 17:30
  • Hi Nelsoni, If i want to Press "T" OR "S" Or "M" than will it work, i tried same but from "browser.keys(browser.Keys.T)" is not working. – Ashish-BeJovial Jul 26 '16 at 11:49

5 Answers5

13

You can try the 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 the above way because nightwatch.js Keys does not have any alphabet command in its 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);".

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Ashish-BeJovial
  • 1,829
  • 3
  • 38
  • 62
  • 2
    what about keeping a key down? any hint? – AndreaBogazzi Sep 30 '16 at 11:46
  • you can use above scenario within for loop with with some wait., i hope it will work for you. – Ashish-BeJovial May 12 '17 at 10:57
  • 1
    It a shame how vaguely nightwatch docs mentions this Keys map http://nightwatchjs.org/api#setValue – lasec0203 May 24 '18 at 19:09
  • This does not work. If you do this with > client.Keys.ENTER < you'll get "Error: function uses multiple asynchronous interfaces: callback and promise to use the callback interface: do not return a promise to use the promise interface: remove the last argument to the function" – Alichino Sep 17 '20 at 09:00
6

if you entend to send a simple key stroke, you can do it directly through the following

browser.keys('j')

this will simulate the pressing on the J key

but according to http://nightwatchjs.org/api#setValue this should also do the job

demoTest = function (browser) {
  browser.setValue('input[type=text]', ['this does the job', browser.Keys.ENTER]);
};

So if you need a press a simple character, send it as string, otherwise use one of the special character in the key.json in the nightwatch package

    {
  "NULL"        : "\uE000",
  "CANCEL"      : "\uE001",
  "HELP"        : "\uE002",
  "BACK_SPACE"  : "\uE003",
  "TAB"         : "\uE004",
  "CLEAR"       : "\uE005",
  "RETURN"      : "\uE006",
  "ENTER"       : "\uE007",
  "SHIFT"       : "\uE008",
  "CONTROL"     : "\uE009",
  "ALT"         : "\uE00A",
  "PAUSE"       : "\uE00B",
  "ESCAPE"      : "\uE00C",
  "SPACE"       : "\uE00D",
  "PAGEUP"      : "\uE00E",
  "PAGEDOWN"    : "\uE00F",
  "END"         : "\uE010",
  "HOME"        : "\uE011",
  "LEFT_ARROW"  : "\uE012",
  "UP_ARROW"    : "\uE013",
  "RIGHT_ARROW" : "\uE014",
  "DOWN_ARROW"  : "\uE015",
  "ARROW_LEFT"  : "\uE012",
  "ARROW_UP"    : "\uE013",
  "ARROW_RIGHT" : "\uE014",
  "ARROW_DOWN"  : "\uE015",
  "INSERT"      : "\uE016",
  "DELETE"      : "\uE017",
  "SEMICOLON"   : "\uE018",
  "EQUALS"      : "\uE019",
  "NUMPAD0"     : "\uE01A",
  "NUMPAD1"     : "\uE01B",
  "NUMPAD2"     : "\uE01C",
  "NUMPAD3"     : "\uE01D",
  "NUMPAD4"     : "\uE01E",
  "NUMPAD5"     : "\uE01F",
  "NUMPAD6"     : "\uE020",
  "NUMPAD7"     : "\uE021",
  "NUMPAD8"     : "\uE022",
  "NUMPAD9"     : "\uE023",
  "MULTIPLY"    : "\uE024",
  "ADD"         : "\uE025",
  "SEPARATOR"   : "\uE026",
  "SUBTRACT"    : "\uE027",
  "DECIMAL"     : "\uE028",
  "DIVIDE"      : "\uE029",
  "F1"          : "\uE031",
  "F2"          : "\uE032",
  "F3"          : "\uE033",
  "F4"          : "\uE034",
  "F5"          : "\uE035",
  "F6"          : "\uE036",
  "F7"          : "\uE037",
  "F8"          : "\uE038",
  "F9"          : "\uE039",
  "F10"         : "\uE03A",
  "F11"         : "\uE03B",
  "F12"         : "\uE03C",
  "COMMAND"     : "\uE03D",
  "META"        : "\uE03D"
}
OliverAssad
  • 1,022
  • 11
  • 8
3

A simple way of doing this is to use .keys() method name and then to pass the key name you want to press.

For example: The below command will press the Down arrow key. .keys(browser.Keys.ARROW_DOWN)

aayush
  • 85
  • 1
  • 6
1

I think the keys method from Selenium protocol will be the one you need:

http://nightwatchjs.org/api#keys

Qrzy
  • 186
  • 7
0

You should pass the controller in sendKeys funtion.

Like This.

elements: {
        textBoxSearch: {
            selector: '.nav-search-input'
        },

.sendKeys('@textBoxSearch',client.Keys.ENTER);
  • 1
    You post looks like it is one block of code but if so in syntactically wrong and makes little sense. –  Jan 26 '17 at 23:19
  • 1
    @John If you are familiar with pageObjects, and nightwatch you'll get the idea here. The unnecessary parts have been left out for brevity it seems. – zero_cool Nov 28 '17 at 22:14