javascript 将 Ctrl+A 组合发送到元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25300034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Ctrl+A combination to an element
提问by alecxe
I'm using protractorfor angular end-to-end aka e2e
testing.
我正在使用量角器进行角度端到端又名e2e
测试。
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
?
如何发送组合键,例如Ctrl+A
?
I've searched through protractor source codeat github, but haven't found a relevant example.
我在 github 上搜索了量角器源代码,但没有找到相关的例子。
回答by Leo Gallucci
It's perfectly possible in Linux and Windows but not in OSX
在 Linux 和 Windows 中完全有可能,但在 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();
回答by Droogans
If you use protractor-hotkeys, you can use simple hotkey strings (like those from angular-hotkeys) to trigger them in protractor tests.
如果您使用protractor-hotkeys,您可以使用简单的热键字符串(如来自 angular-hotkeys 的那些)在量角器测试中触发它们。
So, this would become:
所以,这将变成:
var hotkeys = require('protractor-hotkeys');
hotkeys.trigger('ctrl+a', { targetElement: element(by.model('myModel')) });
回答by Arun Viswanathan
Seems like an old post. But Just sharing a solution worked for me to clear content of Tinymce editor using protractor in MAC.
好像是个老帖子。但只是分享一个解决方案对我有用,可以在 MAC 中使用量角器清除 Tinymce 编辑器的内容。
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);
回答by Sergey Pleshakov
FOR MAC USERS - 2019
MAC 用户 - 2019
The following code selects last word from an input
以下代码从输入中选择最后一个单词
await $elem.sendKeys(protractor.Key.ALT, protractor.Key.SHIFT, protractor.Key.ARROW_LEFT);