Jasmine - Unble to get DOM elements

蹲街弑〆低调 提交于 2019-12-08 12:50:46

问题


I am testing an angularjs directive that manipulates the DOM.

I am trying to get the element in my Jasmine spec, so that I can test the functionality of the directive. However, when I use document.getElementsByClassName or TagName or ID, it doesn't return anything. Does anyone have ideas about this?

html = document.getElementsByClassName('analog');
console.dir(html);

回答1:


If you create a test in headless browser/chrome etc., you could append a dummy object, for example JQuery node, then remove that node in afterEach.

E.g.

beforeEach(() => {
    var mockHtml = $('<div class="form-group" style="position: absolute;left: -10000px;"><input class="testInput" id="some_input"></div>');
    $('body').append(mockHtml);
});

afterEach(() => {
    $('.form-group').remove();
});


来源:https://stackoverflow.com/questions/37011652/jasmine-unble-to-get-dom-elements

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