Saturday 13 May 2023

Playwright: check whether the element is editable or not

Locator.isEditable() method return true when the element is editable, else false.

 

Signature

boolean isEditable()
boolean isEditable(IsEditableOptions options)

Example

boolean isNameDisabled = page.locator("#name").isDisabled();

Find the below working application.

 

readOnlyCheck.html

<!DOCTYPE html>
<html>

<body>

	<h1>disabled attribute example</h1>

	<form>
		<label for="name">Name:</label>
		<input type="text" id="name" name="name"><br><br>

		<label for="age">age:</label>
		<input type="text" id="age" name="age" disabled><br><br>

		<label for="country">Country:</label>
		<input type="text" id="country" name="country" value="India" readonly><br><br>

		<input type="submit" value="Submit">
	</form>

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

	}
}

EditableCheckDemo.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 EditableCheckDemo {
	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 + "readOnlyCheck.html");

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

			boolean isNameEditable = page.locator("#name").isEditable();
			boolean isAgeEditable = page.locator("#age").isEditable();
			boolean isCountryEditable = page.locator("#country").isEditable();

			System.out.println("isNameEditable : " + isNameEditable);
			System.out.println("isAgeEditable : " + isAgeEditable);
			System.out.println("isCountryEditable : " + isCountryEditable);

		}
	}
}

Output

isNameEditable : true
isAgeEditable : false
isCountryEditable : false



 

Previous                                                 Next                                                 Home

No comments:

Post a Comment