我正在使用Geb为我的网站进行功能测试。我想使用无头浏览器,所以我选择了PhantomJS。该网站似乎加载,就像javascript被禁用一样。问题是我的网站是基于javascript的,而Geb无法检测到我需要测试的网站上的常规元素,例如按钮。你可以看到JS显然已启用。
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true)
PhantomJSDriver desktopDriver = new PhantomJSDriver(capabilities)
desktopDriver.manage().window().setSize(new Dimension(1024, 768))
我可以从日志输出中证明这一点......
[INFO - 2015-12-13T22:09:46.926Z] Session [389fe0a0-a1e6-11e5-89cc-4322be01a995] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"1.9.7","driverName":"ghostdriver","driverVersion":"1.1.0","platform":"mac-unknown-32bit",
"javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
当我从phantomJS转储页面源时,它会显示以下标记:
</body></html>
<!--?xml version="1.0" encoding="UTF-8"?--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Login</title></head>
<body onload="document.forms[0].submit()">
<!-- Notify user that they must perform an action if Javascript is disabled. -->
<noscript>
<p>
<strong>Note:</strong> Since your browser does not support Javascript,
you must press the continue button once to proceed.
</p>
</noscript>
<form action="https://www.myaction.com” method="post">
<div>
<input type="hidden" name=“request” value="">
</div>
<!-- Only provide a Continue button if Javascript is not enabled. -->
<noscript>
<div>
<input type="submit" value="Continue"/>
</div>
</noscript>
</form>
顺便说一句,相同的测试在Firefox和Chrome浏览器驱动程序中都能正常运行。
有没有人知道PhantomJS会发生这种事情的任何问题?