In this blog we will be utilizing Playwright Java to assert “page title” and “text not present on the webpage”.

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 code (text not present)

Assert page title (assertTrue method)

We will now assert and verify the title of the page. To do that, go to https://www.rahulshettyacademy.com/lifetime-access 

Mouse hover the page title. We will assert/verify if the page title contains the text “QA Automation Courses”

 Execute. 

Notice that the test passes

 Let us introduce an error in the page title and execute. Notice that the assertion fails 

 When we give the entire title and re-execute, the assertion passes

 Assert page title (If-else loop)

The page's title can be validated by utilizing an if-else statement. 

Both the expected and actual titles are stored in the title and expectedPageTitle variables, respectively. 

By using equalsIgnoreCase 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

 Let us remove some text from the expectedPageTitle and re-execute. 

Notice that the code enters inside the “else” block this time

 Validate that the text is not present on the page

To validate a text that is not available on a webpage using Playwright Java, we can utilize the assertFalse assertion.

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. 

By utilizing the assertFalse() assertion and inputting the unwanted text, we can effectively verify and confirm that the texts are indeed absent

 The reason assertFalse() assertion failed (in the above example) is because, the unwanted text “Broken Link” is present on the webpage

 Source code (page title)

package com.rsa.playwrightjava;


import org.testng.Assert;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

public class Blog38_Verify_PageTitle {

private Browser browser;

private Page page;


@BeforeMethod

public void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public void test_1() {

page.navigate("https://www.rahulshettyacademy.com/lifetime-access");

String title = page.title();

//System.out.println(title);

//Assert.assertTrue(title.contains("Q Automation Courses"), "Match");

//Assert.assertEquals(title, "QA Automation Courses");

//Assert.assertEquals(title, "Lifetime Access Subscription to 30+ QA Automation Courses | Rahul Shetty Academy");

String expectedPageTitle = "Lifetime Subscription to 30+ QA Automation Courses | Rahul Shetty Academy";


if (title.equalsIgnoreCase(expectedPageTitle)) {

System.out.println("Page title matches");

} else {

System.out.println("Page title does not match");

}

}


@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Source code (text not present)

package com.rsa.playwrightjava;


import org.testng.Assert;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Locator;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog38_Verify_TextNotPresent {

private Browser browser;

private Page page;

@BeforeMethod

public void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public void test_1() {

page.navigate("https://rahulshettyacademy.com/AutomationPractice/");

Locator body = page.locator("body");

String bodyText = body.textContent();

//Assert.assertFalse(bodyText.contains("Broken Link"), "Broken Link not present on this page");

Assert.assertFalse(bodyText.contains("junk"), "junk not present on this page");

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Thanks.


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}