Protractor: Geolocation in Google Chrome

I am currently developing a hybrid app using Ionic.
For end-to-end tests, they recommend Protractor.
Furthermore, I use a Selenium docker container.

As many other apps, I make use of geolocation to receive the user's position. However, when I tried to run a test with geolocation, it failed. I then used VNC to see if there is any error message, and I discovered Google Chrome prompts the user to allow or disallow geolocation. I searched the internet for answers, but couldn't find any. This didn't work for me.

On the Selenium website they describe a way to change the preferences passed to the GoogleDriver using the ChromeOptions. After some more experimenting I found there is an option "profile.default_content_setting_values.geolocation". If this is set to 1, the browser will no longer ask the user for permission.
Happy testing!

exports.config = {
  capabilities: {
    browserName: "chrome",
    chromeOptions: {
      prefs: {
        "profile.default_content_setting_values.geolocation": 1,
  // ... rest of your configuration

Oh, and now I found there is an issue about this topic with the same answer here.


Ok, the reason why the mocked version of (see above) didn't work for me was the timing. My app immediately tries to get the location, right after page load. The execution of browser.executeScript() just takes too long to have any effect, the geolocation has already been processed. After inserting a manual setTimeout(..., 1000) it worked. BUT: I do not want to have any artificial delay just to get my tests right. Ummm...have to think about that...

At some point there will be a locate-me-now-button, so I will probably make my tests click that button after executeScript(fakeGps). It is not exactly what happens at user's end, but I think it will be good enough for the test. :|