0

I just phantomjs + jasmine + requirejs + saga to test JavaScript codes.

I used the way to mock navigator.geolocation.getcurrentposition following 0 in Does phantomJS support geolocations?

My JavaScript code looks like this:

define(function (require) {

'use strict';
var authenticator = {};
var Authenticator = require("clientservice/authenticator/Authenticator");
var webpage = require('webpage');
describe("Request geolocation information", function(){
    beforeEach(function(){
        webpage.onInitialized = function() {
            webpage.injectJs('geolocation.js');
        };
        authenticator =  new Authenticator(configObj);
    });

    it("will request browser API for getting geolocation information", function(){
         authenticator.authenticate(mockController.successHandler);

     });
});

Then I got below error when running mvn clean package verify.

  • New Session Created: 6246caf0-4e47-11e6-bc1f-bf8e9b2d1d0e [ERROR - 2016-07-20T06:58:38.443Z] Session [6246caf0-4e47-11e6-bc1f-bf8e9b2d1d0e ] - page.onError - msg: Error: Script error for: webpage

Any idea about it?

Community
  • 1
  • 1
Sunny
  • 1

1 Answers1

0

I tried another way to mock the navigator.geolocation:

if(navigator.geolocation === void 0) {
   console.log("It's Ok, we can mock one!");
}

navigator.geolocation = function() {};
navigator.geolocation.getCurrentPosition = function(callBack) {
    console.log("Mock navigator is invoked!");
    callBack();};

function callBack() {
   console.log("callBack is invoked!");
};

if (navigator.geolocation.getCurrentPosition === void 0) {
    console.log("Oh my god!");
}

navigator.geolocation.getCurrentPosition(callBack);

phantom.exit();

Although function callBack is invoked, there is on error when running the js: callBack is invoked! TypeError: 'undefined' is not a function (evaluating 'navigator.geolocation.getC urrentPosition(callBack)()')

Sunny
  • 1