Sending Ctrl+A combination to an element

2019-01-25 02:19发布

问题:

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.

回答1:

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();


回答2:

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')) });


回答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);