{"id":3355,"date":"2025-01-16T05:43:23","date_gmt":"2025-01-16T05:43:23","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3355"},"modified":"2025-01-17T10:43:07","modified_gmt":"2025-01-17T10:43:07","slug":"emulate-mock-geo-locations-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/emulate-mock-geo-locations-using-playwright-java\/","title":{"rendered":"Emulate\/Mock GEO Locations using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will learn about how to mock\/emulate geo locations using playwright java.<\/em><strong>&nbsp;<\/strong><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Mock geo location using Playwright-Java for kfc website<\/li>\n<li>Mock geo location using Playwright-Java for whatmylocation website<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Mock geo location using Playwright-Java for kfc website<\/strong><\/p>\n<p>To explore this concept, go to <a href=\"https:\/\/locations.kfc.com\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/locations.kfc.com\/<\/a><\/p>\n<p>This website enables us to find a KFC restaurant in the United States<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3358\" width=\"529\" data-init-width=\"1160\" height=\"410\" data-init-height=\"898\" title=\"Screenshot 2025-01-16 at 11.14.33\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.14.33\u202fAM.png\" data-width=\"529\" data-height=\"410\" style=\"aspect-ratio: auto 1160 \/ 898;\"><\/span><\/p>\n<p>&nbsp;By clicking on the &#8216;USE MY LOCATION&#8217; 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<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3359\" width=\"559\" data-init-width=\"1160\" height=\"288\" data-init-height=\"598\" title=\"Screenshot 2025-01-16 at 11.15.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.15.26\u202fAM.png\" data-width=\"559\" data-height=\"288\" style=\"aspect-ratio: auto 1160 \/ 598;\"><\/span><\/p>\n<p>&nbsp;Consider the following scenario:&nbsp;<\/p>\n<p><em>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 &#8216;USE MY LOCATION&#8217; 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?<\/em><\/p>\n<p>To address this scenario, we have the option to simulate or imitate being in the United States. By selecting &#8216;USE MY LOCATION&#8217;, the results displayed will correspond to locations near your friend&#8217;s place in the US. Let&#8217;s observe this in action.<\/p>\n<p>Let us search for \u201c<em>New York city\u201d<\/em> co-ordinates<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3360\" width=\"569\" data-init-width=\"1160\" height=\"297\" data-init-height=\"606\" title=\"Screenshot 2025-01-16 at 11.16.48\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.16.48\u202fAM.png\" data-width=\"569\" data-height=\"297\" style=\"aspect-ratio: auto 1160 \/ 606;\"><\/span><\/p>\n<p>&nbsp; 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<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3361\" width=\"578\" data-init-width=\"1160\" height=\"302\" data-init-height=\"606\" title=\"Screenshot 2025-01-16 at 11.17.46\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.17.46\u202fAM.png\" data-width=\"578\" data-height=\"302\" style=\"aspect-ratio: auto 1160 \/ 606;\"><\/span><\/p>\n<p>&nbsp;Execute the code.<\/p>\n<p>Notice that playwright clicks \u2018USE MY LOCATION\u2019 and New York city is shown in search results instead of \u2018Delhi\u2019&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3362\" width=\"568\" data-init-width=\"1160\" height=\"794\" data-init-height=\"1622\" title=\"Screenshot 2025-01-16 at 11.18.25\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.18.25\u202fAM.png\" data-width=\"568\" data-height=\"794\" style=\"aspect-ratio: auto 1160 \/ 1622;\"><\/span><\/p>\n<p>&nbsp;Execute code.<\/p>\n<p>Notice below that DC stores are picked up!<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3363\" width=\"528\" data-init-width=\"1160\" height=\"415\" data-init-height=\"912\" title=\"Screenshot 2025-01-16 at 11.19.31\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.19.31\u202fAM.png\" data-width=\"528\" data-height=\"415\" style=\"aspect-ratio: auto 1160 \/ 912;\"><\/span><\/p>\n<p>&nbsp;This is how you can mock\/emulate a geo location using playwright-java.<\/p>\n<p><strong>Mock geo location using Playwright-Java for whatmylocation website<\/strong><\/p>\n<p>When we go to <a href=\"https:\/\/whatmylocation.com\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/whatmylocation.com\/<\/a> we see our current location (here \u2018Delhi\u2019)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3364\" width=\"496\" data-init-width=\"1160\" height=\"599\" data-init-height=\"1400\" title=\"Screenshot 2025-01-16 at 11.20.07\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.20.07\u202fAM.png\" data-width=\"496\" data-height=\"599\" style=\"aspect-ratio: auto 1160 \/ 1400;\"><\/span><\/p>\n<p>&nbsp;Notice below that current location is now DC<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3365\" width=\"539\" data-init-width=\"1160\" height=\"433\" data-init-height=\"932\" title=\"Screenshot 2025-01-16 at 11.20.58\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.20.58\u202fAM.png\" data-width=\"539\" data-height=\"433\" style=\"aspect-ratio: auto 1160 \/ 932;\"><\/span><\/p>\n<p>&nbsp;This is how we can mock the geo location when using <a href=\"https:\/\/whatmylocation.com\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/whatmylocation.com\/<\/a> website.<\/p>\n<p>You can further read official documentation<\/p>\n<p>https:\/\/playwright.dev\/java\/docs\/emulation#geolocation&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3366\" width=\"607\" data-init-width=\"1160\" height=\"425\" data-init-height=\"812\" title=\"Screenshot 2025-01-16 at 11.21.34\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.21.34\u202fAM.png\" data-width=\"607\" data-height=\"425\" style=\"aspect-ratio: auto 1160 \/ 812;\"><\/span><\/p>\n<p>&nbsp;<strong>Source code<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> com.microsoft.playwright.Browser;<strong>import<\/strong> com.microsoft.playwright.BrowserContext;<strong>import<\/strong> com.microsoft.playwright.BrowserType;<strong>import<\/strong> com.microsoft.playwright.Page;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>import<\/strong> com.microsoft.playwright.options.Geolocation;<strong>public<\/strong><strong>class<\/strong> Blog20_GeoLocation {<strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {Playwright playwright = Playwright.<em>create<\/em>();Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));BrowserContext context = browser.newContext();\/\/context.setGeolocation(new Geolocation(40.7128, -74.0060)); \/\/ New Yorkcontext.setGeolocation(<strong>new<\/strong> Geolocation(38.9072, -77.0369)); \/\/ DCPage page = context.newPage();&nbsp; page.navigate(&#8220;https:\/\/whatmylocation.com\/&#8221;);\/\/page.navigate(&#8220;https:\/\/locations.kfc.com\/&#8221;);\/\/page.click(&#8220;text = use my location&#8221;);}}Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will learn about how to mock\/emulate geo locations using playwright java.&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3355"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3355\/revisions"}],"predecessor-version":[{"id":3726,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3355\/revisions\/3726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}