{"id":3215,"date":"2025-01-15T20:21:59","date_gmt":"2025-01-15T20:21:59","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3215"},"modified":"2025-01-17T10:36:54","modified_gmt":"2025-01-17T10:36:54","slug":"keypress-feature-and-introduction-to-api-testing-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/keypress-feature-and-introduction-to-api-testing-in-playwright-java\/","title":{"rendered":"Keypress feature and introduction to API testing in Playwright Java\u00a0\u00a0"},"content":{"rendered":"<p><em>In this blog we will be discovering the Keypress Functionality and will get started with API Testing in Playwright Java.<\/em><em>&nbsp;<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Keypress feature<\/li>\n<li>Automate GET Request for status code 200<\/li>\n<li>Automate GET Request for status code 404<\/li>\n<li>Source code (keypress)<\/li>\n<li>Source code (GET Request status code 200)<\/li>\n<li>Source code (GET Request status code 404)<\/li>\n<\/ul>\n<p><strong>Keypress feature<\/strong><\/p>\n<p>Go to&nbsp;<\/p>\n<p><a href=\"https:\/\/the-internet.herokuapp.com\/key_presses\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/the-internet.herokuapp.com\/key_presses<\/a>&nbsp; &nbsp;<\/p>\n<p>When we press any key, the name of the same key would become visible. Press \u2018Shift\u2019 key, notice that we get the message \u201cYou entered: SHIFT\u201d<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3218\" width=\"531\" data-init-width=\"1182\" height=\"141\" data-init-height=\"314\" title=\"Screenshot 2025-01-16 at 1.54.15\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.54.15\u202fAM.png\" data-width=\"531\" data-height=\"141\" style=\"aspect-ratio: auto 1182 \/ 314;\"><\/span><\/p>\n<p>&nbsp;Similarly, let us press \u2018Caps Lock\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3219\" width=\"364\" data-init-width=\"1182\" height=\"307\" data-init-height=\"998\" title=\"Screenshot 2025-01-16 at 1.55.31\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.55.31\u202fAM.png\" data-width=\"364\" data-height=\"307\" style=\"aspect-ratio: auto 1182 \/ 998;\"><\/span><\/p>\n<p>&nbsp;We will now explore the possibilities of automating this scenario.<\/p>\n<p>Inspect the text area, it is located using \u201cid\u201d&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3220\" width=\"402\" data-init-width=\"1182\" height=\"220\" data-init-height=\"648\" title=\"Screenshot 2025-01-16 at 1.56.18\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.56.18\u202fAM.png\" data-width=\"402\" data-height=\"220\" style=\"aspect-ratio: auto 1182 \/ 648;\"><\/span><\/p>\n<p>&nbsp;We can make use of <strong>\u2018press\u2019<\/strong> method to simulate pressing a key<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3221\" width=\"443\" data-init-width=\"1182\" height=\"180\" data-init-height=\"480\" title=\"Screenshot 2025-01-16 at 1.57.10\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.57.10\u202fAM.png\" data-width=\"443\" data-height=\"180\" style=\"aspect-ratio: auto 1182 \/ 480;\"><\/span><\/p>\n<p>&nbsp;By using the press method, we can simulate a variety of keys<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3222\" width=\"507\" data-init-width=\"1182\" height=\"638\" data-init-height=\"1488\" title=\"Screenshot 2025-01-16 at 1.57.50\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.57.50\u202fAM.png\" data-width=\"507\" data-height=\"638\" style=\"aspect-ratio: auto 1182 \/ 1488;\"><\/span><\/p>\n<p>&nbsp;n the same way, you can try other keys.<\/p>\n<p><strong>Automate GET Request for status code 200<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/reqres.in\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/reqres.in\/<\/a>&nbsp;<\/p>\n<p>Click GET request button displayed next to LIST USERS to retrieve the user list<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3223\" width=\"551\" data-init-width=\"1182\" height=\"139\" data-init-height=\"298\" title=\"Screenshot 2025-01-16 at 1.58.40\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.58.40\u202fAM.png\" data-width=\"551\" data-height=\"139\" style=\"aspect-ratio: auto 1182 \/ 298;\"><\/span><\/p>\n<p>&nbsp;Notice above response code 200 (200 meanins that the request was successfully processed by server).<\/p>\n<p>We will now automate to validate the response code 200.<\/p>\n<p>The url will be summation of \u201cbase url\u201d and \u201curi\u201d.<\/p>\n<p>base url = <a href=\"https:\/\/reqres.in\" target=\"_blank\" rel=\"noopener\">https:\/\/reqres.in<\/a><\/p>\n<p>request uri = \/api\/users?\/page=2<\/p>\n<p>The navigate statement than becomes:<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3224\" width=\"512\" data-init-width=\"1182\" height=\"253\" data-init-height=\"584\" title=\"Screenshot 2025-01-16 at 1.59.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.59.26\u202fAM.png\" data-width=\"512\" data-height=\"253\" style=\"aspect-ratio: auto 1182 \/ 584;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>200 response code is printed<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3225\" width=\"518\" data-init-width=\"1182\" height=\"408\" data-init-height=\"930\" title=\"Screenshot 2025-01-16 at 2.00.14\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.00.14\u202fAM.png\" data-width=\"518\" data-height=\"408\" style=\"aspect-ratio: auto 1182 \/ 930;\"><\/span><\/p>\n<p>&nbsp;<strong>Automate GET Request for status code 404<\/strong><\/p>\n<p>We will proceed to test the API below, which has a response code of 404<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3226\" width=\"509\" data-init-width=\"1182\" height=\"701\" data-init-height=\"1628\" title=\"Screenshot 2025-01-16 at 2.00.59\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.00.59\u202fAM.png\" data-width=\"509\" data-height=\"701\" style=\"aspect-ratio: auto 1182 \/ 1628;\"><\/span><\/p>\n<p>&nbsp;Also notice that the response body is blank<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3229\" width=\"520\" data-init-width=\"1182\" height=\"194\" data-init-height=\"442\" title=\"Screenshot 2025-01-16 at 2.02.37\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.02.37\u202fAM.png\" data-width=\"520\" data-height=\"194\" style=\"aspect-ratio: auto 1182 \/ 442;\"><\/span><\/p>\n<p>&nbsp;Testing Get APIs can be done using this approach.<\/p>\n<p><strong>Source code (keyress)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog13_keypress {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/key_presses?&#8221;);<\/p>\n<p>pg.press(&#8220;#target&#8221;,&#8221;F6&#8243;);<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (GET Request status code 200)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Response;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog13_StatusCode200 {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>Response resp = pg.navigate(&#8220;https:\/\/reqres.in\/api\/users?page=2&#8221;);<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(resp.status());<\/p>\n<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (GET Request status code 404)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Response;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog13_StatusCode404 {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>Response resp = pg.navigate(&#8220;https:\/\/reqres.in\/api\/unknown\/23&#8221;);<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(resp.status());<\/p>\n<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be discovering the Keypress Functionality and will get started with API Testing in Playwright Java.&nbsp; Topics that we will cover: Keypress feature Automate GET Request for status code 200 Automate GET Request for status code 404 Source code (keypress) Source code (GET Request status code 200) Source code (GET Request [&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-3215","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\/3215","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=3215"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3215\/revisions"}],"predecessor-version":[{"id":3711,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3215\/revisions\/3711"}],"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=3215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}