[Selenium] HTML5 中的 Geolocation
在 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();
}
}
[Selenium] HTML5 中的 Geolocation的更多相关文章
- [Selenium] Android HTML5 中 Application Cache
HTML5 中引入了 Application Cache,这意味着 Web 应用程序可以被缓存到本地,且可在没有网络的情况下也能访问该 Web 应用程序 Application Cache 在以下3个 ...
- [Selenium] Android HTML5 中 Web Storage
在 HTML5 中,Web Storage 这个新特性可让用户将数据存储在本地的浏览器中.在早期的浏览器中可通过 cookies 来完成这个任务,但 Web Storage 会更加安全和高效,且 We ...
- 这是html5中WebGL的演示
这是html5中WebGL的演示,让我们与他人分享爱您发送短消息.每次你进入它使用不同的位置,新的爱情点被添加到全球.让世界更明亮的地方与你的朋友分享! 源文件:部分代码:<!DOCTYPE h ...
- html5中canvas的使用 获取鼠标点击页面上某点的RGB
1.html5中的canvas在IE9中可以跑起来.在IE8则跑不起来,这时候就需要一些东西了. 我推荐这种方法,这样显得代码不乱. <!--[if lt IE9]> <script ...
- html5中新增的form表单属性
html5中新增两个表单属性,分别autocomplete和novalidate属性 1.autocomplete属性 该属性用于控制自动完成功能的开启和关闭.可以设置表单或者input元素,有两个属 ...
- HTML5 中的 canvas 画布(一)
---恢复内容开始--- 在HTML5中新添加的元素,canvas 现在支持 IE9+的版本 注意:HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript ...
- HTML5中引入的关键特性
新特性 描述 accesskey 定义通过键盘访问元素的快捷键 contenteditable 该特性设置为true时,浏览器应该允许用户编辑元素的内容.不指定变化后的内容如何保存 contextme ...
- HTML5中与页面显示相关的API
1.HTML5中与页面显示相关的API 在HTML5中,增加了几个与页面显示相关的API,其中一个是Page Visibility API Page Visibility API 是指当页面变为最小 ...
- HTML5 中的新属性autocomplete="off"失效的解决方法(兼容firefox,IE,360)
因为业务需求,在写一个注册页面的时候,发现浏览器会自动填充此域名下已经保存的账号密码,给用户带来不便.加了HTML5 中的新属性autocomplete="off" ,但是并没有产 ...
随机推荐
- gcc、arm-Linux-gcc和arm-elf-gcc的组成及区别
2017-01-19 嵌入式ARM 嵌入式ARM 一.GCC简介 The GNU Compiler Collection,通常简称 GCC,是一套由 GNU 开发的编译器集,为什么是编辑器集而不是编译 ...
- R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)
Modified: 27-Oct-2013 Type: HOWTO In this Document Goal Solution References APPLIES TO: BI Publisher ...
- [React] Persist Form Data in React and Formik with formik-persist
It can be incredibly frustrating to spend a few minutes filling out a form only to accidentally lose ...
- zoj How Many Shortest Path
How Many Shortest Path 题目: 给出一张图,求解最短路有几条.处理特别BT.还有就是要特别处理map[i][i] = 0,数据有不等于0的情况! 竟然脑残到了些错floyd! ! ...
- HDU-1076-An Easy Task(Debian下水题測试.....)
An Easy Task Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- XUtils BitmapUtils 改造以加入drawable支持
=== XUtilsBitmapUtils 改造以加入drawable支持 === # XUtils 简单介绍 XUtils 是一套少有的早期国产安卓框架, 其源于AFinal, 文件夹结构也与之相似 ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- ShopMall
https://github.com/KingsleyYau/ShopMall-Android https://github.com/KingsleyYau/ShopMall-iOS https:// ...
- 让 Logo "飞" 出屏幕
让 Logo "飞" 出屏幕 推荐序 本文介绍了一种思路,即利用矢量工具来生成动画的关键代码,然后进一步制作成完整的动画效果,感谢作者授权转载. 作者介绍:一缕殇流化隐半边冰霜 ...
- iOS优秀博文合集
一.优化篇 Xcode7中你一定要知道的炸裂调试神技 iOS使用Instrument-Time Profiler工具分析和优化性能问题 dSYM 文件分析工具 二.功能篇 3分钟实现iOS语言本地化/ ...