简体   繁体   中英

Disable geolocation browser using javascript

I'm writing an integration test using QUnit, and in my webapp if geolocation is not available, (I use if (navigator.geolocation) to detect if is present or not, but I want to disable it in order to implement the test when geolocation is not available.

I tried navigator.geolocation = undefined but unfortunately it didn't work.

You can't really "disable" navigator.geolocation since it's a read-only property.

Instead, you could create a wrapper that checks for certain functionality and then mock it up for your tests.

var supports = {
  geolocation: !!navigator.geolocation // will be `true` if geolocation is defined
};

// In your tests...
supports.geolocation = false;
executeTest();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM