PhantomJS支持地理位置吗?

8
我正在尝试用PhantomJS运行qunit测试用例。当PhantomJS尝试访问DOM的navigator.geolocation函数时,我的某个测试会挂起。相同的测试在浏览器中可以正常工作,但在控制台中与PhantomJS一起使用时却挂起。
PhantomJS是否支持地理位置信息?有什么建议吗?
在以下条件语句中中断:
  if(navigator.geolocation) {
            window.navigator.geolocation.watchPosition(updateLocation, null, { frequency: 3000 });
        }
2个回答

11

不需要。

只需查看features.js示例即可。

>phantomjs.exe features.js
Detected features (using Modernizr 2.0.6):

Supported:
  touch
  generatedcontent
  fontface
  flexbox
  canvas
  canvastext
  postmessage
  websqldatabase
  hashchange
  history
  draganddrop
  websockets
  rgba
  hsla
  multiplebgs
  backgroundsize
  borderimage
  borderradius
  boxshadow
  textshadow
  opacity
  cssanimations
  csscolumns
  cssgradients
  cssreflections
  csstransforms
  csstransitions
  localstorage
  sessionstorage
  webworkers
  applicationcache
  svg
  inlinesvg
  smil
  svgclippaths

Not supported:
  csstransforms3d
  webgl
  geolocation
  indexeddb
  video
  audio

非常感谢。这真的很有帮助。成功跳过了地理定位。 - Sajeewa Wickramarachchi

5
您可以通过在初始化时将所需的函数注入到DOM中来添加“geolocation-fake-support”以运行测试或执行其他操作。以下示例会伪造navigator.geolocation.getCurrentPosition,因此当浏览器中的网站调用此API端点时,PhantomJS将始终返回配置的位置。
webpage.onInitialized = function() {
    webpage.injectJs('geolocation.js')
};

geolocation.js:

window.navigator.geolocation = {
    getCurrentPosition: function (success, failure) {
        success({
            coords: {
                // Will always return Germany, Rostock
                latitude: 54.0834,
                longitude: 12.1004

            }, timestamp: Date.now()
        });
    }
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接