Sunday, 12 March 2023

Playwright: focus the element

Using Locator.focus() method we can focus the element.

 

Let’s see it with an example.

 

Example

page.locator("#demo").focus();

 

Find the below working application.

 

focusAction.html
<!DOCTYPE html>
<html>

<body>

	Enter your something: <input type="text" id="demo" onfocusout="f1()" onfocusin="f2()" onfocus="f3()">

	<br />
	<br />
	<br />

	onfocusout: <p id="result1"></p>
	onfocusin: <p id="result2"></p>
	onfocus: <p id="result3"></p>

	<input type="button" id="myButton" value="submit" />


	<script>
		function f1() {
			let x = document.getElementById("demo");
			x.value = x.value.toUpperCase();
			document.getElementById("result1").innerHTML = x.value;
			document.getElementById("result2").innerHTML = "";
			document.getElementById("result3").innerHTML = "";
		}

		function f2() {
			document.getElementById("result2").innerHTML = "Element is in focus";
		}

		function f3() {
			document.getElementById("result3").innerHTML = "Element is in focus";

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

	}
}

FocusEvent.java

package com.sample.app.actions;

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 FocusEvent {
	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("actions" + File.separator + "focusAction.html");

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

			page.locator("#demo").focus();
			String result1, result2, result3;
			result1 = page.locator("#result1").innerText();
			result2 = page.locator("#result2").innerText();
			result3 = page.locator("#result3").innerText();
			System.out.println("result1 : " + result1);
			System.out.println("result2 : " + result2);
			System.out.println("result3 : " + result3);

			
			System.out.println("\nType hello to input box");
			page.locator("#demo").type("Hello");
			result1 = page.locator("#result1").textContent();
			result2 = page.locator("#result2").textContent();
			result3 = page.locator("#result3").textContent();
			System.out.println("result1 : " + result1);
			System.out.println("result2 : " + result2);
			System.out.println("result3 : " + result3);
			
			
			// To generate focus out event
			System.out.println("\nMove the cursor out of textbox");
			page.locator("#myButton").click();
			
			result1 = page.locator("#result1").textContent();
			result2 = page.locator("#result2").textContent();
			result3 = page.locator("#result3").textContent();
			System.out.println("result1 : " + result1);
			System.out.println("result2 : " + result2);
			System.out.println("result3 : " + result3);
			
			
		}
	}

}

Output

result1 : 
result2 : Element is in focus
result3 : Element is in focus

Type hello to input box
result1 : 
result2 : Element is in focus
result3 : Element is in focus

Move the cursor out of textbox
result1 : HELLO
result2 : 
result3 :

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment