2

不确定这是否可能......我们如何测试从不同位置的浏览?

这是为了测试使用 GeoPlugin 的 Web 应用程序。该插件根据用户的 IP 提供地理信息(城市、纬度、经度...),而 Web 应用程序又使用这些信息来生成自定义主页。

除了从另一个位置进行物理浏览之外,还有什么方法可以对此进行测试?

谢谢!

4

1 回答 1

0

您可以通过覆盖 navigator.geolocation 对象来模拟 Webkit 浏览器中的位置。在 Google Chrome 中尝试以下示例:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <script type="text/javascript" language="JavaScript">
            /* Replace navigator.geolocation with mock */

            var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
            if (typeof navigator == "undefined" || navigator === null) {
              window.navigator = {};
            }

            // Replace real geolocation with mock geolocation
            delete navigator.geolocation;
            navigator.geolocation = {
              isMock: true,
              paused: true,
              delay: 1000,
              shouldFail: false,
              failsAt: -1,
              unFailsAt: -1,
              errorMessage: "There was an error retrieving the position!",
              currentTimeout: -1,
              lastPosReturned: 0,
              overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints)
              useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy

              _geoCall: function(method, success, error, repeat) {

                  return this.currentTimeout = window[method].call(null, __bind(function() {

                    var nextPos;

                    if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){
                        nextPos = this.lastPosReturned++;
                    }else{
                        this.lastPosReturned = nextPos = 0;
                    }


                    if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true;
                    if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false;

                    if(repeat) this._geoCall("setTimeout", success, error, true);

                    if (this.shouldFail && (error != null)) {
                        return error(this.errorMessage);
                    }
                    else if (success != null && (!this.paused || !repeat)) {
                        var result = this.waypoints[nextPos];
                        result.isMock = true;
                        if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;
                        success(result);
                        return nextPos;
                    }
                  }, this), this.delay);

              },
              getCurrentPosition: function(success, error) {
                return this._geoCall("setTimeout", success, error, false);
              },
              watchPosition: function(success, error) {
                this._geoCall("setTimeout", success, error, true);
                return this.currentTimeout;
              },
              clearWatch: function(id) {
                return clearInterval(this.currentTimeout);
              },
              waypoints: []
            };
        </script>

        <script type="text/javascript" language="JavaScript">
            navigator.geolocation.waypoints = [
                {coords:{latitude:50.001, longitude:-4.001}},
                {coords:{latitude:50.001, longitude:-4.002}},
                {coords:{latitude:50.001, longitude:-4.003}},
                {coords:{latitude:50.001, longitude:-4.004}},
                {coords:{latitude:50.001, longitude:-4.005}},
                {coords:{latitude:50.001, longitude:-4.006}},
                {coords:{latitude:50.001, longitude:-4.007}},
                {coords:{latitude:50.001, longitude:-4.007}},
                {coords:{latitude:50.001, longitude:-4.009}},
                {coords:{latitude:50.001, longitude:-4.010}},
                {coords:{latitude:50.001, longitude:-4.011}},
                {coords:{latitude:50.001, longitude:-4.012}},
                {coords:{latitude:50.001, longitude:-4.013}},
                {coords:{latitude:50.001, longitude:-4.014}},
                {coords:{latitude:50.001, longitude:-4.015}},
                {coords:{latitude:50.001, longitude:-4.016}},
                {coords:{latitude:50.001, longitude:-4.017}},
                {coords:{latitude:50.001, longitude:-4.017}},
                {coords:{latitude:50.001, longitude:-4.019}},
                {coords:{latitude:50.001, longitude:-4.020}}
            ];

            function runTests(){
                navigator.geolocation.paused = false; // run geomocking
                test1();
            }

            function test1(){
                console.log("Test 1: Get current position");
                navigator.geolocation.getCurrentPosition(
                    function(position){
                        console.log("Position updated: latitude="+position.coords.latitude+", longitude="+position.coords.longitude);
                        test2();
                    },
                    function(error){
                        console.error("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
                    }
                );
            }

            function test2(){
            console.log("Test 2: Watch position");
                navigator.geolocation.watchPosition(
                    function(position){
                        console.log("Position updated: latitude="+position.coords.latitude+", longitude="+position.coords.longitude);
                    },
                    function(error){
                        console.error("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
                    }
                );
            }

            document.addEventListener('DOMContentLoaded', runTests, false);
        </script>
    </head>
    <body>

    </body>
</html>
于 2013-10-24T22:14:54.423 回答