在 HTML5 ,新特性 Geolocation 用于定位用户位置信息。

由于用户位置信息是敏感信息,所以需要得到用户允许后,才能让程序通过 API  获取当前用户信息。WebDriver 程序每次重新允许都是新的会话进程,及时之前在浏览器中已经通过手工方式运行浏览器访问用户的位置信息,但在当前运行环境中依旧无法获取之前用户设置。解决方法是让浏览器每次执行 WebDriver 测试程序时,依旧加载之前用户设置即可。

以Firefox 为例,在Mac OS 平台上,可通过如下命令打开用户 Profile 管理器

$ /Applications/Firefox.app/Contents/MacOS/firefox-bin -ProfileManager

其他平台打开方式查询官方开发者文档:

https://developer.mozilla.org/en-US/docs/Mozilla/Multiple_Firefox_Profiles

创建 geolocation Profile 成功后,单击 Start Firefox 启动 Firefox 浏览器。

以 http://www.weschools.com/html/html5_geolocation.asp 为例。为演示完整的示例代码,还需创建一个包含 Geolocation 信息的 JSON 文件,这里命名为 location.json,内容如下:

{

  "status":"OK",

  "accuracy":10.0,

  "location":{"lat":52.1771129, "lng":5.4}

}

示例:

package com.learningselenium.html5;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxProfile;

import org.openqa.selenium.firefox.internal.Profileslni;

import  org.testng.annotations.*;

public class testHTML5Geolocation{

  private static WebDriver driver;

  @BeforeClass

  public void setUp() throws Exception{

    //获取geolocation Profile

    FirefoxProfile profile = new Profileslni().getProfile("geolocation");

    //配置Geolocation 信息

    profile.setPreference("geo.wifi.uri", "/Selenium 2/mydoc/codes/4/location.json");

    //通过定制 profile 启动浏览器

    driver = new FirefoxDriver(profile);

    driver.get("http://www.weschools.com/html/html5_geolocation.asp");

  }

  @Test

  public void testGetLocation() throws Exception{

    driver.findElement(By.cssSelector("p#demo button")).click();

  }

  @AfterClass

  public void tearDown() throws Exception{

    driver.quit();

  }

}

最新文章

  1. FZU 1914 单调队列
  2. 第二章 Mybatis代码生成工具
  3. mysql出现“SELECT list is not in GROUP BY clause and contains nonaggregated column [duplicate]”错误提示
  4. wxPython Major类
  5. H5单页面架构:backbone + requirejs + zepto + underscore
  6. httl开源JAVA模板引擎,动态HTML页面输出
  7. java集合循环删除
  8. 什么是Annotation
  9. 错误:set Assigning an instance of 'esri.***' which is not a subclass of 'esri.***‘
  10. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)
  11. stark组件之delete按钮、filter过滤
  12. Elastic-search在linux上的安装
  13. linux basic test
  14. day056 多表增加和查询
  15. js - 如何使子元素阻止继承父元素事件
  16. 为什么虚拟DOM更优胜一筹
  17. Mato的文件管理 (莫队)题解
  18. 配置Tomcat直接显示目录结构和文件列表
  19. 单源最短路:Bellman-Ford算法 及 证明
  20. NSIS制作安装包,如何检测并卸载已有版本

热门文章

  1. Codeforces 849B Tell Your World (计算几何)
  2. ganglia-monitoring-centos-linux
  3. uicollectionview 使用uibutton或者uiimageview实现旋转出现scale的问题
  4. 【Lucene】具体解释Lucene全文检索的信息写入与读取
  5. 【Todo】RTP/RTCP/RTSP/SIP/SDP 等多媒体传输和会话协议
  6. PostgreSQL触发器的使用
  7. 程序的载入和运行(五)——《x86汇编语言:从实模式到保护模式》读书笔记25
  8. Linux - Unix环境高级编程(第三版) 代码编译
  9. redis中关于过期键的删除策略
  10. BoW(SIFT/SURF/...)+SVM/KNN的OpenCV 实现