关闭

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

标签: geolocationGEO地理位置信息相关测试firefox
337人阅读 评论(0) 收藏 举报
分类:


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/


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74241次
    • 积分:83
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:3篇
    • 译文:0篇
    • 评论:13条
    最新评论