How do I enable geolocation support in chromedriver?

Thomas Keller picture Thomas Keller · Dec 7, 2011 · Viewed 16.8k times · Source

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?

Answer

redochka picture redochka · Apr 3, 2012

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