{"id":3617,"date":"2025-01-17T09:23:30","date_gmt":"2025-01-17T09:23:30","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/verify-current-versus-expected-url-verify-placeholder-text-using-playwright-java\/"},"modified":"2025-01-17T09:23:30","modified_gmt":"2025-01-17T09:23:30","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><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=\"3617\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<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<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>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 class=\"\" href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>\u00a0<\/p>\n<p>Let us call this as our \u201ccurrent url\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473954165\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3620\" data-css=\"tve-u-194739556b0\" data-height=\"227\" data-id=\"3620\" data-init-height=\"468\" data-init-width=\"1176\" data-width=\"571\" decoding=\"async\" height=\"227\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.54.06\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 468;\" title=\"Screenshot 2025-01-17 at 2.54.06\u202fPM\" width=\"571\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 \u00a0 Now, on the top right hand side, we can see a link \u201c<em>Free Access to InterviewQues\/ResumeAssistance\/Material<\/em>\u201d\u00a0<\/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>\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473961353\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3622\" data-css=\"tve-u-194739620e1\" data-height=\"321\" data-id=\"3622\" data-init-height=\"698\" data-init-width=\"1176\" data-width=\"541\" decoding=\"async\" height=\"321\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.55.12\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 698;\" title=\"Screenshot 2025-01-17 at 2.55.12\u202fPM\" width=\"541\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.\u00a0<\/p>\n<p>Notice the console output<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947396ad7a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3623\" data-css=\"tve-u-1947396bffc\" data-height=\"404\" data-id=\"3623\" data-init-height=\"814\" data-init-width=\"1176\" data-width=\"583\" decoding=\"async\" height=\"404\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.55.58\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 814;\" title=\"Screenshot 2025-01-17 at 2.55.58\u202fPM\" width=\"583\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Click \u2018Home\u2019 link. You will still be on the same page <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/<\/a>\u00a0<\/p>\n<p>Inspect \u2018Home\u2019<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473971dc9\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3624\" data-css=\"tve-u-19473972c9c\" data-height=\"273\" data-id=\"3624\" data-init-height=\"570\" data-init-width=\"1176\" data-width=\"564\" decoding=\"async\" height=\"273\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.56.29\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 570;\" title=\"Screenshot 2025-01-17 at 2.56.29\u202fPM\" width=\"564\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In present case, the current and expected urls are same. We can now perform the verification<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947397a94d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3625\" data-css=\"tve-u-1947397b9a4\" data-height=\"563\" data-id=\"3625\" data-init-height=\"1238\" data-init-width=\"1176\" data-width=\"535\" decoding=\"async\" height=\"563\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.56.59\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 1238;\" title=\"Screenshot 2025-01-17 at 2.56.59\u202fPM\" width=\"535\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Verify \u201cplaceholder text\u201c<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>\u00a0<\/p>\n<p>You can see the default placeholder text \u201cType to Select Countries\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19473981505\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3626\" data-css=\"tve-u-19473982698\" data-height=\"123\" data-id=\"3626\" data-init-height=\"274\" data-init-width=\"1176\" data-width=\"529\" decoding=\"async\" height=\"123\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.57.33\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 274;\" title=\"Screenshot 2025-01-17 at 2.57.33\u202fPM\" width=\"529\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Now 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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947398bd32\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3627\" data-css=\"tve-u-1947398cbb2\" data-height=\"365\" data-id=\"3627\" data-init-height=\"834\" data-init-width=\"1176\" data-width=\"514\" decoding=\"async\" height=\"365\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.58.05\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 834;\" title=\"Screenshot 2025-01-17 at 2.58.05\u202fPM\" width=\"514\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us now correct the text and re-execute. The verification passes this time<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739925cb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3628\" data-css=\"tve-u-19473993b4a\" data-height=\"233\" data-id=\"3628\" data-init-height=\"454\" data-init-width=\"1176\" data-width=\"603\" decoding=\"async\" height=\"233\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.58.43\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 454;\" title=\"Screenshot 2025-01-17 at 2.58.43\u202fPM\" width=\"603\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p><strong>Source code (current versus expected url)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<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><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/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><br \/>\n<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><br \/>\n<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><br \/>\n<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><\/p>\n<p><strong>Source code (placeholder text)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><br \/>\n<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><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/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><br \/>\n<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><\/p>\n<p><strong>if<\/strong> (placeHolderText.contains(&#8220;Type to Select Coutries&#8221;)) {<\/p>\n<p><\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Verification success&#8221;);<\/p>\n<p><\/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><br \/>\n<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<\/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 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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3617","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\/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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3617\/revisions"}],"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}]}}