{"id":3643,"date":"2025-01-17T09:36:16","date_gmt":"2025-01-17T09:36:16","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3643"},"modified":"2025-01-17T11:00:20","modified_gmt":"2025-01-17T11:00:20","slug":"verify-page-title-text-not-present-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/verify-page-title-text-not-present-using-playwright-java\/","title":{"rendered":"Verify \u201cPage Title\u201d, \u201cText Not Present\u201d using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to assert \u201cpage title\u201d and \u201ctext not present on the webpage\u201d.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Assert page title (<em>assertTrue<\/em> method)<\/li>\n<li>Assert page title (<em>assertEquals<\/em> method)<\/li>\n<li>Assert page title (<em>If-else<\/em> loop)<\/li>\n<li>Validate that the text is not present on the page<\/li>\n<li>Source code (page title)<\/li>\n<li>Source code (text not present)<\/li>\n<\/ul>\n<p><strong>Assert page title (<\/strong><strong><em>assertTrue<\/em><\/strong><strong> method)<\/strong><\/p>\n<p>We will now assert and verify the title of the page. To do that, go to <a href=\"https:\/\/www.rahulshettyacademy.com\/lifetime-access\" style=\"outline: none;\">https:\/\/www.rahulshettyacademy.com\/lifetime-access<\/a>&nbsp;<\/p>\n<p>Mouse hover the page title. We will assert\/verify if the page title contains the text \u201cQA Automation Courses\u201d<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3646\" width=\"508\" data-init-width=\"1176\" height=\"395\" data-init-height=\"914\" title=\"Screenshot 2025-01-17 at 3.07.51\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.07.51\u202fPM.png\" data-width=\"508\" data-height=\"395\" style=\"aspect-ratio: auto 1176 \/ 914;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>Notice that the test passes<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3647\" width=\"530\" data-init-width=\"1176\" height=\"238\" data-init-height=\"528\" title=\"Screenshot 2025-01-17 at 3.08.31\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.08.31\u202fPM.png\" data-width=\"530\" data-height=\"238\" style=\"aspect-ratio: auto 1176 \/ 528;\"><\/span><\/p>\n<p>&nbsp;Let us introduce an error in the page title and execute. Notice that the assertion fails&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3648\" width=\"565\" data-init-width=\"1176\" height=\"634\" data-init-height=\"1320\" title=\"Screenshot 2025-01-17 at 3.09.19\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.09.19\u202fPM.png\" data-width=\"565\" data-height=\"634\" style=\"aspect-ratio: auto 1176 \/ 1320;\"><\/span><\/p>\n<p>&nbsp;When we give the entire title and re-execute, the assertion passes<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3649\" width=\"574\" data-init-width=\"1176\" height=\"253\" data-init-height=\"518\" title=\"Screenshot 2025-01-17 at 3.10.07\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.10.07\u202fPM.png\" data-width=\"574\" data-height=\"253\" style=\"aspect-ratio: auto 1176 \/ 518;\"><\/span><\/p>\n<p>&nbsp;<strong>Assert page title (<\/strong><strong><em>If-else<\/em><\/strong><strong> loop)<\/strong><\/p>\n<p>The page&#8217;s title can be validated by utilizing an if-else statement.&nbsp;<\/p>\n<p>Both the expected and actual titles are stored in the <em>title<\/em> and <em>expectedPageTitle<\/em> variables, respectively.&nbsp;<\/p>\n<p>By using <em>equalsIgnoreCase<\/em> in the if statement, we compare both variables. If they match, the code proceeds to the if body. However, if there is no match, the code executes the else body<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3650\" width=\"533\" data-init-width=\"1176\" height=\"304\" data-init-height=\"670\" title=\"Screenshot 2025-01-17 at 3.10.42\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.10.42\u202fPM.png\" data-width=\"533\" data-height=\"304\" style=\"aspect-ratio: auto 1176 \/ 670;\"><\/span><\/p>\n<p>&nbsp;Let us remove some text from the <em>expectedPageTitle<\/em> and re-execute.&nbsp;<\/p>\n<p>Notice that the code enters inside the \u201celse\u201d block this time<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3651\" width=\"499\" data-init-width=\"1176\" height=\"238\" data-init-height=\"562\" title=\"Screenshot 2025-01-17 at 3.11.14\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.11.14\u202fPM.png\" data-width=\"499\" data-height=\"238\" style=\"aspect-ratio: auto 1176 \/ 562;\"><\/span><\/p>\n<p>&nbsp;<strong>Validate that the text is not present on the page<\/strong><\/p>\n<p>To validate a text that is not available on a webpage using Playwright Java, we can utilize the <em>assertFalse<\/em> assertion.<\/p>\n<p>Imagine a scenario where the webpage of your site becomes flooded with unwanted texts. As a tester, it is our responsibility to ensure that the unwanted text no longer exists on that particular page.&nbsp;<\/p>\n<p>By utilizing the <em>assertFalse()<\/em> assertion and inputting the unwanted text, we can effectively verify and confirm that the texts are indeed absent<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3652\" width=\"552\" data-init-width=\"1176\" height=\"229\" data-init-height=\"488\" title=\"Screenshot 2025-01-17 at 3.11.51\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.11.51\u202fPM.png\" data-width=\"552\" data-height=\"229\" style=\"aspect-ratio: auto 1176 \/ 488;\"><\/span><\/p>\n<p>&nbsp;The reason <em>assertFalse()<\/em> assertion failed (in the above example) is because, the unwanted text \u201cBroken Link\u201d is present on the webpage<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3653\" width=\"510\" data-init-width=\"1176\" height=\"454\" data-init-height=\"1048\" title=\"Screenshot 2025-01-17 at 3.12.54\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.12.54\u202fPM.png\" data-width=\"510\" data-height=\"454\" style=\"aspect-ratio: auto 1176 \/ 1048;\"><\/span><\/p>\n<p>&nbsp;<strong>Source code (page title)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.testng.Assert;<\/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><strong>public<\/strong><strong>class<\/strong> Blog38_Verify_PageTitle {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<\/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.rahulshettyacademy.com\/lifetime-access&#8221;);<\/p>\n<p>String title = page.title();<\/p>\n<p>\/\/System.out.println(title);<\/p>\n<p>\/\/Assert.assertTrue(title.contains(&#8220;Q Automation Courses&#8221;), &#8220;Match&#8221;);<\/p>\n<p>\/\/Assert.assertEquals(title, &#8220;QA Automation Courses&#8221;);<\/p>\n<p>\/\/Assert.assertEquals(title, &#8220;Lifetime Access Subscription to 30+ QA Automation Courses | Rahul Shetty Academy&#8221;);<\/p>\n<p>String expectedPageTitle = &#8220;Lifetime Subscription to 30+ QA Automation Courses | Rahul Shetty Academy&#8221;;<\/p>\n<\/p>\n<p><strong>if<\/strong> (title.equalsIgnoreCase(expectedPageTitle)) {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Page title matches&#8221;);<\/p>\n<p>} <strong>else<\/strong> {<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Page title does not match&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/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 (text not present)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.testng.Assert;<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog38_Verify_TextNotPresent {<\/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 body = page.locator(&#8220;body&#8221;);<\/p>\n<p>String bodyText = body.textContent();<\/p>\n<p>\/\/Assert.assertFalse(bodyText.contains(&#8220;Broken Link&#8221;), &#8220;Broken Link not present on this page&#8221;);<\/p>\n<p>Assert.<em>assertFalse<\/em>(bodyText.contains(&#8220;junk&#8221;), &#8220;junk not present on this page&#8221;);<\/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>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to assert \u201cpage title\u201d and \u201ctext not present on the webpage\u201d. Topics that we will cover: Assert page title (assertTrue method) Assert page title (assertEquals method) Assert page title (If-else loop) Validate that the text is not present on the page Source code (page title) Source [&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-3643","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\/3643","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=3643"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3643\/revisions"}],"predecessor-version":[{"id":3763,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3643\/revisions\/3763"}],"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=3643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}