JS测试:从CasperJS和PhanthomJS触发jQuery keypress事件 [英] JS Testing: Trigger jQuery keypress event from CasperJS and PhanthomJS
问题描述
我的网页有一个输入键按下事件的监听器。我试图运行下面的casperjs代码来触发此事件,但没有成功。
my webpage has a listener to the enter key press event. I am trying to run the casperjs code below to trigger this event, but without success.
虽然没有提示错误,但(evaluate)函数返回true并且代码我的Chrome控制台工作正常,应该向服务器发送请求的功能结果永远不会发生
Although no error is prompted out, the (evaluate) function returns true and the code works fine from my chrome console, the function result, that should be sending a request to the server is never happening
casper.then(function(){
var result = this.evaluate(function(term){
var search_form_id = "#search-form";
$(search_form_id).val(term);
jQuery(search_form_id).trigger(jQuery.Event('keypress', {which: 13, keyCode: 13}));
return true;
}, 'Techcrunch');
console.log(result);
});
这是关于PhantomJS和jQuery事件的任何问题吗?
Is that any issue regarding PhantomJS and jQuery events?
推荐答案
看起来,你不能使用jQuery触发 keypress
事件。有一个解决方法使用底层 casper.page.sendEvent
功能。虽然有必要关注元素,但会触发 keypress
。在以下示例中,我使用 sendKeys
函数的 keepFocus
选项。
It looks like, you can't trigger the keypress
event using jQuery. There is a workaround using the underlying casper.page.sendEvent
function. Though it is necessary to focus on the element, where the keypress
will be triggered. In the following example I use the keepFocus
option of the sendKeys
function.
var casper = require('casper').create();
casper.start("https://duckduckgo.com/");
casper.then(function() {
this.sendKeys("#search_form_homepage input[name=q]", "casperjs", { keepFocus: true });
this.capture("typed.png");
this.page.sendEvent("keypress", this.page.event.key.Enter);
});
casper.waitForSelector("#links_wrapper");
casper.then(function() {
this.capture("searched.png");
});
casper.run();
这篇关于JS测试:从CasperJS和PhanthomJS触发jQuery keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!