3

我有一个页面,我在页面的标题中引用了一堆带有常规脚本标记的脚本:jQuery 等,以及我编译的名为 index.js 的脚本。后者很大,大约 120 万(不要问为什么)。

我的 index.html 看起来像这样:

<html>
<head>
<title>Cool</title>
<script src='jQuery.js'></script>
<!-- more scripts -->
<script src='index.js'></script>
<!-- more stuff -->

问题是如果我使用 PhantomJS 引擎,我的脚本不会在任何平台上执行。如果我使用 SlimerJS/XUL,那么它会在 Windows 上执行,但不会在 OSX 上执行。我用常规的waitFor验证它,评估一个全局变量的值,没什么特别的:

casper.test.begin('index.js executes', 1, function (test) {
    casper.start(host);

    casper.waitFor(
        function () {
            return casper.evaluate(function () {
                return !!window.ENV;
            });
        },
        function () {
            test.pass('index.js run');
        },
        function () {
            test.fail('index.js did not run');
        });

    casper.run(function () {
        test.done();
    });
});

另一方面,jQuery 在任一环境中都被初始化,我可以在评估上下文中很好地运行 jQuery 代码,所以这将通过:

casper.test.begin('jQuery works', 1, function suite(test) {
    casper.start(host);

    casper.waitFor(
        function () {
            return casper.evaluate(function () {
                try {
                    return !!$('html').length;
                }
                catch (e) {
                    return false;
                }
            });
        },
        function () {
            test.pass('jQuery works');
        },
        function () {
            test.fail('jQuery did not init');
        });

    casper.run(function () {
        test.done();
    });
});

如果我注销页面事件,我可以看到 index.js 加载的 HTTP 结果为 200,它只是没有被执行。

以前有人经历过吗?PhantomJS 或 SlimeJS 是否对脚本大小/复杂性有一些特定于平台的限制?

4

0 回答 0