Monday 22 May 2023

Playwright: Select or highlight the text content

Locator.selectText() method select all the text content attached to element mapped to this selector.

 

Signature

void selectText()
void selectText(SelectTextOptions options)

 

Example

page.locator("#h1").selectText();

 

Find the below working application.

 

selectText.html
<!DOCTYPE html>
<html>

<body>

	<h1 id ="h1">Highlight matching content demo</h1>

	<p> The Java Programming Language solves all the above problems. The Java programming language platform
		provides a <span id="features">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="otherLangs"> 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();
		}

	}
}

 

SelectTextDemo.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 SelectTextDemo {
	public static void main(String[] args) throws IOException, InterruptedException {
		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 + "selectText.html");

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

			page.locator("#h1").selectText();
			String filePath = "/Users/Shared/playwright/screenShot1.png";
			page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)));

			page.locator("#features").selectText();
			filePath = "/Users/Shared/playwright/screenShot2.png";
			page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)));
		}
	}
}

 

Run above application, you can get below screen shots.

 

screenShot1.png

 

 


screenShot2.png

 



 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment