gpt4 book ai didi

javascript - PhantomJS 不编译带有类定义的 JavaScript 脚本

转载 作者:行者123 更新时间:2023-11-27 22:49:13 25 4
gpt4 key购买 nike

好吧,我是 PhantomJS 和整个 headless 浏览的新手。我正在使用 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;
}
}

bar.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 页面时,出现以下错误:

[ERROR - 2016-07-04T21:19:44.274Z] Session [f2c46410-422c-11e6-881e-555d71de793e] - page.onError - msg: ReferenceError: Can't find variable: add

phantomjs://platform/console++.js:263 in error [ERROR - 2016-07-04T21:19:44.275Z] Session [f2c46410-422c-11e6-881e-555d71de793e] - page.onError - stack:
global code (http://localhost:56135/Scripts/bar.js:1)

phantomjs://platform/console++.js:263 in error

当我执行第一行(导航)时,就会出现错误。但是,一旦我从 foo.js 中删除(Polygon)类定义,一切就变得很美好:

this works...

1 + 2 = 3

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

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

有人可以帮忙解决这个问题吗?

谢谢!

最佳答案

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

关于javascript - PhantomJS 不编译带有类定义的 JavaScript 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com