PhantomJS不会使用类定义编译JavaScript脚本

PhantomJS doesn't compile JavaScript scripts with class definitions

本文关键字:定义 编译 JavaScript 脚本 PhantomJS      更新时间:2023-09-26

好的,我是PhantomJS和整个无头浏览的新手。我正在使用Selenium和PhantomJS来测试我的网站。该网站是使用 .NET MVC 在 C# 上构建的。PhantomJS似乎无法编译任何包含类定义的JS脚本。

Index.cshtml 仅包含对两个脚本的引用:

@section scripts {
    <script src="~/Scripts/foo.js"></script>
    <script src="~/Scripts/bar.js"></script>
}

foo.js的内容是:

function add(a, b) {
    console.log("this works...")
    return a + b;
}
class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

酒吧.js的内容是:

console.log("1 + 2 = ", add(1, 2));

我正在使用F#的REPL玩Selenium。导航到该页面的代码是:

    let driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(@"C:'pathToPhantomJS'");
    let url = "http://myLocalHost:12345/home/index"
    driver.Navigate().GoToUrl(url)
    let logs = driver.Manage().Logs.GetLog(OpenQA.Selenium.LogType.Browser) 
    logs |> Seq.iter (fun l -> printf "%s'n" l.Message)

当我尝试使用PhantomJS点击index.cshtml页面时,出现以下错误:

[错误 - 2016-07-04T21:19:44.274Z] 会话 [f2c46410-422c-11e6-881e-555d71de793e] - page.onError - msg: 引用错误: 找不到变量: 添加

phantomjs://platform/console++.js:263 错误 [错误 - 2016-07-04T21:19:44.275Z]会话 [f2c46410-422c-11e6-881e-555d71de793e] - page.onError - 堆栈:
全局代码 (http://localhost:56135/Scripts/bar.js:1)

错误 phantomjs://platform/console++.js:263

一旦我执行第一行(导航),就会出现错误。但是一旦我从 foo.js 中删除类定义(多边形),一切都变得笨拙:

这行得通...

1 + 2 = 3

这在Chrome/Firefox中没有问题。我到处搜索过,我唯一能找到的接近这个问题的是 page.evaluate() 的整个"监禁执行"/"沙盒"(见这里)。但我显然没有在这里使用任何 page.evaluate(),而且因为脚本在标记中按顺序引用,浏览器应该能够获取定义。事实上,它确实这样做了,它似乎只有在包含类定义时才会中断。

如果它有帮助,我正在运行 Windows 10 和 PhantomJS 的 2.1.1 版。从窗口的命令提示符运行PhantomJS时遇到同样的问题,所以我认为这不是Selenium问题。

任何人都可以帮忙吗?

谢谢!

事实证明,

这是 PhantomJS 对 ECMA6 支持的问题;我正在使用TypeScript并将其编译为ECMA6,它适用于Chrome/Firefox,但PhantomJS尚不支持。