Webdriver - Firefox 设置地理位置信息相关测试

转载 2015年11月19日 18:29:01


Webdriver - Firefox 设置地理位置信息相关测试

作者:

时间: 2015/11


Faking Geolocation in Selenium WebDriver with Firefox


I recently worked on some Selenium WebDriver tests that needed to provide a Geolocation to a HTML5 site so it could display some results. There’s a couple of things to keep in mind when doing this:

  1. There are two Firefox about:config preferences that you must both set in your WebDriver Firefox profile to always supply the Geolocation information to your test (instead of displaying a prompt). These are:
    1. set ‘geo.prompt.testing’ to true
    2. set ‘geo.prompt.testing.allow’ to true
  2. If you want to use a specific location you need to override an about:config preference to a JSON file containing that location. This is only supported in very recent versions of Firefox (I tested version 31). Whilst this can bemanually done using a data:application/json string, Firefox using WebDriver in C# completely ignores this so the workaround is to create a JSON file with the location and reference using using a file:/// prefix
    1. Create C:\Dev\Brussels.json etc containing something like:
      {
          "status": "OK",
          "accuracy": 10.0,
          "location": {
              "lat": 50.850780,
              "lng": 4.358138,
              "latitude": 50.850780,
              "longitude": 4.358138,
              "accuracy": 10.0
          }
      }
    2. set ‘geo.wifi.uri’ to ‘file:///C:/Dev/brussels.json’ (or wherever your file is)

Once you’ve done this you should be able to test Geolocation without seeing the prompt or being able to completely override the location. Some example C# WebDriver scripts are below.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace GeoLocation.Tests.Acceptance
{
    [TestClass]
    public class WebDriverGeoLocation
    {
        [TestMethod]
        public void CanOverrideLocationInNewerVersionsOfFirefoxLike31()
        {
            var profile = new FirefoxProfile {EnableNativeEvents = false};
            profile.SetPreference("geo.prompt.testing", true);
            profile.SetPreference("geo.prompt.testing.allow", true);
            profile.SetPreference("geo.wifi.uri", "file:///C:/Dev/brussels.json");
            var driver = new FirefoxDriver(profile);
            driver.Navigate().GoToUrl("http://html5demos.com/geo");
            new WebDriverWait(driver, TimeSpan.FromSeconds(15)).Until(ExpectedConditions.ElementExists((By.ClassName("success"))));
            Assert.AreEqual("found you!", driver.FindElement(By.Id("status")).Text);
            driver.Close();
        }

        [TestMethod]
        public void CantOverrideLocationInOlderVersionsOfFirefoxLike24()
        {
            var profile = new FirefoxProfile { EnableNativeEvents = false };
            profile.SetPreference("geo.prompt.testing", true);
            profile.SetPreference("geo.prompt.testing.allow", true);
            var driver = new FirefoxDriver(profile);
            driver.Navigate().GoToUrl("http://html5demos.com/geo");
            new WebDriverWait(driver, TimeSpan.FromSeconds(15)).Until(ExpectedConditions.ElementExists((By.ClassName("success"))));
            Assert.AreEqual("found you!", driver.FindElement(By.Id("status")).Text);
            driver.Close();
        }
    }
}

Enjoy your Geolocation testing!


原文:http://watirmelon.com/2014/09/18/faking-geolocation-in-selenium-webdriver-with-firefox/


webDriver + Firefox 浏览器 完美兼容

webDriver + Firefox 浏览器 完美兼容 搞java最烦的就是不同版本的适配问题。现分享下实测成功的案例。 Firefox:4.0.1 selenium:selenium...

iOS 关于图片地理位置隐私信息的分析和读取

今天突然想到微信朋友圈发照片,涉及个人隐私的地理位置是否外泄。因为iphone拍照的照片都会带有地理位置等信息,我们先来实现怎么读取里面的安全信息,然后再来分析 #import "ViewContr...

【Android笔记 二】Location获取地理位置信息(上)

2011 7 22暑假实训的第五天,跟大家分享以下我对Android location的学习吧,这是一个最基本的获取地理位置信息的入门,下一次给大家介绍更质能化的地理位置选择Criteria,今天就先...

android地理位置信息修改

  • 2017年11月09日 12:47
  • 4.12MB
  • 下载

演示:HTML5获取地理位置定位信息

演示:HTML5获取地理位置定位信息 .demo{width:560px; margin:60px auto 10px auto} .geo{margin-top:20px} .demo p...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Webdriver - Firefox 设置地理位置信息相关测试
举报原因:
原因补充:

(最多只允许输入30个字)