In this blog we will be utilizing Playwright Java to verify “current versus expected url” and verify “placeholder text”.

Topics that we will cover:

  • Verify “current versus expected url”
  • Verify “placeholder text“
  • Source code (current versus expected url)
  • Source code (placeholder text)

Verify “current versus expected url”

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 https://rahulshettyacademy.com/AutomationPractice/ 

Let us call this as our “current url”

    Now, on the top right hand side, we can see a link “Free Access to InterviewQues/ResumeAssistance/Material” 

Click this link. You will be re-directed to a different url https://rahulshettyacademy.com/documents-request 

Let us consider this as our “expected url”.

In line#26 and 30, we define our “current” and “expected” urls.

Lines#32-36 will perform the verification. In line#32, we are saying that “if current and expected urls donot match, than the verification passes”

 Execute. 

Notice the console output

 Click ‘Home’ link. You will still be on the same page https://rahulshettyacademy.com/ 

Inspect ‘Home’

 In present case, the current and expected urls are same. We can now perform the verification

Verify “placeholder text“

Go to https://rahulshettyacademy.com/AutomationPractice/ 

You can see the default placeholder text “Type to Select Countries”

 Now how do we verify this placeholder text?

We can use the “getAttribute()” method tofecth the placeholder text (see line#28).

Let us intentionally make a mistake in the placeholder text (line#30)

 Let us now correct the text and re-execute. The verification passes this time

 

Source code (current versus expected url)

package com.rsa.playwrightjava;


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 Blog36_Verify_CurrentURL_ExpectedURL {

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/");

String currentUrl = page.url();

// System.out.println(currentUrl);

page.locator("a:has-text('Free Access')").click();

String expectedUrl = "https://rahulshettyacademy.com/documents-request";

if (!currentUrl.equals(expectedUrl)) {

System.out.println("Verification passed. The current and expected url does not match");

} else {

System.out.println("Verification failed. The current and expected url match. Expected url --> " + expectedUrl + ", current url--> " + currentUrl);

}

}

@Test(enabled=false)

public void test_2() {

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

String currentUrl = page.url();

// System.out.println(currentUrl);

page.locator("//div[@class='nav-outer clearfix']//a[normalize-space()='Home']").click();

String expectedUrl = "https://www.rahulshettyacademy.com/";

if (currentUrl.equals(expectedUrl)) {

System.out.println("Verification passed. The current and expected url match");

} else {

System.out.println("Verification failed. The current and expected url do not match. Expected the url --> " + expectedUrl + ", but got the url--> " + currentUrl);

}

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Source code (placeholder text)

package com.rsa.playwrightjava;


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 Blog36_Verify_Placeholder_Text {

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 searchBar = page.locator("#autocomplete");

String placeHolderText = searchBar.getAttribute("placeholder");


if (placeHolderText.contains("Type to Select Coutries")) {


System.out.println("Verification success");


} else {

System.out.println("Verification failed. There is so such placeholder text");

}

}

@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"}