如何在 chromedriver 中启用地理定位支持? [英] How do I enable geolocation support in chromedriver?

查看:66
本文介绍了如何在 chromedriver 中启用地理定位支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Selenium 测试 JS 地理定位功能,并且我正在使用 chromedriver 在最新的 Chrome 上运行测试.

现在的问题是 Chrome 在测试期间提示我启用地理定位,而我不知道如何在运行时单击那个小栏,所以我拼命寻找一种方法来启动 chromedriver 和 chrome 并带有一些选项或触发以默认启用此功能.我能在此处找到的只是我如何完全禁用地理定位.>

我该如何解决这个问题?

解决方案

chromedriver wiki 的已知问题部分 他们说你不能指定自定义配置文件

这就是为什么在我看来,@Sotomajor 关于在 Chrome 中使用配置文件的回答,就像在 Firefox 中一样行不通.

在我的一次集成测试中,我遇到了同样的问题.但是因为我并不关心真正的地理位置值,所以我所要做的就是模拟 window.navigator.gelocation

在您的 Java 测试代码中,将此解决方法用于避免 Chrome geoloc 权限信息栏.

<前>chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition =功能(成功){var position = {坐标":{"纬度": "555",经度":999"}};成功(位置);}");

这里的纬度 (555) 和经度 (999) 值只是测试值

I need to test a JS geolocation functionality with Selenium and I am using chromedriver to run the test on the latest Chrome.

The problem is now that Chrome prompts me to enable Geolocation during the test and that I don't know how to click that little bar on runtime, so I am desperately looking for a way to start the chromedriver and chrome with some option or trigger to enable this by default. All I could find here was however how I can disable geolocation altogether.

How can I solve this issue?

解决方案

In the known issues section of the chromedriver wiki they said that you Cannot specify a custom profile

This is why it seems to me that @Sotomajor answer about using profile with Chrome as you would do with firefox will not work.

In one of my integration tests I faced the same issue. But because I was not bothering about the real geolocation values, all I had to do is to mock window.navigator.gelocation

In you java test code put this workaround to avoid Chrome geoloc permission info bar.

chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition = 
    function(success){
         var position = {"coords" : {
                                       "latitude": "555", 
                                       "longitude": "999"
                                     }
                         }; 
         success(position);}");

latitude (555) and longitude (999) values here are just test value

这篇关于如何在 chromedriver 中启用地理定位支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆