我正在使用 karma测试我用 JavaScript 完成的客户端代码。所以我有一个要加载的 html 文件,然后我想在我的测试脚本中从该 html 页面获取所有元素。
下面是 karma.conf.js
页面代码,我通过它加载我的所有页面。
// list of files / patterns to load in the browser
files = [
'node_modules/karma-jstd-adapter/jstd-adapter.js',
'testSetup.js',
'config.js',
'jquery.js',
'test.htm',
'startup.test.js'
];
test.htm 中有 ID 为 test
的“DIV”。我正在向该 DIV
动态添加内容。所以我需要验证,我想获取元素内容,但我无法做到这一点。
最佳答案
您可以使用 html2js preprocessor ,它基本上将 HTML 文件转换为 JavaScript 字符串并包含这些文件。
然后,您可以在测试中访问这些字符串:
var elm = $(window.__html__['test.htm']);
关于node.js - 使用 Karma 加载 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18317693/