[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" ,但是并没有产 ...
随机推荐
- golang测试框架--smartystreets/goconvey
视频教程和配套博客:goconvey - 课时 1:优雅的单元测试 Go 语言虽然自带单元测试功能,在 GoConvey 诞生之前也出现了许多第三方辅助库.但没有一个辅助库能够像 GoConvey 这 ...
- .net core 使用 codegenerator 创建默认CRUD代码
dotnet.exe aspnet-codegenerator controller --force --controllerName [controller-name] --relativeFold ...
- XCTFreverse第一道DMD
MD5解一次密即可
- 使用wget进行整站下载(转)
wget在Linux下默认已经安装,Windows下需要自行安装. Windows下载地址:http://wget.addictivecode.org/Faq.html#download,链接:htt ...
- 如何快速的知道Maven插件的命令行输入参数
用命令行使用Maven的插件时,-D表示属性的输入,-P表示构建配置文件的输入. 比如要使用package生命周期阶段对Application项目进行打包jar时,查找方式如下: 1.由于packag ...
- Git checkout on a remote branch does not work
I believe this occurs when you are trying to checkout a remote branch that your local git repo is no ...
- [Testing] Static Analysis Testing JavaScript Applications
The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript pro ...
- docker nginx镜像+phpfpm 镜像 组合配置 搭建 PHP+nginx 环境
前言 在以往的容器环境部署中 运行环境 我们通常把 类似 apache nginx php 等 打包在一个镜像中 起一个容器. 这样做的好处是 方便 简单. 不便的地方是 如果PHP 需要扩展新的 相 ...
- 信雅达面试题atoi函数实现
atoi函数: 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); 函数说明 参数nptr字符串,如 ...
- iOS UILabel换行同时修改字体大小颜色
UIButton *onlyPriceBtn = [UIButton buttonWithType:UIButtonTypeCustom]; onlyPriceBtn.layer.borderColo ...