Tuesday 16 May 2023

Playwright: Check whether an element is visible or not in the page

There are multiple ways to check whether an element is visible or not in a page.

 

Approach 1: Using page.locator(someLocator).isVisible() method

isVisible() method return true, if the element is visible, else false.

 

Approach 2: Using page.isVisible method.

page.isVisible(someSelector)

 

Approach 3: By counting the number of elements matched to given selector.

page.locator(someSelector).count()

 

elementVisibleCheck.html

<!DOCTYPE html>
<html>

<body>

	<h1>Element visible check</h1>

	<p id="myPara1">Hi there</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();
		}

	}
}

 

ElementVisibleCheck.java

package com.sample.app.miscellaneous;

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

	public static void elementVisibleCheck(final Page page, final String selector) {
		boolean isVisible1 = page.locator(selector).isVisible();
		boolean isVisible2 = page.isVisible(selector);
		boolean isVisible3 = (page.locator(selector).count() > 0 ? true : false);

		System.out.println("\nis the element with selector " + selector + " visible : " + isVisible1);
		System.out.println("is the element with selector " + selector + " visible : " + isVisible2);
		System.out.println("is the element with selector " + selector + " visible : " + isVisible3);
	}

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

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

			String idSelector = "p[id=\"myPara1\"]";
			elementVisibleCheck(page, idSelector);

			idSelector = "p[id=\"myPara2\"]";
			elementVisibleCheck(page, idSelector);

		}
	}

}

Output

is the element with selector p[id="myPara1"] visible : true
is the element with selector p[id="myPara1"] visible : true
is the element with selector p[id="myPara1"] visible : true

is the element with selector p[id="myPara2"] visible : false
is the element with selector p[id="myPara2"] visible : false
is the element with selector p[id="myPara2"] visible : false


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment