Monday, 10 April 2023

Playwright: allTextContents: all the text content values for all matching nodes

Locator.allTextContents() method returns a list of textContent values for all matching nodes.

 

allInnerTexts () vs allTextContents ()

allInnerTexts() method returns the text, without spacing and 'allTextContents' property returns the text along with spacing.

 

Find the below working application.

 

allTextContent.html

<!DOCTYPE html>

<html>

<head>
	<title>inner texts demo</title>
</head>

<body>
	<p class="oddPara"><b>Paragraph1</b>  starts          from here.</p>
	<p class="evenPara"><b>Paragraph2</b>  starts          from here.</p>
	<p class="oddPara"><b>Paragraph3</b>  starts          from here.</p>
	<p class="evenPara"><b>Paragraph4</b>  starts          from here.</p>
	<p class="oddPara"><b>Paragraph5</b>  starts          from here.</p>
	<p class="evenPara"><b>Paragraph6</b>  starts          from here.</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();
		}

	}
}

AllTextContext.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;

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

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

			System.out.println("allTextContents : ");
			List<String> allTextContents = page.locator(".oddPara").allTextContents();
			for (String text : allTextContents) {
				System.out.println(text);
			}

			System.out.println("\n\nallInnerTexts : ");
			List<String> allInnerTexts = page.locator(".oddPara").allInnerTexts();
			for (String text : allInnerTexts) {
				System.out.println(text);
			}
		}
	}
}

Output

allTextContents : 
Paragraph1  starts          from here.
Paragraph3  starts          from here.
Paragraph5  starts          from here.


allInnerTexts : 
Paragraph1 starts from here.
Paragraph3 starts from here.
Paragraph5 starts from here.

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment