{"id":3604,"date":"2025-01-17T09:16:10","date_gmt":"2025-01-17T09:16:10","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3604"},"modified":"2025-01-17T10:57:13","modified_gmt":"2025-01-17T10:57:13","slug":"usage-of-stream-has-text-and-mouse-right-click-methods-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/usage-of-stream-has-text-and-mouse-right-click-methods-using-playwright-java\/","title":{"rendered":"Usage of stream(), has-text() and mouse right click methods using Playwright Java\u00a0"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to handle mouse right click and will see the usage of steam() and has-text() methods.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Mouse \u2018Right click\u2019 operation<\/li>\n<li>Usage of <em>stream()<\/em> and <em>has-text()<\/em> methods<\/li>\n<li>Source code (mouse right click)<\/li>\n<li>Source code (<em>stream()<\/em> , <em>has-text()<\/em>)<\/li>\n<\/ul>\n<p><strong>Mouse \u2018Right click\u2019 operation<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/demo.automationtesting.in\/Windows.html\" target=\"_blank\" rel=\"noopener\">https:\/\/demo.automationtesting.in\/Windows.html<\/a>&nbsp;<\/p>\n<p>Our goal is to right click \u2018click\u2019 button so that the right click menu comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3607\" width=\"623\" data-init-width=\"1234\" height=\"191\" data-init-height=\"378\" title=\"Screenshot 2025-01-17 at 2.47.05\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.47.05\u202fPM.png\" data-width=\"623\" data-height=\"191\" style=\"aspect-ratio: auto 1234 \/ 378;\"><\/span><\/p>\n<p>Inspect \u2018click\u2019 button and copy the xpath<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3608\" width=\"564\" data-init-width=\"1234\" height=\"212\" data-init-height=\"464\" title=\"Screenshot 2025-01-17 at 2.48.52\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.48.52\u202fPM.png\" data-width=\"564\" data-height=\"212\" style=\"aspect-ratio: auto 1234 \/ 464;\"><\/span><\/p>\n<p>&nbsp;<em>\/\/*[@id=&#8221;Tabbed&#8221;]\/a\/button<\/em><\/p>\n<p>We can now use this xpath along with \u2018MouseButton.RIGHT\u2019 method as shown below&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3609\" width=\"586\" data-init-width=\"1234\" height=\"430\" data-init-height=\"906\" title=\"Screenshot 2025-01-17 at 2.49.24\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.49.24\u202fPM.png\" data-width=\"586\" data-height=\"430\" style=\"aspect-ratio: auto 1234 \/ 906;\"><\/span><\/p>\n<p>&nbsp;Execute and notice below that mouse right click operaion is successful and the right click menu comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3610\" width=\"574\" data-init-width=\"1234\" height=\"335\" data-init-height=\"720\" title=\"Screenshot 2025-01-17 at 2.50.08\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.50.08\u202fPM.png\" data-width=\"574\" data-height=\"335\" style=\"aspect-ratio: auto 1234 \/ 720;\"><\/span><\/p>\n<p>&nbsp;<strong>Usage of&nbsp;<\/strong><strong><em>stream()<\/em><\/strong><strong> and <\/strong><strong><em>has-text()<\/em><\/strong><strong> methods<\/strong><\/p>\n<p>Let us go to <a href=\"https:\/\/www.amazon.in\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/www.amazon.in\/<\/a>&nbsp;<\/p>\n<p>Our task is to extract all the links from the page that have the text \u2018Amazon\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3611\" width=\"600\" data-init-width=\"1234\" height=\"191\" data-init-height=\"392\" title=\"Screenshot 2025-01-17 at 2.50.43\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.50.43\u202fPM.png\" data-width=\"600\" data-height=\"191\" style=\"aspect-ratio: auto 1234 \/ 392;\"><\/span><\/p>\n<p>&nbsp;Now link is represented by \u2018a\u2019 tag. Now there is a \u2018has-text()\u2019 method that we can use to extract all the links having text \u2018Amazon\u2019, as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3612\" width=\"570\" data-init-width=\"1234\" height=\"418\" data-init-height=\"904\" title=\"Screenshot 2025-01-17 at 2.51.14\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.51.14\u202fPM.png\" data-width=\"570\" data-height=\"418\" style=\"aspect-ratio: auto 1234 \/ 904;\"><\/span><\/p>\n<p>&nbsp;So our code logic would like as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3613\" width=\"557\" data-init-width=\"1234\" height=\"230\" data-init-height=\"510\" title=\"Screenshot 2025-01-17 at 2.51.49\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.51.49\u202fPM.png\" data-width=\"557\" data-height=\"230\" style=\"aspect-ratio: auto 1234 \/ 510;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>Notice below that all the link texts are printed that have the text \u2018Amazon\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3614\" width=\"411\" data-init-width=\"832\" height=\"475\" data-init-height=\"962\" title=\"Screenshot 2025-01-17 at 2.52.20\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.52.20\u202fPM.png\" data-width=\"411\" data-height=\"475\" style=\"aspect-ratio: auto 832 \/ 962;\"><\/span><\/p>\n<p>&nbsp;So this is how we can use <em>has-text()<\/em> alongwith <em>stream() method.<\/em><\/p>\n<p><strong>Source code (mouse right click)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.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.Locator;<\/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.options.MouseButton;<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog35_RightClick_PWJava {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>page = browser.newPage();<\/p>\n<p>}<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> test_1() {<\/p>\n<\/p>\n<p>page.navigate(&#8220;https:\/\/demo.automationtesting.in\/Windows.html&#8221;);<\/p>\n<p>page.locator(&#8220;\/\/*[@id=&#8221;Tabbed&#8221;]\/a\/button&#8221;).click(<strong>new<\/strong> Locator.ClickOptions().setButton(MouseButton.<strong><em>RIGHT<\/em><\/strong>));<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(5000);<\/p>\n<p>} <strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> tearDown() {<\/p>\n<p>page.pause();<\/p>\n<p>browser.close();<\/p>\n<p>page.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (<\/strong><strong><em>stream()<\/em><\/strong><strong> , <\/strong><strong><em>has-text()<\/em><\/strong><strong>)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> java.util.List;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.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.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog35_SteamHasText_PWJava {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>page = browser.newPage();<\/p>\n<p>}<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> test_1() {<\/p>\n<p>page.navigate(&#8220;https:\/\/www.amazon.in\/&#8221;);<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(5000);<\/p>\n<p>} <strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>List&lt;String&gt; linksListAmaz = page.locator(&#8220;a:has-text(&#8216;Amazon&#8217;)&#8221;).allInnerTexts();<\/p>\n<p>linksListAmaz.stream().forEach(l -&gt; System.<strong><em>out<\/em><\/strong>.println(l));<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> tearDown() {<\/p>\n<p>page.pause();<\/p>\n<p>browser.close();<\/p>\n<p>page.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to handle mouse right click and will see the usage of steam() and has-text() methods. Topics that we will cover: Mouse \u2018Right click\u2019 operation Usage of stream() and has-text() methods Source code (mouse right click) Source code (stream() , has-text()) Mouse \u2018Right click\u2019 operation Go to [&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-3604","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\/3604","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=3604"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3604\/revisions"}],"predecessor-version":[{"id":3757,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3604\/revisions\/3757"}],"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=3604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}