w3c / webdriver-bidi

Bidirectional WebDriver protocol for browser automation
https://w3c.github.io/webdriver-bidi/
336 stars 35 forks source link

Emulation: Support geolocation override #343

Open jimevans opened 1 year ago

jimevans commented 1 year ago

One request often heard from users is that they want to simulate a browser at a specific geolocation for their sites. WebDriver should provide a mechanism to facilitate this testing.

whimboo commented 1 year ago

Thank you @jimevans for filing!

I want to add that clients should be able to set this location override per tab.

css-meeting-bot commented 3 months ago

The Browser Testing and Tools Working Group just discussed Geolocation emulation.

The full IRC log of that discussion <jgraham> Topic: Geolocation emulation
<jgraham> github: https://github.com/w3c/webdriver-bidi/issues/343
<orkon> q+
<jgraham> jgraham: We know there are requests for this and Puppeteer has it, so this should be the next priority for emulation and overrides
<jgraham> ack next
<jgraham> orkon: We support this in puppeteer. The blocker for using this in BiDi is that we need to be able to provide permissions. It might be possible to use a preload script to override things, but I'm not sure.
<jgraham> q?
<jgraham> jgraham: I assumed we'd just provide mock data, does that require implementing permissions?
<jgraham> orkon: To request geolocation data you first have to give permissions. We could say that providing the data atuo-grants the permissions, but it might make sense to do permissions first, if that's needed cross-browser.
<orkon> q+
<JimEvans> q+
<jgraham> jgraham: Makes sense. We can do the work in parallel though; browsers might have some out of band way to disable the permissions prompt so you could still test that the right geolocation data is returned without supporting the permissions API.
<jgraham> ack next
<jgraham> orkon: Perhaps there is also a relation to the user prompt handing changes; permissions are like user prompts and we could have similar auto-accept functionality.
<jgraham> ack next
<jgraham> JimEvans: Does the permissions spec have a BiDi implementation?
<jgraham> orkon: We have it in Chrome, but I don't know about other implementations. It only covers overrides.
<jgraham> jgraham: We have the permissions spec implemented in classic, but not yet for BiDi (need to check which parts are actually shipping already)
<jgraham> RRSAgent: make minutes
<RRSAgent> I have made the request to generate https://www.w3.org/2024/04/10-webdriver-minutes.html jgraham
<jgraham> zakim, bye
<Zakim> leaving. As of this point the attendees have been AutomatedTester_, orkon, jdescottes, whimboo, jgraham, sasha, MaksimSadym, JimEvans, lightning00blade
<sasha> orkon: `browsingContext.setViewport` command already has a step to check if a specified context is a top-level context, and throw an error if it's not, so we don't need to do anything for iframes
<jgraham> RRSAgent: bye
<RRSAgent> I see no action items
OrKoN commented 3 months ago

Should we propose it as part of the https://w3c.github.io/geolocation-api/ spec?

OrKoN commented 3 months ago

Chrome currently supports latitude, longitude, accuracy overrides which works for the top-level context and iframes. It does not affect the permission and it does not allow to use geolocation on insecure contexts.

christian-bromann commented 3 months ago

WebdriverIO currently already supports this by overwriting the navigator.geolocation interface in the browser via the preload script:

const patchedFn = options instanceof Error
    ? `cbError(new Error(${JSON.stringify(options.message)}))`
    : `cbSuccess({
        coords: ${JSON.stringify(options)},
        timestamp: Date.now()
    })`
await this.scriptAddPreloadScript({
    functionDeclaration: /*js*/`() => {
        Object.defineProperty(navigator.geolocation, 'getCurrentPosition', {
            value: (cbSuccess, cbError) => ${patchedFn}
        })
    }`
})
OrKoN commented 3 months ago

@christian-bromann Thanks for sharing! Do you support changing it after the page is loaded/during the test? Also, what about Geolocation.watchPosition()?

I think the override on the browser side would be more robust but preload scripts offer the ability to implement something already.

christian-bromann commented 3 months ago

I agree. The current emulation features we have can't be changed after the page has loaded. It would be helpful, especially for component tests to modify these capabilities without having to do a page load.