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

23 篇文章 0 订阅
1 篇文章 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/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值