Sunday 18 June 2023

Playwright: get the locator by a selector

 Playwright.locator() method return the locator by a selector.

 

Signature

Locator locator(String selector)
Locator locator(String selector, LocatorOptions options)

Example

Locator locatorByClass = page.locator(".para");
Locator locatorById = page.locator("#para1");
Locator locatorByAttribute = page.locator("p[type=\"text\"]");
Locator locatorByXPath = page.locator("//title");

Find the below working application.

 

locatorBySelector.html

<!DOCTYPE html>

<html>

<head>
	<title>Locator by selector demo</title>
</head>

<body>

	<p class="para" id="para1">Paragraph1</p>
	<p class="para" type="text">Paragraph2</p>
	<p class="para">Paragraph3</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();
		}

	}
}

GetTheLocatorBySelector.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.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

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

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

			Locator locatorByClass = page.locator(".para");
			Locator locatorById = page.locator("#para1");
			Locator locatorByAttribute = page.locator("p[type=\"text\"]");
			Locator locatorByXPath = page.locator("//title");

			System.out.println("Content matched to locatorByClass : ");
			List<String> allTextContent = locatorByClass.allTextContents();
			for (String str : allTextContent) {
				System.out.println(str);
			}

			System.out.println("\nlocatorById : " + locatorById.textContent());
			System.out.println("\nlocatorByAttribute : " + locatorByAttribute.textContent());
			System.out.println("\nlocatorByXPath : " + locatorByXPath.textContent());

		}
	}
}

Output

Content matched to locatorByClass : 
Paragraph1
Paragraph2
Paragraph3

locatorById : Paragraph1

locatorByAttribute : Paragraph2

locatorByXPath : Locator by selector demo


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment