Tutorial 5 – ‘Record’ feature in Playwright-Java
What you will Learn in this blog:
How to use ‘Record’ feature in Playwright-Java
Code snippets
How to use ‘Record feature in Playwright-Java
Let us create a simple test with page.pause() method as shown below. This method will allow us to use the ‘Record’ feature in playwright inspector
Save and run the test.
Notice below that the webpage launches alongwith the inspector having ‘Record’ button
Click this ‘Record’ button
Enter username as ‘Admin’ and password as ‘admin123’
Click Login
The below page would come up
Notice that the actions get automatically recorded in the inspector, see below
Let us copy the line#3 and lines#13-27 and paste them in notepad
Click ‘Record’ to stop recording
Close the inspector window.
Paste the recorded code snippet inside our test (line#8, lines#16-26)
Make sure that pause() command is at the end (line#27).
Save and execute.
The script replays and the login operation is success
Click ‘step-over’
The script execution gets over
This is how we can easily record an action in playwright!
Code snippet (before Record operation)
package com.w2a.pwjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class PWTest {
@Test
public void PWFirstTest() {
Playwright pw = Playwright.create();
Page page = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)).newPage();
page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
page.pause();
}
}
Code snippet (after Record operation)
package com.w2a.pwjava;
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 PWTest {
@Test
public void PWFirstTest() {
Playwright pw = Playwright.create();
Page page = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)).newPage();
page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
// Click [placeholder="Username"]
page.locator("[placeholder=\"Username\"]").click();
// Fill [placeholder="Username"]
page.locator("[placeholder=\"Username\"]").fill("Admin");
// Click [placeholder="Password"]
page.locator("[placeholder=\"Password\"]").click();
// Fill [placeholder="Password"]
page.locator("[placeholder=\"Password\"]").fill("admin123");
// Click button:has-text("Login")
page.locator("button:has-text(\"Login\")").click();
assertThat(page).hasURL("https://opensource-demo.orangehrmlive.com/web/index.php/pim/viewEmployeeList");
page.pause();
}
}
Thank you for reading!