Monday, 20 March 2023

Playwright: dispatch an event on the element

Locator.dispatchEvent method, dispatch an event on the element.

 

Signature

void dispatchEvent(String type)
void dispatchEvent(String type, Object eventInit)
void dispatchEvent(String type, Object eventInit, DispatchEventOptions options)

'type' specifies the event type like 'click', 'dragstart' etc.,

'eventInit' specifies event-specific initialization properties.

 

Since the argument 'eventInit' is event-specific, refer to the events documentation for the lists of initial properties:

a.   DragEvent : https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent

b.   FocusEvent : https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent

c.    KeyboardEvent : https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

d.   MouseEvent : https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent

e.   PointerEvent : https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent

f.     TouchEvent : https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent

g.   Event : https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

 

Example

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

Above snippet generate a click event on the Page.

 

Find the below working application.

 

registerMe.html

<!DOCTYPE html>
<html>

<head> </head>

<body>

	<form onsubmit="return false;">

		<table>
			<tr>
				<td> <label for="email"><b>Email</b></label> </td>
				<td><input type="text" placeholder="Enter your email id" name="email" id="email" required> </td>
			</tr>

			<tr>
				<td> <label for="password"><b>Password</b></label></td>
				<td><input type="password" placeholder="Enter Password" name="password" id="password" required> </td>
			</tr>

			<tr>
				<td> <label for="confirm-password"><b>Repeat Password</b></label> </td>
				<td><input type="password" placeholder="Repeat Password" name=confirm-password" id="confirm-password"
						required></td>
			</tr>

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


		</table>

	</form>

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

	<script>
		function display() {
			let result = document.getElementById("result");
			let userEmail = document.querySelector('input[id="email"]').value;
			result.innerHTML = `<b>Your email :  ${userEmail} is registered</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();
		}

	}
}

DispatchEvent.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 DispatchEvent {
	private static void printValueBySelector(Page page, String selector, String label) {
		String value1 = page.inputValue(selector);

		System.out.println(label + " : " + value1);
	}

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

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

			page.getByLabel("email").type("demo@demo.com");
			page.locator("input#password").type("test1234");
			page.locator("input#confirm-password").type("test1234");

			printValueBySelector(page, "input#email", "email");
			printValueBySelector(page, "input#password", "password");
			printValueBySelector(page, "input#confirm-password", "confirm password");

			System.out.println("Dispatching click event");
			page.locator("input[type=\"submit\"]").dispatchEvent("click");
		
			String result = page.locator("#result").textContent();
			System.out.println(result);

		}
	}
}

Output

email : demo@demo.com
password : test1234
confirm password : test1234
Dispatching click event
Your email :  demo@demo.com is registered


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment