2014-02-18 61 views
6

我正在嘗試使用CasperJS編寫一個測試,其中在輸入中按Enter鍵是頁面觸發器,以便以其他方式輸入到輸入中的文本執行某些操作。CasperJS:吞下特殊鍵,如Enter?

的CasperJS測試的縮寫/簡體版本:

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 test this-test.js

我驗證過sendKeys越來越文成輸入,但文本從未出現在#stage元素中。一個「香草」PhantomJS按鍵的實現工作正常,其中webpage.sendEvent('keypress', '\uE007')導致頁面事件處理程序觸發。看看casper.sendKeys的來源,我看到它正在委託Casper實例的PhantomJS實例(即line 1613 in the current version of casper.js)上的sendEvent

任何想法?任何人都可以通過CasperJS測試獲得這些密鑰?

回答

12

您可能希望將{keepFocus:true}添加到第一個sendKeys調用。如果您看到源代碼,但未添加keepFocus,則會模糊文本區域,這意味着您的第二個sendKeys調用不會接受按鍵。

這似乎工作。

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

對於casper.page.event.key可用的特殊鍵的列表,請參閱:https://github.com/ariya/phantomjs/commit/cab2635e66d74b7e665c44400b8b20a8f225153a – pxwise