{"id":3617,"date":"2025-01-17T09:23:30","date_gmt":"2025-01-17T09:23:30","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3617"},"modified":"2025-01-17T10:58:26","modified_gmt":"2025-01-17T10:58:26","slug":"verify-current-versus-expected-url-verify-placeholder-text-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/verify-current-versus-expected-url-verify-placeholder-text-using-playwright-java\/","title":{"rendered":"Verify \u201cCurrent versus Expected Url\u201d, Verify \u201cPlaceholder Text\u201d using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to verify \u201ccurrent versus expected url\u201d and verify \u201cplaceholder text\u201d.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Verify \u201ccurrent versus expected url\u201d<\/li>\n<li>Verify \u201cplaceholder text\u201c<\/li>\n<li>Source code (current versus expected url)<\/li>\n<li>Source code (placeholder text)<\/li>\n<\/ul>\n<p><strong>Verify \u201ccurrent versus expected url\u201d<\/strong><\/p>\n<p>In a test case, we might need to verify whether the curent url of the page matches with the expected url. For example, let us launch the url <a href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>&nbsp;<\/p>\n<p>Let us call this as our \u201ccurrent url\u201d<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3620\" width=\"571\" data-init-width=\"1176\" height=\"227\" data-init-height=\"468\" title=\"Screenshot 2025-01-17 at 2.54.06\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.54.06\u202fPM.png\" data-width=\"571\" data-height=\"227\" style=\"aspect-ratio: auto 1176 \/ 468;\"><\/span><\/p>\n<p>&nbsp; &nbsp; Now, on the top right hand side, we can see a link \u201c<em>Free Access to InterviewQues\/ResumeAssistance\/Material<\/em>\u201d&nbsp;<\/p>\n<p>Click this link. You will be re-directed to a different url <a href=\"https:\/\/rahulshettyacademy.com\/documents-request\">https:\/\/rahulshettyacademy.com\/documents-request<\/a>&nbsp;<\/p>\n<p>Let us consider this as our \u201cexpected url\u201d.<\/p>\n<p>In line#26 and 30, we define our \u201ccurrent\u201d and \u201cexpected\u201d urls.<\/p>\n<p>Lines#32-36 will perform the verification. In line#32, we are saying that \u201cif current and expected urls donot match, than the verification passes\u201d<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3622\" width=\"541\" data-init-width=\"1176\" height=\"321\" data-init-height=\"698\" title=\"Screenshot 2025-01-17 at 2.55.12\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.55.12\u202fPM.png\" data-width=\"541\" data-height=\"321\" style=\"aspect-ratio: auto 1176 \/ 698;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>Notice the console output<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3623\" width=\"583\" data-init-width=\"1176\" height=\"404\" data-init-height=\"814\" title=\"Screenshot 2025-01-17 at 2.55.58\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.55.58\u202fPM.png\" data-width=\"583\" data-height=\"404\" style=\"aspect-ratio: auto 1176 \/ 814;\"><\/span><\/p>\n<p>&nbsp;Click \u2018Home\u2019 link. You will still be on the same page <a href=\"https:\/\/rahulshettyacademy.com\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/<\/a>&nbsp;<\/p>\n<p>Inspect \u2018Home\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3624\" width=\"564\" data-init-width=\"1176\" height=\"273\" data-init-height=\"570\" title=\"Screenshot 2025-01-17 at 2.56.29\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.56.29\u202fPM.png\" data-width=\"564\" data-height=\"273\" style=\"aspect-ratio: auto 1176 \/ 570;\"><\/span><\/p>\n<p>&nbsp;In present case, the current and expected urls are same. We can now perform the verification<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3625\" width=\"535\" data-init-width=\"1176\" height=\"563\" data-init-height=\"1238\" title=\"Screenshot 2025-01-17 at 2.56.59\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.56.59\u202fPM.png\" data-width=\"535\" data-height=\"563\" style=\"aspect-ratio: auto 1176 \/ 1238;\"><\/span><\/p>\n<p><strong>Verify \u201cplaceholder text\u201c<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>&nbsp;<\/p>\n<p>You can see the default placeholder text \u201cType to Select Countries\u201d<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3626\" width=\"529\" data-init-width=\"1176\" height=\"123\" data-init-height=\"274\" title=\"Screenshot 2025-01-17 at 2.57.33\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.57.33\u202fPM.png\" data-width=\"529\" data-height=\"123\" style=\"aspect-ratio: auto 1176 \/ 274;\"><\/span><\/p>\n<p>&nbsp;Now how do we verify this placeholder text?<\/p>\n<p>We can use the \u201c<em>getAttribute()<\/em>\u201d method tofecth the placeholder text (see line#28).<\/p>\n<p>Let us intentionally make a mistake in the placeholder text (line#30)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3627\" width=\"514\" data-init-width=\"1176\" height=\"365\" data-init-height=\"834\" title=\"Screenshot 2025-01-17 at 2.58.05\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.58.05\u202fPM.png\" data-width=\"514\" data-height=\"365\" style=\"aspect-ratio: auto 1176 \/ 834;\"><\/span><\/p>\n<p>&nbsp;Let us now correct the text and re-execute. The verification passes this time<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3628\" width=\"603\" data-init-width=\"1176\" height=\"233\" data-init-height=\"454\" title=\"Screenshot 2025-01-17 at 2.58.43\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.58.43\u202fPM.png\" data-width=\"603\" data-height=\"233\" style=\"aspect-ratio: auto 1176 \/ 454;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Source code (current versus expected url)<\/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.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog36_Verify_CurrentURL_ExpectedURL {<\/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:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;);<\/p>\n<p>String currentUrl = page.url();<\/p>\n<p>\/\/ System.out.println(currentUrl);<\/p>\n<p>page.locator(&#8220;a:has-text(&#8216;Free Access&#8217;)&#8221;).click();<\/p>\n<p>String expectedUrl = &#8220;https:\/\/rahulshettyacademy.com\/documents-request&#8221;;<\/p>\n<p><strong>if<\/strong> (!currentUrl.equals(expectedUrl)) {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification passed. The current and expected url does not match&#8221;);<\/p>\n<p>} <strong>else<\/strong> {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification failed. The current and expected url match. Expected url &#8211;&gt; &#8221; + expectedUrl + &#8220;, current url&#8211;&gt; &#8221; + currentUrl);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>@Test(enabled=<strong>false<\/strong>)<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> test_2() {<\/p>\n<p>page.navigate(&#8220;https:\/\/www.rahulshettyacademy.com\/&#8221;);<\/p>\n<p>String currentUrl = page.url();<\/p>\n<p>\/\/ System.out.println(currentUrl);<\/p>\n<p>page.locator(&#8220;\/\/div[@class=&#8217;nav-outer clearfix&#8217;]\/\/a[normalize-space()=&#8217;Home&#8217;]&#8221;).click();<\/p>\n<p>String expectedUrl = &#8220;https:\/\/www.rahulshettyacademy.com\/&#8221;;<\/p>\n<p><strong>if<\/strong> (currentUrl.equals(expectedUrl)) {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification passed. The current and expected url match&#8221;);<\/p>\n<p>} <strong>else<\/strong> {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification failed. The current and expected url do not match. Expected the url &#8211;&gt; &#8221; + expectedUrl + &#8220;, but got the url&#8211;&gt; &#8221; + currentUrl);<\/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 (placeholder text)<\/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>public<\/strong><strong>class<\/strong> Blog36_Verify_Placeholder_Text {<\/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:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;);<\/p>\n<p>Locator searchBar = page.locator(&#8220;#autocomplete&#8221;);<\/p>\n<p>String placeHolderText = searchBar.getAttribute(&#8220;placeholder&#8221;);<\/p>\n<\/p>\n<p><strong>if<\/strong> (placeHolderText.contains(&#8220;Type to Select Coutries&#8221;)) {<\/p>\n<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification success&#8221;);<\/p>\n<\/p>\n<p>} <strong>else<\/strong> {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification failed. There is so such placeholder text&#8221;);<\/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>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to verify \u201ccurrent versus expected url\u201d and verify \u201cplaceholder text\u201d. Topics that we will cover: Verify \u201ccurrent versus expected url\u201d Verify \u201cplaceholder text\u201c Source code (current versus expected url) Source code (placeholder text) Verify \u201ccurrent versus expected url\u201d In a test case, we might need 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-3617","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\/3617","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=3617"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3617\/revisions"}],"predecessor-version":[{"id":3759,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3617\/revisions\/3759"}],"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=3617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}