One of the essential features of Playwright Java is the keyboard.down()
method. This method allows us to simulate user interactions with keyboard inputs accurately. Whether it’s pressing a single key, handling key modifiers, or emulating continuous key presses, keyboard.down() offers a wide array of possibilities to enhance the realism and accuracy of test scenarios.
This article will dive deep into the capabilities of the keyboard.down() method in Playwright Java.
- Method declaration – void down(String key);
- What does it do? It dispatches a keydown event for the key passed as an argument. We can pass any keyboard key, including the function or control key.
A list of keyboard keys can be found on this page – https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values.
When is the keydown event triggered?
When a key is pressed, the keydown event is triggered. The key will remain pressed until it is released with the keyboard.up() method.
Syntax of using down() method
page.keyboard().down(key);
Code Example
We will demonstrate the keyboard.down() method on our playground website (https://testkru.com/Interactions/KeyboardActions).
Let’s trigger a keydown event for the ShiftLeft key using the down() method.
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");
browser.close();
playwright.close();
}
}
The code above will initiate a keydown event for the shiftLeft key, as shown on our playground website.
So, this is how we can hold down any key using the down() function.
Combining multiple keys
We can also hold down multiple keys at once.
page.keyboard().down("ShiftLeft");
page.keyboard().down("a");
There are few points to remember –
- If a key is a single character, it is case-sensitive, so the values
a
andA
will generate different respective texts. - If a key is a modifier key such as Shift, Control, Alt, or Meta, any subsequent key pressed will be sent with that modifier active. For example, pressing Shift and then ‘a’ will result in the letter ‘A’ being typed.
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) throws InterruptedException {
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");
// holding down multiple keys
page.keyboard().down("ShiftLeft");
page.keyboard().down("a");
browser.close();
playwright.close();
}
}
The What If scenarios
Q – What if we pass an invalid key as an argument to the keyboard.down() function?
We will get a PlaywrightException if we pass an invalid ( say, “abc” ) as an argument to the down() 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) throws InterruptedException {
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");
// passing invalid key
page.keyboard().down("abc");
browser.close();
playwright.close();
}
}
Output –
Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
message='Unknown key: "abc"
name='Error
stack='Error: Unknown key: "abc"
Q – What if we pass null as an argument to the keyboard.down() 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) throws InterruptedException {
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");
// passing null
page.keyboard().down(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.