{"id":3215,"date":"2025-01-15T20:21:59","date_gmt":"2025-01-15T20:21:59","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/keypress-feature-and-introduction-to-api-testing-in-playwright-java\/"},"modified":"2025-01-15T20:21:59","modified_gmt":"2025-01-15T20:21:59","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"},"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=\"3215\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be discovering the Keypress Functionality and will get started with API Testing in Playwright Java.<\/em><em>\u00a0<\/em><\/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>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\u00a0<\/p>\n<p><a class=\"\" href=\"https:\/\/the-internet.herokuapp.com\/key_presses\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/key_presses<\/a>\u00a0 \u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba52395\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3218\" data-css=\"tve-u-1946ba53966\" data-height=\"141\" data-id=\"3218\" data-init-height=\"314\" data-init-width=\"1182\" data-width=\"531\" decoding=\"async\" height=\"141\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.54.15\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 314;\" title=\"Screenshot 2025-01-16 at 1.54.15\u202fAM\" width=\"531\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Similarly, let us press \u2018Caps Lock\u2019<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba5d6c1\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3219\" data-css=\"tve-u-1946ba5ee39\" data-height=\"307\" data-id=\"3219\" data-init-height=\"998\" data-init-width=\"1182\" data-width=\"364\" decoding=\"async\" height=\"307\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.55.31\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 998;\" title=\"Screenshot 2025-01-16 at 1.55.31\u202fAM\" width=\"364\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We will now explore the possibilities of automating this scenario.<\/p>\n<p>Inspect the text area, it is located using \u201cid\u201d\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba6a78b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3220\" data-css=\"tve-u-1946ba6b868\" data-height=\"220\" data-id=\"3220\" data-init-height=\"648\" data-init-width=\"1182\" data-width=\"402\" decoding=\"async\" height=\"220\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.56.18\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 648;\" title=\"Screenshot 2025-01-16 at 1.56.18\u202fAM\" width=\"402\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We can make use of <strong>\u2018press\u2019<\/strong> method to simulate pressing a key<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba7230f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3221\" data-css=\"tve-u-1946ba747a8\" data-height=\"180\" data-id=\"3221\" data-init-height=\"480\" data-init-width=\"1182\" data-width=\"443\" decoding=\"async\" height=\"180\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.57.10\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 480;\" title=\"Screenshot 2025-01-16 at 1.57.10\u202fAM\" width=\"443\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0By using the press method, we can simulate a variety of keys<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba7ec4d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3222\" data-css=\"tve-u-1946ba80c26\" data-height=\"638\" data-id=\"3222\" data-init-height=\"1488\" data-init-width=\"1182\" data-width=\"507\" decoding=\"async\" height=\"638\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.57.50\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 1488;\" title=\"Screenshot 2025-01-16 at 1.57.50\u202fAM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0n 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 class=\"\" href=\"https:\/\/reqres.in\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/reqres.in\/<\/a>\u00a0<\/p>\n<p>Click GET request button displayed next to LIST USERS to retrieve the user list<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba88278\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3223\" data-css=\"tve-u-1946ba892bc\" data-height=\"139\" data-id=\"3223\" data-init-height=\"298\" data-init-width=\"1182\" data-width=\"551\" decoding=\"async\" height=\"139\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.58.40\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 298;\" title=\"Screenshot 2025-01-16 at 1.58.40\u202fAM\" width=\"551\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Notice 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\" rel=\"noopener\" target=\"_blank\">https:\/\/reqres.in<\/a><\/p>\n<p>request uri = \/api\/users?\/page=2<\/p>\n<p>The navigate statement than becomes:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946ba94ce8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3224\" data-css=\"tve-u-1946ba96244\" data-height=\"253\" data-id=\"3224\" data-init-height=\"584\" data-init-width=\"1182\" data-width=\"512\" decoding=\"async\" height=\"253\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.59.26\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 584;\" title=\"Screenshot 2025-01-16 at 1.59.26\u202fAM\" width=\"512\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>200 response code is printed<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946baa01b8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3225\" data-css=\"tve-u-1946baa15d5\" data-height=\"408\" data-id=\"3225\" data-init-height=\"930\" data-init-width=\"1182\" data-width=\"518\" decoding=\"async\" height=\"408\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.00.14\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 930;\" title=\"Screenshot 2025-01-16 at 2.00.14\u202fAM\" width=\"518\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946baad32c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3226\" data-css=\"tve-u-1946baae54b\" data-height=\"701\" data-id=\"3226\" data-init-height=\"1628\" data-init-width=\"1182\" data-width=\"509\" decoding=\"async\" height=\"701\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.00.59\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 1628;\" title=\"Screenshot 2025-01-16 at 2.00.59\u202fAM\" width=\"509\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Also notice that the response body is blank<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946bac5da1\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3229\" data-css=\"tve-u-1946bac7143\" data-height=\"194\" data-id=\"3229\" data-init-height=\"442\" data-init-width=\"1182\" data-width=\"520\" decoding=\"async\" height=\"194\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-2.02.37\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 442;\" title=\"Screenshot 2025-01-16 at 2.02.37\u202fAM\" width=\"520\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Testing 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><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog13_keypress {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<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><\/p>\n<p><strong>Source code (GET Request status code 200)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog13_StatusCode200 {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<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><\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (GET Request status code 404)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog13_StatusCode404 {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<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><\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u00a0<\/p>\n<p>Thanks.<\/p>\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 be discovering the Keypress Functionality and will get started with API Testing in Playwright Java.\u00a0 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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3215","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\/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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3215\/revisions"}],"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}]}}