Friday, 2 June 2023

Playwright: get the locator by label text

Using 'page.getByLabel()' method, we can interact with an element using label text.

 

For example, we can use this method to fill the text field data, to locate a radio button by associated label text etc.,

 

Example

page.getByLabel("Java").click();

 

Above snippet click on the element associated with given label. For example, if the label is associated with the radio button, the it select it.

 

Find the below working application.

 

labelDemo.html

<!DOCTYPE html>
<html>

<body>

	<h1>User favorite language form</h1>

	<p>Select one of your favorite language and click on submit button</p>

	<form onsubmit="return false;">
		  <input type="radio" id="java" name="my_fav_language" value="Java">
		  <label for="java">Java</label><br>

		  <input type="radio" id="go" name="my_fav_language" value="Go">
		  <label for="go">Go</label><br>

		  <input type="radio" id="python" name="my_fav_language" value="Python">
		  <label for="python">Python</label><br><br>

		<input type="submit" value="submit" onclick="display()">
	</form>

	<p id="result"></p>

	<script>
		function display() {
			let result = document.getElementById("result");
			let selectedValue = document.querySelector('input[name="my_fav_language"]:checked').value;
			result.innerHTML = `<b>Your favourite language is :  ${selectedValue}</b>`
		}
	</script>

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

	}
}

GetByLabel.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;

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 GetByLabel {

	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 + "labelDemo.html");

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

			page.getByLabel("Java").click();
			page.locator("input[type=\"submit\"]").click();

			String textContent = page.locator("id=result").textContent();
			System.out.println(textContent);
		}
	}

}

Output

Your favourite language is :  Java


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment