Sunday 14 May 2023

Playwright: Check whether the element is enabled or not

Locator.isEnabled() method return true, when the element is enabled, else false.

 

Signature

boolean isEnabled()
boolean isEnabled(IsEnabledOptions options)

 

Find the below working application.

 

elementEnableCheck.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>
		
		<label for="gender">Gender:</label>
		<input type="text" id="gender" name="gender" value="M" hidden><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();
		}

	}
}

ElementEnableCheck.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 ElementEnableCheck {
	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 isNameEnabled = page.locator("#name").isEnabled();
			boolean isAgeEnabled = page.locator("#age").isEnabled();
			boolean isCountryEnabled = page.locator("#country").isEnabled();

			System.out.println("isNameEnabled : " + isNameEnabled);
			System.out.println("isAgeEnabled : " + isAgeEnabled);
			System.out.println("isCountryEnabled : " + isCountryEnabled);

		}
	}
}

Output

isNameEnabled : true
isAgeEnabled : false
isCountryEnabled : true

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment