简体   繁体   English

使用zombie.js同步外部JS加载

[英]Synchronous external JS loading with zombie.js

The problem: 问题:

I'm using zombie.js to test my client-side javascript, but I am running into a problem. 我正在使用zombie.js来测试我的客户端javascript,但我遇到了一个问题。 Zombie.js does not provide synchronous <script> tag execution, and, in fact seems to not execute external JS files at all. Zombie.js不提供同步<script>标签执行,实际上似乎根本不执行外部JS文件。 A basic test confirms this: 基本测试证实了这一点:

<script type="text/javascript" src="test1.js"></script>
<script type="text/javascript" src="test2.js"></script>
<script type="text/javascript" src="test3.js"></script>

<script type="text/javascript">
   console.log("Inline javascript.");
</script>

Each test#.js contains a single line: console.log("TEST#.JS"); 每个测试#.js都包含一行: console.log("TEST#.JS");

When I render this in a regular browser, the console displays the expected: 当我在常规浏览器中呈现它时,控制台显示预期的:

TEST1.JS
TEST2.JS
TEST3.JS
Inline javascript.

But when I run it with zombie.js, I only see a single line Inline javascript. 但是当我用zombie.js运行它时,我只看到一行Inline javascript.

Here's what I have tried to get around the issue: 以下是我试图解决的问题:

  1. using document.createElement to dynamically append a script tag to the document 使用document.createElement动态地将脚本标记附加到文档
  2. using document.write to add the script block into the html 使用document.write将脚本块添加到html中
  3. using a setTimeout on console.log("Inline javascript") in combination with 1 and 2 to give the test scripts some time to load. 使用console.log("Inline javascript")上的setTimeout console.log("Inline javascript")与1和2结合使用,为测试脚本提供一些加载时间。

Is there any way to resolve this issue, besides placing the JS code from all my external JS files into a huge <script> block? 除了将所有外部JS文件中的JS代码放入一个巨大的<script>块之外,还有什么方法可以解决这个问题吗?

Are you sure the browser object has the "runScripts" option set to true? 您确定浏览器对象的“runScripts”选项设置为true吗? If not you can use the following syntax: 如果不是,您可以使用以下语法:

browser.visit('... your page url ...', { runScripts: true }, function (e, b) { 
    console.log('executing callback'); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM