-->

是否phantomJS支持geolocations?(Does phantomJS support

2019-08-20 14:54发布

我试图用PhantomJS运行qunit测试用例。 我的一个测试都挂在当phantomJS尝试访问DOM的navigator.geolocation功能。 同样的测试在浏览器工作正常,只是挂在phantomJS控制台。

美国能源部phantomJS支持geolocations? 什么建议吗?

在if条件下休息

  if(navigator.geolocation) {
            window.navigator.geolocation.watchPosition(updateLocation, null, { frequency: 3000 });
        }

Answer 1:

没有。

简单地检查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


Answer 2:

您可以添加“地理位置假票支持”运行测试或通过注射所需的功能集成到在初始化的DOM做其他的东西。 下面的例子假货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()
        });
    }
};


文章来源: Does phantomJS support geolocations?