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

来源:互联网 发布:sql求和保留两位小数 编辑:程序博客网 时间:2024/06/10 12:12


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
原创粉丝点击