keyboard.up() method in Playwright Java

Earlier, we discussed the keyboard.down() method that dispatches a keydown event ( means a key will remain pressed until it is released). This article will delve into the keyboard.up() method in Playwright Java, exploring its functionality and how it contributes to seamless web automation.

What does keyboard.up() method do?
  • The ‘keyboard.up()‘ method sends a ‘keyup‘ event. This event is triggered when a key is released.
  • up() method takes a key as an argument and triggers a keyup event for that key.
Syntax of using keyboard.up() method?
page.keyboard().up(key);
Code Example

We will use our playground website to showcase the use of the up() method.

Usually, the keyboard.up() method is used after the keyboard.down() method to release a key after holding it down.

In our example, we will use the down() function to press and hold the “ShiftLeft” key, and then release it using the up() function.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();

		// Launching the browser
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating the page
		Page page = browserContext.newPage();

		page.navigate("https://testkru.com/Interactions/KeyboardActions");

		page.keyboard().down("ShiftLeft");
		page.keyboard().up("ShiftLeft");

		browser.close();
		playwright.close();

	}

}
keyup event using shiftleft key

We can see that the keyup event is fired at the end.

The What If scenarios
Q – What if we pass an invalid key as an argument to the keyboard.up() function?

We will get a PlaywrightException if we pass an invalid ( say, “xyz” ) as an argument to the up() function, as illustrated by the below example.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();

		// Launching the browser
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating the page
		Page page = browserContext.newPage();

		page.navigate("https://testkru.com/Interactions/KeyboardActions");

		page.keyboard().up("xyz");

		browser.close();
		playwright.close();

	}

}

Output –

Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
  message='Unknown key: "xyz"
  name='Error
  stack='Error: Unknown key: "xyz"
Q – What if we pass null as an argument to the keyboard.up() function?

We will again get the PlaywrightException.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();

		// Launching the browser
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		// creating the page
		Page page = browserContext.newPage();

		page.navigate("https://testkru.com/Interactions/KeyboardActions");

		page.keyboard().up(null);

		browser.close();
		playwright.close();

	}

}

Output –

Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
  message='key: expected string, got undefined
  name='Error
  stack='Error: key: expected string, got undefined

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *