In this blog, we will learn about Playwright-Java's 'Record' capability that enables users to create scripts by recording their actions on a web page, making it a convenient tool for test automation.
Topics that we will cover:
- ‘Record’ capability in Playwright-Java
- Source code (before Record operation)
- Source code (after Record operation)
‘Record’ capability in Playwright-Java
In order to utilize the 'Record' feature in playwright inspector, we can create a basic test and incorporate the page.pause() method as demonstrated below. This will enable us to pause the execution at specific points during the test

Execute the script.
The webpage is launched simultaneously with the inspector, which includes a 'Record' button, see below

On the webpage you can see username and password fields.
Let us click ‘Record’ to start recording. Notice below that ‘Record’ menu comes up


The actions are automatically recorded in the inspector, as shown below

Let us copy line#3 and lines#12-16 and paste them in notepad

Let us close the playwright inspector window.
Insert the recorded code snippet into our test, at line 7 and lines 19 to 23

It is important to have the pause() command positioned at the end of line 25 (see above).
Execute the code.
Below, you can see the script being replayed and the subsequent entry of the username and password


So, recording actions in playwright is made simple with this approach.
Source code (before Record operation)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog5_Record {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
Page pg1 = pt.chromium().launch(new BrowserType.
LaunchOptions().
setHeadless(false)).
newPage();
pg1.navigate("https://www.facebook.com/");
pg1.pause();
}
}
Source code (after Record operation)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class Blog5_Record {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
Page pg1 = pt.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)).
newPage();
pg1.navigate("https://www.facebook.com/");
pg1.getByTestId("royal_email").click();
pg1.getByTestId("royal_email").fill("playwrightjava@gmail.com");
pg1.getByTestId("royal_pass").click();
pg1.getByTestId("royal_pass").fill("playwright");
pg1.getByTestId("royal_login_button").click();
pg1.pause();
}
}
Thanks.