- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 jsdom 加载本地 HTML 文件,这是代码
var config = {
file: "filename",
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.URL)
console.log(window.document.children)
}
}
jsdom.env(config)
window.document.URL
显示正确的 url,但 window.document.innerHTML
没有任何数据。我什至尝试过
var config = {
text: *raw-html*,
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.children)
}
}
但我得到了相同的结果,有什么帮助吗?
我发现了
var rawhtml = fs.readFileSync("filename.html","utf8")
jsdom.env(
rawhtml,
["http://code.jquery.com/jquery.js"],
function (err, window) {
console.log(window.$("html").html())
}
);
按预期工作(它记录了 html
标签内的所有内容)
然后我发现
config = {
file: "filename.html",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (err, window) {
var $ = window.$;
console.log($("html").html());
}
}
jsdom.env(config);
也可以,但同样不能正确创建document
对象
jsdom 不会以与浏览器控制台相同的方式显示 document.children
的输出,但是,您期望的所有功能都在那里。最终的工作 document
对象在下面
var func = function(document){
console.log(document.body.innerHTML)
document.body.innerHTML = "i changed the body"
}
function getdoc(file, func){
var document;
jsdom.env(
file,
(err, window) => {
document = window.document
func(document)
}
);
}
getdoc("index.html",code)
我使用 jsdom.env()
获取 document
,然后将 document
传递给 func
where所有有趣的 JavaScript 魔法都是免费发生的。请记住,对 文档
所做的任何更改都需要手动保存。
最佳答案
没有 document.innerHTML
属性。
检查文档中的“innerHTML”
; - 你会得到 false
。
关于javascript - Jsdom 不返回文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737490/
我对如何使用 jsdom 仍然一头雾水。 jsdom.env() 和 jsdom.jsdom 有什么区别? 最佳答案 The readme file解释说 jsdom.env is built for
我有一个使用 ES2015 模块语法的代码库,即。 从“bar”导入 foo。它也有这样的代码: // setup.js var foo = window.foo || ''; 我想使用 jsdom
有没有一种方法可以将 HTML 传递给 jest 内置 JSDOM 中的构造函数,例如 const dom = new JSDOM('Hello world'); - in orginal JSDOM
我正在使用 mocha 和 jsdom 对 JavaScript 库进行单元测试。库中的一个模块具有不同的行为,具体取决于文档是否准备好。为了测试这种行为,我需要模拟一个尚未完全准备好的文档(即它的
我想开始使用“jsdom”包jsdom:https://github.com/tmpvar/jsdom 我在文档中看到以下代码片段: var jsdom = require("jsdom"); jsd
Background 因此,我一直在遵循教程来了解 JS 测试框架,并了解到要测试 Web UI,最好使用 JSDOM 库,因为它不需要浏览器并带来 DOM,然后可以以 headless 方式执行。
我正在尝试使用 jsdom 来分析一些 html 内容。我看到的示例使用基于 html 内容的 jsdom.jsdom 文档的 .createWindow() 方法。 但是当我尝试按照这些示例进行操作
我正在尝试使用 Ember、Brunch 和 JSDom 进行 headless 测试。我正在使用 ember-brunch-skeleton 生成应用程序。 https://github.com/f
我已经安装了webppl-agents库(以及 webppl 和 webppl-dp )并尝试运行命令行测试,但我遇到了一些麻烦。 npm 文件模块 (1) 中的 jsdom 似乎存在依赖性问题(2)
在 express.js 中使用 jsdom.jsdom() 我创建了一个带有一些“基本”布局标记的文档,并将一些客户端库(例如 jQuery)附加到其窗口。 只需进行一次此设置就好了。 问题是窗口文
我创建了一个非常简单的页面,仅显示一条消息,因为我正在尝试测试 JSDOM 以使用 document。但是,我收到以下错误。 首先,我在网上看到了无数的例子,除了Stack Overflow上发布的问
这个问题已经有答案了: What do querySelectorAll and getElementsBy* methods return? (12 个回答) 已关闭 4 年前。 我正在尝试使用 T
根据JSDom README ,您可以传递一个 Javascript 字符串数组来执行,但似乎此功能对我不起作用。 这是我的 Node 应用程序: var fs = require('fs'); va
我正在使用 jsdom解析html const data = dom.window.document.querySelector('.row h1').textContent 在那种特殊情况下,我的数
所以我尝试用 jsdom 设置 mocha 测试,经过多次调试尝试,我将问题缩小到 jsdom 执行绝对 URL 脚本(例如 http://code.jquery.com/jquery- 2.1.3.
下面是使用 script 参数的 jsdom 代码的最小示例。尽管我尝试过寻找一种引用外部 JS 的方法,但我还是不断收到此消息 ReferenceError: exVar is not define
我似乎无法让 jsdom 执行外部脚本,而且我似乎找不到任何具体示例。当我调用我的测试函数时,我没有收到任何错误,但什么也没有发生。 这是我的代码: var window = jsdom.js
交叉发布自 https://github.com/tmpvar/jsdom/issues#issue/127 我将在第二天左右发布一个最小的测试用例 - 但我想看看是否有其他人遇到同样的问题 - 或者
我正在使用 jsdom 通过我的 Node.js 应用程序加载网页。有时,我没有获得完整的 DOM,因为一些网页使用脚本在 onload 事件被触发后动态加载它们的内容。 jsdom 默认情况下停用这
我正在尝试使用 jsdom 加载本地 HTML 文件,这是代码 var config = { file: "filename", scripts: ["node_modules
我是一名优秀的程序员,十分优秀!