{"id":3355,"date":"2025-01-16T05:43:23","date_gmt":"2025-01-16T05:43:23","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/emulate-mock-geo-locations-using-playwright-java\/"},"modified":"2025-01-16T05:43:23","modified_gmt":"2025-01-16T05:43:23","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><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3355\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will learn about how to mock\/emulate geo locations using playwright java.<\/em><strong>\u00a0<\/strong><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\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 class=\"\" href=\"https:\/\/locations.kfc.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/locations.kfc.com\/<\/a><\/p>\n<p>This website enables us to find a KFC restaurant in the United States<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da53a63\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3358\" data-css=\"tve-u-1946da55a5f\" data-height=\"410\" data-id=\"3358\" data-init-height=\"898\" data-init-width=\"1160\" data-width=\"529\" decoding=\"async\" height=\"410\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.14.33\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 898;\" title=\"Screenshot 2025-01-16 at 11.14.33\u202fAM\" width=\"529\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0By 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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da6a112\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3359\" data-css=\"tve-u-1946da6b010\" data-height=\"288\" data-id=\"3359\" data-init-height=\"598\" data-init-width=\"1160\" data-width=\"559\" decoding=\"async\" height=\"288\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.15.26\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 598;\" title=\"Screenshot 2025-01-16 at 11.15.26\u202fAM\" width=\"559\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Consider the following scenario:\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da73b3f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3360\" data-css=\"tve-u-1946da74c15\" data-height=\"297\" data-id=\"3360\" data-init-height=\"606\" data-init-width=\"1160\" data-width=\"569\" decoding=\"async\" height=\"297\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.16.48\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 606;\" title=\"Screenshot 2025-01-16 at 11.16.48\u202fAM\" width=\"569\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da82880\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3361\" data-css=\"tve-u-1946da83cc6\" data-height=\"302\" data-id=\"3361\" data-init-height=\"606\" data-init-width=\"1160\" data-width=\"578\" decoding=\"async\" height=\"302\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.17.46\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 606;\" title=\"Screenshot 2025-01-16 at 11.17.46\u202fAM\" width=\"578\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute 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\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da8c2b1\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3362\" data-css=\"tve-u-1946da8d35a\" data-height=\"794\" data-id=\"3362\" data-init-height=\"1622\" data-init-width=\"1160\" data-width=\"568\" decoding=\"async\" height=\"794\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.18.25\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 1622;\" title=\"Screenshot 2025-01-16 at 11.18.25\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute code.<\/p>\n<p>Notice below that DC stores are picked up!<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946da9bb70\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3363\" data-css=\"tve-u-1946da9cc33\" data-height=\"415\" data-id=\"3363\" data-init-height=\"912\" data-init-width=\"1160\" data-width=\"528\" decoding=\"async\" height=\"415\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.19.31\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 912;\" title=\"Screenshot 2025-01-16 at 11.19.31\u202fAM\" width=\"528\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This 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 class=\"\" href=\"https:\/\/whatmylocation.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/whatmylocation.com\/<\/a> we see our current location (here \u2018Delhi\u2019)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946daa6639\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3364\" data-css=\"tve-u-1946daa771d\" data-height=\"599\" data-id=\"3364\" data-init-height=\"1400\" data-init-width=\"1160\" data-width=\"496\" decoding=\"async\" height=\"599\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.20.07\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 1400;\" title=\"Screenshot 2025-01-16 at 11.20.07\u202fAM\" width=\"496\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Notice below that current location is now DC<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946dab1af7\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3365\" data-css=\"tve-u-1946dab2a9f\" data-height=\"433\" data-id=\"3365\" data-init-height=\"932\" data-init-width=\"1160\" data-width=\"539\" decoding=\"async\" height=\"433\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.20.58\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 932;\" title=\"Screenshot 2025-01-16 at 11.20.58\u202fAM\" width=\"539\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how we can mock the geo location when using <a class=\"\" href=\"https:\/\/whatmylocation.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/whatmylocation.com\/<\/a> website.<\/p>\n<p>You can further read official documentation<\/p>\n<p>https:\/\/playwright.dev\/java\/docs\/emulation#geolocation\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946dab965c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3366\" data-css=\"tve-u-1946daba6b5\" data-height=\"425\" data-id=\"3366\" data-init-height=\"812\" data-init-width=\"1160\" data-width=\"607\" decoding=\"async\" height=\"425\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.21.34\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 812;\" title=\"Screenshot 2025-01-16 at 11.21.34\u202fAM\" width=\"607\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Source code<\/strong><\/p>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserContext;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.options.Geolocation;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog20_GeoLocation {<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/div>\n<div class=\"tcb-plain-text\">Playwright playwright = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions()<\/div>\n<div class=\"tcb-plain-text\">.setHeadless(<strong>false<\/strong>));<\/div>\n<div class=\"tcb-plain-text\">BrowserContext context = browser.newContext();<\/div>\n<div class=\"tcb-plain-text\">\/\/context.setGeolocation(new Geolocation(40.7128, -74.0060)); \/\/ New York<\/div>\n<div class=\"tcb-plain-text\">context.setGeolocation(<strong>new<\/strong> Geolocation(38.9072, -77.0369)); \/\/ DC<\/div>\n<div class=\"tcb-plain-text\">Page page = context.newPage();\u00a0 page.navigate(&#8220;https:\/\/whatmylocation.com\/&#8221;);\/\/page.navigate(&#8220;https:\/\/locations.kfc.com\/&#8221;);<\/div>\n<div class=\"tcb-plain-text\">\/\/page.click(&#8220;text = use my location&#8221;);<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">Thanks.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will learn about how to mock\/emulate geo locations using playwright java.\u00a0 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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3355","post","type-post","status-publish","format-standard","hentry","category-java","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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3355\/revisions"}],"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}]}}