Does phantomJS support geolocations?

青春壹個敷衍的年華 提交于 2019-12-10 03:17:13

问题


I'm trying to run qunit test cases with PhantomJS. One of my tests are hanging in when phantomJS try to access the navigator.geolocation function of DOM. same test is working fine in the browser, just hangs in the console with phantomJS.

doe phantomJS support geolocations ? any suggestion?

breaks in the following if condition

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

回答1:


No.

Simply check the features.js example.

>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



回答2:


You can add "geolocation-fake-support" for running tests or doing other stuff by injecting the required functions into the DOM on initialization. The following example fakes the navigator.geolocation.getCurrentPosition, thus when the website in the browser calls this API endpoint PhantomJS will always return the configured location.

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()
        });
    }
};


来源:https://stackoverflow.com/questions/15894090/does-phantomjs-support-geolocations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!