Is it possible to change to multiple geolocations in the middle of Playwright tests?

Is it possible to change to multiple geolocations in the middle of Playwright tests?

Hi Dipen,

Yes, you can change to multiple geolocation while running Playwright tests on LambdaTest.

Please refer to the below code for the same:

const {chromium} = require(""playwright"");

(async () => {
    let browser, address;
    try {
        const capabilities = {
            ""browserName"": ""Chrome"",
            ""browserVersion"": ""latest"",
            ""LT:Options"": {
                ""platform"": ""Windows 10"",
                ""build"": ""Test GPS build"",
                ""name"": ""Change GPS within the test"",
                ""user"": process.env.LT_USERNAME,
                ""accessKey"": process.env.LT_ACCESS_KEY,
                ""network"": true,
                ""video"": true,
                ""console"": true,
            },
        };

        browser = await chromium.connect({
            wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(
                JSON.stringify(capabilities))}`,
        });

        const context = await browser.newContext({
            permissions: ['geolocation'],
            geolocation: {latitude: 40.758896, longitude: -73.985130}
        });

        const page = await context.newPage();

        // Get initial geolocation, NYC
        await page.goto('https://www.gps-coordinates.net/my-location')
        await page.waitForTimeout(1000)
        address = await page.locator('#addr').textContent()
        console.log('Current location=====>', address)

        // Set geolocation as Bangalore
        await context.setGeolocation({latitude: 12.972442, longitude: 77.580643});
        await page.goto('https://www.gps-coordinates.net/my-location')
        await page.waitForTimeout(1000)
        address = await page.locator('#addr').textContent()
        console.log('Current location=====>', address)

        await page.close();
        await browser.close();
    } catch (e) {
        console.log(""ERROR:: "", e);
        await page.close();
        await browser.close();
    }
})();"