【问题标题】:Load HTML files with Karma使用 Karma 加载 HTML 文件
【发布时间】:2013-08-19 15:35:56
【问题描述】:

我正在使用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 动态添加内容。所以我需要验证一下,我想获取元素内容,但我无法做到。

【问题讨论】:

    标签: node.js testing karma-runner


    【解决方案1】:

    您可以使用html2js preprocessor,它基本上将HTML文件转换为JavaScript字符串并包含这些文件。

    然后,您可以在测试中访问这些字符串:

    var elm = $(window.__html__['test.htm']);
    

    【讨论】:

    • 我很好奇是否有特定原因使用这种方法而不是 document.open/write/close 如下所示:gist.github.com/ssimeonov/8778619 这会以某种方式“破坏”业力/茉莉花连接在浏览器中?
    猜你喜欢
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多