如何在守夜人中发送按键
How to send keypress in nightwatch
我知道如何用 nightwatch 发送点击事件:
browser.click('#my-control');
但是我一直找不到发送按键事件的方法。这在守夜人中是怎么做到的?
我认为 Selenium 协议中的 keys
方法将是您需要的方法:
您可以尝试以下方式按nightwatch.js中的任意键;我正在按 T,效果非常好!
client.keys("t", function(done) {
client.pause(5000);
client.expect.element('#carousel_container').to.have.css('display').which.equals('block');
});
我们用上面的方法是因为nightwatch.jsKeys的数组里没有字母命令,我安慰了一下,没找到可以按的字母。
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: ''
},
您可以像 "client.keys(client.Keys.ENTER);" 一样轻松地按上面数组中的任意键。
您应该在 sendKeys 函数中传递控制器。
像这样。
elements: {
textBoxSearch: {
selector: '.nav-search-input'
},
.sendKeys('@textBoxSearch',client.Keys.ENTER);
如果你想发送一个简单的击键,你可以直接通过下面的方式发送
browser.keys('j')
这将模拟按下 J 键
但根据 http://nightwatchjs.org/api#setValue
这也应该做的工作
demoTest = function (browser) {
browser.setValue('input[type=text]', ['this does the job', browser.Keys.ENTER]);
};
因此,如果您需要按一个简单字符,请将其作为字符串发送,否则请使用 nightwatch 包 key.json 中的特殊字符之一
{
"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"
}
一个简单的方法是使用 .keys()
方法名称,然后传递您要按下的键名。
例如:下面的命令将按向下箭头键。
.keys(browser.Keys.ARROW_DOWN)
我知道如何用 nightwatch 发送点击事件:
browser.click('#my-control');
但是我一直找不到发送按键事件的方法。这在守夜人中是怎么做到的?
我认为 Selenium 协议中的 keys
方法将是您需要的方法:
您可以尝试以下方式按nightwatch.js中的任意键;我正在按 T,效果非常好!
client.keys("t", function(done) {
client.pause(5000);
client.expect.element('#carousel_container').to.have.css('display').which.equals('block');
});
我们用上面的方法是因为nightwatch.jsKeys的数组里没有字母命令,我安慰了一下,没找到可以按的字母。
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: ''
},
您可以像 "client.keys(client.Keys.ENTER);" 一样轻松地按上面数组中的任意键。
您应该在 sendKeys 函数中传递控制器。
像这样。
elements: {
textBoxSearch: {
selector: '.nav-search-input'
},
.sendKeys('@textBoxSearch',client.Keys.ENTER);
如果你想发送一个简单的击键,你可以直接通过下面的方式发送
browser.keys('j')
这将模拟按下 J 键
但根据 http://nightwatchjs.org/api#setValue 这也应该做的工作
demoTest = function (browser) {
browser.setValue('input[type=text]', ['this does the job', browser.Keys.ENTER]);
};
因此,如果您需要按一个简单字符,请将其作为字符串发送,否则请使用 nightwatch 包 key.json 中的特殊字符之一
{
"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"
}
一个简单的方法是使用 .keys()
方法名称,然后传递您要按下的键名。
例如:下面的命令将按向下箭头键。
.keys(browser.Keys.ARROW_DOWN)