Quick revision
The short answer
With Playwright for Java we create a Playwright and a Browser once, give each test a fresh BrowserContext and Page, navigate, fill the fields found with getByLabel, click the button found with getByRole, and assert with PlaywrightAssertions.assertThat(...).isVisible(), which retries until its timeout. The waiting is built in: fill and click first wait for the element to be visible, enabled and stable, and web-first assertions keep retrying, so the explicit WebDriverWait calls a Selenium test needs simply disappear.
What to remember
In this answer, we wrote a Playwright for Java login test with one Browser per class and a fresh BrowserContext per test, user-facing locators from getByLabel and getByRole, and a web-first assertThat(...).isVisible() that retries. We saw that fill and click wait for the element to be visible, enabled, stable and not covered before acting, and that assertions retry until their timeout, which replaces the explicit WebDriverWait calls Selenium needs at the same points.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With Playwright for Java we create a Playwright and a Browser once, give each test a fresh BrowserContext and Page, navigate, fill the fields found with getByLabel, click the button found with getByRole, and assert with PlaywrightAssertions.assertThat(...).isVisible(), which retries until its timeout. The waiting is built in: fill and click first wait for the element to be visible, enabled and stable, and web-first assertions keep retrying, so the explicit WebDriverWait calls a Selenium test needs simply disappear.
Why this question is asked
Playwright has become a popular choice for new UI test suites, and Java teams often meet it after years of Selenium. The main thing that changes is not the syntax but where the waiting happens. Interviewers ask for this small test to see whether we know Playwright's object model, browser, context and page, its user-facing locators, and exactly which waits it does for us. In this answer, we will recap Playwright's main objects, write the test, walk through where Playwright waits automatically compared with Selenium, and set the test up so each test is isolated.
1A Quick Recap of Playwright's Objects
Playwright for Java is a library from Microsoft that drives Chromium, Firefox and WebKit through its own protocol. It has four main objects. Playwright is the entry point, created with Playwright.create(), which starts a small driver process in the background. Browser is a launched browser, for example from playwright.chromium().launch(). BrowserContext is an isolated session inside that browser, like a fresh incognito profile with its own cookies and storage. Page is one tab inside a context.
Launching a browser takes time, so we do it once. Creating a context takes milliseconds, so we create one per test. That is how Playwright gives each test a clean session cheaply.
- Playwrightone per thread;
Playwright.create() - Browserlaunched once, reused across tests
- BrowserContextone per test: own cookies, storage, permissions
- Pagea tab in the context; where the test acts
4 layers, from the top down. Step through them one at a time.
2Writing the Test
Here is a complete JUnit 5 test. It opens the login page, fills the form, submits it and checks that the dashboard heading appears.
Notice the locators. getByLabel finds an input by the text of its label, the way a user identifies a field, and getByRole finds an element by its accessibility role and accessible name, here a button named Sign in and a heading named Dashboard. These locators survive changes to layout and class names, and they quietly check that the page is accessible. A Locator in Playwright is a description of how to find an element, evaluated fresh every time we act on it, so there is no equivalent of Selenium's stale element problem.
3Where Playwright Waits for Us
Every action on a locator runs actionability checks before it acts. For click(), Playwright waits until the element is attached to the page, visible, stable (not moving in an animation), enabled, and able to receive the click, meaning no other element is covering it. For fill(), it waits until the element is visible, enabled and editable. If those conditions are not met within the action timeout, 30 seconds by default, it fails with a message saying which check did not pass.
The assertion waits too. PlaywrightAssertions.assertThat(locator).isVisible() is a web-first assertion: it keeps re-evaluating the locator until the heading is visible or the assertion timeout expires, five seconds by default. So the test does not need to know how long the login request takes. If the dashboard appears after 800 milliseconds, the assertion passes after 800 milliseconds.
| Step | Selenium needs | Playwright |
|---|---|---|
| Type the username | findElement, maybe wait for visibility | fill() waits: visible, enabled, editable |
| Click Sign in | WebDriverWait elementToBeClickable | click() waits: visible, stable, enabled, not covered |
| Overlay on the button | Separate wait for invisibility | Included: waits until it receives events |
| Check the heading | WebDriverWait visibilityOfElementLocated, then assert | assertThat(...).isVisible() retries |
| Element re-rendered | Re-find to avoid stale reference | Locator is re-evaluated on each action |
4The Same Test in Selenium, for Comparison
To see the difference, here are the same steps with Selenium. Every point where the page may still be changing needs an explicit wait that we write ourselves.
Notice that elementToBeClickable does not check for overlays, so if the page shows a spinner over the button, the Selenium version also needs a wait for the spinner, while Playwright's click already waits for the button to receive events. Neither tool removes the need to think about the application's state, but Playwright covers the common cases by default.
5Isolation and Clean-Up
The test reuses one Browser for the whole class, because launching it is the slow part, and creates a new BrowserContext in @BeforeEach. Each context has its own cookies, local storage and permissions, so a login in one test can never leak into the next. Closing the context in @AfterEach throws all of that away. Closing the Playwright object at the end closes the browser and stops the driver process.
One caution: the Playwright object and its browsers are not thread-safe. If we run JUnit tests in parallel, each thread needs its own Playwright and Browser, for example created per test class with a per-class test instance, or held in a ThreadLocal, rather than a single static shared by all threads.
6Follow-up questions
How do you record a trace for a failing test?
We start tracing on the context before the test and stop it with a file path afterwards: context.tracing().start(new Tracing.StartOptions().setScreenshots(true).setSnapshots(true).setSources(true)) in @BeforeEach, and context.tracing().stop(new Tracing.StopOptions().setPath(Paths.get("traces/login.zip"))) in @AfterEach, before closing the context. To keep only failures, we can stop without a path when the test passed, using a JUnit extension or TestWatcher to know the result. The trace zip contains every action, DOM snapshots before and after, network requests, console messages and screenshots. We open it with the Playwright CLI's show-trace command or at trace.playwright.dev, which runs locally in the browser, and step through the test as it ran.
How do you run headed to watch the test?
Pass launch options: playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(200)). Headed mode with a slow-motion delay is useful when debugging locally; CI stays headless.
Where do the browsers come from?
Playwright downloads its own Chromium, Firefox and WebKit builds that match the library version, the first time it runs or through its install command. It can also use an installed Chrome or Edge through the channel launch option.
7Conclusion
In this answer, we wrote a Playwright for Java login test with one Browser per class and a fresh BrowserContext per test, user-facing locators from getByLabel and getByRole, and a web-first assertThat(...).isVisible() that retries. We saw that fill and click wait for the element to be visible, enabled, stable and not covered before acting, and that assertions retry until their timeout, which replaces the explicit WebDriverWait calls Selenium needs at the same points.
Open with
In Playwright for Java I create a Playwright with Playwright.create() and launch a Chromium browser once for the test class, because launching is slow. For each test I create a new BrowserContext and a Page from it, and close the context afterwards, so every test has its own cookies and storage.
Then, if they want more
The test itself is page.navigate to the login URL, page.getByLabel("Username").fill("ana"), page.getByLabel("Password").fill with the password, then page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign in")).click(), and finally assertThat(page.getByRole(AriaRole.HEADING, with name Dashboard)).isVisible() from PlaywrightAssertions. The waiting is where Playwright differs from Selenium.
Before fill, Playwright waits until the field is visible, enabled and editable; before click, it waits until the button is visible, stable, enabled and not covered by another element, which Selenium's elementToBeClickable does not even check. Locators are re-evaluated on every action, so there is no stale element problem.
And the assertion is web-first: it keeps retrying until the heading appears or about five seconds pass, so I do not need WebDriverWait at all, where Selenium needs an explicit wait at each of those points. One caution is that Playwright objects are not thread-safe, so for parallel tests each thread needs its own Playwright.
To debug failures I would turn on tracing on the context and open the trace zip in the trace viewer.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Playwright for Java test that opens a login page, fills the form, and asserts...": https://scrivio-interview-library.netlify.app/java/ui-testing/424aec3a2c/