问题描述
我正在尝试使用CasperJS编写测试,以用于在输入中按Enter键触发页面对文本进行处理的情况,否则将文本输入到输入中。
I'm trying to write a test using CasperJS for a scenario where pressing Enter in an input is the trigger for the page to do something with the text otherwise typed into the input.
CasperJS测试的缩写/简化版本:
An abbreviated/simplified version of the CasperJS test:
casper.start('http://localhost:3000/input-demo', function() {
this.sendKeys('#demo-input', 'demo text');
this.sendKeys('#demo-input', '\uE007');
this.test.assertEquals(this.getHTML('#stage'), 'input demo');
});
casper.run();
(在我们以 casperjs测试this-test.js的方式运行
)
我已验证 sendKeys
正在将文本输入到输入中,但是文本永远不会显示在 #stage
元素中。按键的原始 PhantomJS实现效果很好,其中 webpage.sendEvent('keypress','\uE007')
会触发页面事件处理程序。查看 casper.sendKeys
的来源,我发现它委托给Casper实例的PhantomJS实例上的 sendEvent
(即,当前版本的casper.js中的)。
I've verified that sendKeys
is getting the text into the input, but that text never shows up in the #stage
element. A "vanilla" PhantomJS implementation of the keypress works fine, where webpage.sendEvent('keypress', '\uE007')
causes the on-page event handler to fire. Looking at the source of casper.sendKeys
, I see that it is delegating to the sendEvent
on the Casper instance's PhantomJS instance (i.e., line 1613 in the current version of casper.js).
有什么想法吗?有人得到了这些密钥才能在CasperJS测试中工作吗?
Any ideas? Anyone gotten these keys to work in a CasperJS test?
推荐答案
您可能想在第一个sendKeys中添加{keepFocus:true}呼叫。如果您看到源代码,但未添加keepFocus,则会模糊文本区域,这意味着您的第二个sendKeys调用将不接受按键。
You might want to add {keepFocus: true} to the first sendKeys call. If you see the source code, without adding a keepFocus, it is blurring the text area and that means your second sendKeys call will not accept the keypress.
这似乎可行
casper.start('http://localhost:3000/input-demo', function() {
this.sendKeys('#demo-input', 'demo text', {keepFocus: true});
this.sendKeys('#demo-input', casper.page.event.key.Enter , {keepFocus: true});
this.test.assertEquals(this.getHTML('#stage'), 'input demo');
});
casper.run();
这篇关于CasperJS:吞下Enter之类的特殊键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!