Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for geolocation mock? #1077

Closed
dickeylth opened this issue Oct 18, 2017 · 17 comments
Closed

Support for geolocation mock? #1077

dickeylth opened this issue Oct 18, 2017 · 17 comments

Comments

@dickeylth
Copy link
Contributor

Steps to reproduce

Tell us about your environment:

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. use puppeteer to capture performance for the page.
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
const co = require('co');
const path = require('path');
co(function* () {
  const PAGE_URL = 'https://h5.m.taobao.com/trip/home/index.html';
  const browser = yield puppeteer.launch({
    headless: false,
    ignoreHTTPSErrors: true,
    args: [
      '--auto-open-devtools-for-tabs'
    ],
    userDataDir: path.join(__dirname, 'user-dir')
  });
  const page = yield browser.newPage();
  yield page.emulate(iPhone);

  try {
    yield page.goto(PAGE_URL);
  } catch (e) {
    console.error(`>> Load page ${PAGE_URL} error encounted: `);
    console.error(e.stack);
  }
  yield page.waitForNavigation({
    waitUntil: 'networkidle',
    networkIdleTimeout: 2000
  });
  const perfEntries = yield page.evaluate(() => performance.getEntries());
  console.log(perfEntries);

  browser.close();

  return stats;
}).then((stats) => {
  console.log('END');
  console.log(stats);
}).catch(e => {
  console.error(e.stack);
});
  1. allow geolocation permission
  2. set the preset geolocation in devtools/sensors:

image

What is the expected result?

The Geolocation preset config is saved to user-dir, and next time chromium should use the settings here to enable geolocation mock.

What happens instead?

The settings in devtools/sensors were not persistent, next time the code runs the geolocation settings were reset, which made the code in the page request geolocation failed.

Or is there any way to set the geolocation mock by args param when chromium launch?

@ebidel
Copy link
Contributor

ebidel commented Oct 18, 2017

Since you're passing a userDataDir, I'd imagine these settings would be preserved. Could be a bug in Devtools/Chrome. cc @aslushnikov

@ebidel
Copy link
Contributor

ebidel commented Oct 18, 2017

@saselovejulie
Copy link

@dickeylth @ebidel
hi,

set the preset geolocation in devtools/sensors:

how do you update chrome location? by javascript code or by yourself operation?
is there any way that i can update my geolocation by javascript and let google map use this geolocation?

@saselovejulie
Copy link

navigator.geolocation.getCurrentPosition = function(success, failure) {
success({ coords: {
latitude: 31.230416,
longitude: 121.473701
}, timestamp: Date.now() });
}
maybe i can use this function to change my geolocation, but how could i enable Geolocation from "No override" to "Custom location" with js code on console?
1508752797

@ebidel
Copy link
Contributor

ebidel commented Oct 23, 2017 via email

@aslushnikov
Copy link
Contributor

This should be easy to implement using the Emulation.setGeolocationOverride method.
Inspiration can be taken from other similar methods, e.g.
https://github.com/GoogleChrome/puppeteer/blob/380bb6a87f23f423df558c2cdb91bad8926464e0/lib/Page.js#L557-L567

I'd be happy to review a PR.

@AshwinDNair
Copy link

how do you override the geolocation settings? @ebidel

@ebidel
Copy link
Contributor

ebidel commented Oct 28, 2017

We need to add that feature. What @aslushnikov suggested :)

@slohmes
Copy link
Contributor

slohmes commented Oct 31, 2017

Is anyone working on a fix for this? If not, I'd like to try implementing it!

@ebidel
Copy link
Contributor

ebidel commented Oct 31, 2017

@slohmes don't think so. Feel free to send a PR with what was suggested in #1077 (comment).

@saikrishna321
Copy link

@ebidel any updates on this ?

@ebidel
Copy link
Contributor

ebidel commented Jun 12, 2018

We haven't prioritized this but would be willing to accept a PR. As mentioned you can also use the devtools protocol apis to override the settings.

@saikrishna321
Copy link

@ebidel i see there is PR #1297 but pretty old. Can you please confirm if i can raise a new PR for the same implementation ?

Can you point to any doc how i can use devtools protocol apis in puppeteer ?

@ebidel
Copy link
Contributor

ebidel commented Jun 12, 2018

Didn't see that. Seems like we need permission dialog support in puppeteer in order for that PR to work: #1297 (comment).

You can send raw protocol methods with:
https://pptr.dev/#?product=Puppeteer&version=v1.5.0&show=api-class-cdpsession

@saikrishna321
Copy link

@ebidel Thanks a lot 👍 let me try

@saikrishna321
Copy link

@ebidel i tried and the location still set to my current location

const page = await browser.newPage();
  const client = await page.target().createCDPSession();
  await client.send("Network.enable");
  const setCookie = await client.send("Emulation.setGeolocationOverride", {
    latitude: 27.1752868,
    longitude: 78.040009,
    accuracy: 100
  });
  await page.goto("https://maps.google.com");
  await page.waitFor(5000);
  process.exit();
}

I was expecting the maps to point to the lat n long given.

@298068845
Copy link

Support for Mobile deviceOrientation mock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants