In this blog we will learn about how to mock/emulate geo locations using playwright java.
Topics that we will cover:
- Mock geo location using Playwright-Java for kfc website
- Mock geo location using Playwright-Java for whatmylocation website
- Source code
Mock geo location using Playwright-Java for kfc website
To explore this concept, go to https://locations.kfc.com/
This website enables us to find a KFC restaurant in the United States

By clicking on the 'USE MY LOCATION' link, we observed that the application promptly switched to our current location in New Delhi. However, it did not yield any results since this site exclusively caters to the US market, as previously mentioned

Consider the following scenario:
You wish to surprise your friend, who resides in Nevada, US, by ordering a KFC meal and having it delivered to their location. To ensure the nearest KFC to their house is selected, you opt to utilize the 'USE MY LOCATION' feature. However, a challenge arises as you are currently in India, and when you activate the feature, it detects New Delhi instead. How would you assist your friend in this situation?
To address this scenario, we have the option to simulate or imitate being in the United States. By selecting 'USE MY LOCATION', the results displayed will correspond to locations near your friend's place in the US. Let's observe this in action.
Let us search for “New York city” co-ordinates

So the complete code will be as shown below. We are setting the co-ordinates in line#17. Make sure that the longitude is prefixed with a negative sign

Execute the code.
Notice that playwright clicks ‘USE MY LOCATION’ and New York city is shown in search results instead of ‘Delhi’

Execute code.
Notice below that DC stores are picked up!

This is how you can mock/emulate a geo location using playwright-java.
Mock geo location using Playwright-Java for whatmylocation website
When we go to https://whatmylocation.com/ we see our current location (here ‘Delhi’)

Notice below that current location is now DC

This is how we can mock the geo location when using https://whatmylocation.com/ website.
You can further read official documentation
https://playwright.dev/java/docs/emulation#geolocation

Source code