Tuesday 18 April 2023

Playwright: filter the matching elements

Locator.filter method narrows existing locator according to the options.

 

Signature

Locator filter()
Locator filter(FilterOptions options)

 

Example

FilterOptions filterOptions = new FilterOptions();
filterOptions.setHasText("Java");
List<String> allTextContent = page.locator(".para").filter(filterOptions).allTextContents();

Above snippet get the text content of all the elments whose text contain the string "Java".

 

Find the below working application.

 

filterMatchedElements.html

<!DOCTYPE html>

<html>

<head>
	<title>Filter matching elements</title>
</head>

<body>

	<p class="para">Hi there</p>
	<p class="para">I love Java</p>
	<p class="para">Playwright Java library is available for open usage</p>

</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();
		}

	}
}

FilterElements.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;
import java.util.List;

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;
import static com.microsoft.playwright.Locator.FilterOptions;

public class FilterElements {
	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 + "filterMatchedElements.html");

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

			FilterOptions filterOptions = new FilterOptions();
			filterOptions.setHasText("Java");
			List<String> allTextContent = page.locator(".para").filter(filterOptions).allTextContents();
			for (String str : allTextContent) {
				System.out.println(str);
			}

		}
	}
}

Output

I love Java
Playwright Java library is available for open usage

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment