在jasmine-phantomjs test中调用html元素上的click()函数 [英] calling click() function on html element in jasmine-phantomjs test

查看:195
本文介绍了在jasmine-phantomjs test中调用html元素上的click()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Karma和PhantomJS运行我的测试:

I run my tests with Karma and PhantomJS:


INFO [karma]:Karma v0.12.32服务器从 http:// localhost:9876 /
INFO [launcher]:启动浏览器PhantomJS INFO [PhantomJS 1.9.8
(Windows 7)]:连接套接字kVOvdbbgtIeUQuV8AAAA机智

INFO [karma]: Karma v0.12.32 server started at http://localhost:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 1.9.8 (Windows 7)]: Connected on socket kVOvdbbgtIeUQuV8AAAA wit

在该测试中,我想点击段落(p)获取我的事件反馈:

In the that test I want to click on the paragraph (p) to get my event-feedback:

// given 
var element = appendToBody("<p id='hello-p'>Hello Hello World!</p>");

element.addEventListener('click', function(event) {
    console.log("1");
});

// when
var p = document.getElementById("hello-p");

console.log(p);
// LOG: <p id="hello-p">Hello Hello World!</p>
console.log(p.id);
// LOG: 'hello-p'

p.click(); // calling click

我最终收到此消息:


TypeError:'undefined'不是函数(评估'p.click()')

TypeError: 'undefined' is not a function (evaluating 'p.click()')

问:为什么p可能未定义?

Q: why p could be undefined?

推荐答案

好的。
因为基于webkit的PhantomJS,我必须创建/发送这样的事件:

Ok. Bacause PhantomJS based on webkit, I have to create/send event like this:

  var cle = document.createEvent("MouseEvent");
  cle.initEvent("click", true, true);
  var elem = document.getElementById('hello-p');
  elem.dispatchEvent(cle);

这篇关于在jasmine-phantomjs test中调用html元素上的click()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆