Tuesday 2 May 2023

Playwright: Highlight the matching elements

Locator.highlight() method highlight all the matching elements on the screen.

 

Example

page.locator(".impt").highlight();

 

Find the below working application.

 

highlightDemo.html
<!DOCTYPE html>
<html>

<body>

	<h1>Highlight matching content demo</h1>

	<p> The Java Programming Language solves all the above problems. The Java programming language platform
		provides a <span class="impt">portable, interpreted, high-performance, simple, object-oriented programming</span> language and supporting
		run-time environment.
		
		<br /><br />

		Java Design and architecture decisions drew from a variety of languages such as  <span class="impt"> Eiffel, SmallTalk, Objective C,
		and Cedar/Mesa</span>. Closely observed the problems in the other languages like platform dependent, Pointers
		complexity, Manual garbage de allocation etc., Java removes the basic problems in other languages.
	</p>


</body>

</html>

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {

	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

HighlightMatchingElements.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class HighlightMatchingElements {
	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {

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

			final String content = FileUtil.resourceAsString("locators" + File.separator + "highlightDemo.html");

			Page page = browser.newPage();
			page.setContent(content);

			page.locator(".impt").highlight();
			String filePath = "/Users/Shared/playwright/demo.png";
			page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)));
		}
	}
}

Above snippet generates below screenshot.




Previous                                                 Next                                                 Home

No comments:

Post a Comment