calling click() function on html element in jasmine-phantomjs test [duplicate]

二次信任 提交于 2019-12-14 02:46:05

问题


I run my tests with Karma and PhantomJS:

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

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

I end up having this message:

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

Q: why p could be undefined?


回答1:


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);


来源:https://stackoverflow.com/questions/29806153/calling-click-function-on-html-element-in-jasmine-phantomjs-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!