28

I'm using protractor for angular end-to-end aka e2e testing.

In order to send keys to an element, I use:

element(by.model('myModel')).sendKeys('Test');

How can I send a combination of keys, like Ctrl+A?


I've searched through protractor source code at github, but haven't found a relevant example.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I'm not positive, but I think it will look something like `sendKeys(protractor.key.CONTROL + 'a')` – Richard Aug 14 '14 at 04:51
  • You might found anything helpful in the webdriverjs documentation. – runTarm Aug 14 '14 at 05:24
  • For those interested in handling `CONTROL` vs `COMMAND`: [Using cross-platform keyboard shortcuts in end-to-end testing](http://stackoverflow.com/questions/33263091/using-cross-platform-keyboard-shortcuts-in-end-to-end-testing). – alecxe Dec 19 '15 at 04:54

3 Answers3

33

It's perfectly possible in Linux and Windows but not in OSX

var elm = element(by.model('myModel'));
elm.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a"));

There is also a non-element variant:

browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('a').perform();
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
  • 1
    Yeah, I was experimenting with `protractor.Key.COMMAND` (on mac) after posting a question - could not make it work. Thanks for the answer and for the link. – alecxe Aug 14 '14 at 21:01
  • 2
    Be careful with the non-element variant, as it will leave the CONTROL key pressed for future tests. So keyUp CONTROL before perform. – Chexpir Mar 26 '15 at 12:02
9

If you use protractor-hotkeys, you can use simple hotkey strings (like those from angular-hotkeys) to trigger them in protractor tests.

So, this would become:

var hotkeys = require('protractor-hotkeys');
hotkeys.trigger('ctrl+a', { targetElement: element(by.model('myModel')) });
yurisich
  • 6,991
  • 7
  • 42
  • 63
  • Oh, wow, that's an awesome idea! Thank you for making it happen - will certainly add it to the toolkit. – alecxe Dec 11 '15 at 20:06
  • Would `protractor-hotkeys` implicitly handle the ctrl vs command on windows vs mac? (referring to [this](http://stackoverflow.com/questions/33263091/using-cross-platform-keyboard-shortcuts-in-end-to-end-testing)) – alecxe Dec 11 '15 at 20:07
  • 1
    I decided to not tackle that issue for the time being -- it's was too difficult to test in my current situation. I tested only the [most basic stuff](https://www.npmjs.com/package/protractor-hotkeys#testing) that I am certain will work cross platform. If you could [give me some feedback on how it works](https://github.com/Droogans/protractor-hotkeys/issues), I'd appreciate it. – yurisich Dec 11 '15 at 21:15
  • 1
    Cool! please let us know if you decide to tacke Mac vs non-osx issues, even a warn.log would help so at least we know something will not work the same way cross platform ;) – Leo Gallucci Dec 19 '15 at 15:51
3

Seems like an old post. But Just sharing a solution worked for me to clear content of Tinymce editor using protractor in MAC.

var body_editor = element(by.id('tinymce'));/*id of body inside iframe*/
body_editor.click().sendKeys(protractor.Key.chord(protractor.Key.COMMAND, "a"));
body_editor.click().sendKeys(protractor.Key.BACK_SPACE);