Here's a carefully curated list of possible Playwright + Java interview questions, including:
-
✅ Core Java (OOPs, exceptions, etc.)
-
✅ Playwright automation
-
✅ TestNG framework
-
✅ BDD with Cucumber
-
✅ Bonus: Code snippets, concepts, and real-life scenarios
Let’s go! 🔥
🚀 Playwright + Java Automation – Interview Q&A
1. What is Playwright and how is it different from Selenium?
✅ Answer:
Playwright is a modern automation library for web apps that supports multiple languages (Java, JavaScript, Python, etc.).
It supports:
-
Multiple browsers (Chromium, Firefox, WebKit)
-
Auto-waiting
-
Headless/headful mode
-
Native support for iframes, tabs, and download uploads
-
Built-in screenshot/video capabilities
📌 Unlike Selenium:
-
Playwright is faster and more reliable due to auto-waiting
-
Has native support for modern UI elements and multiple tabs
2. How do you launch a browser and open a page in Playwright Java?
✅ Code Example:
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://example.com");
3. How do you locate elements in Playwright Java?
✅ Answer:
-
page.locator("css selector")
-
page.getByText("Login")
-
page.getByRole(…)
📌 Example:
Locator loginBtn = page.locator("#login-button");
loginBtn.click();
4. How do you handle dropdowns in Playwright?
page.selectOption("#dropdown-id", new SelectOption().setLabel("Option 1"));
5. How to capture a screenshot?
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot.png")));
🧪 TestNG + Playwright
6. How do you integrate Playwright with TestNG?
✅ Answer:
Create a base class with setup/teardown methods using @BeforeClass
, @AfterClass
, and write test methods using @Test
.
✅ Code Sample:
@BeforeClass
public void setup() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
page = browser.newPage();
}
@Test
public void testLogin() {
page.navigate("https://example.com");
page.fill("#username", "admin");
page.fill("#password", "pass");
page.click("#loginBtn");
}
7. How do you take a screenshot on failure in TestNG?
@AfterMethod
public void tearDown(ITestResult result) {
if (ITestResult.FAILURE == result.getStatus()) {
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshots/" + result.getName() + ".png")));
}
}
🧾 Java + OOPs Interview Questions
8. Explain OOPs Concepts in Java (with real test example)?
✅ Answer:
-
Encapsulation: Page classes hold locators and actions.
-
Inheritance:
BaseTest
class is extended by test classes. -
Polymorphism: Method overloading in utility classes.
-
Abstraction: Interface for Page layer.
9. What’s the difference between ArrayList
and LinkedList
?
✅ Answer:
-
ArrayList
: Fast for retrieval, slower for inserts/removals -
LinkedList
: Faster for insertions/removals, slower for retrieval
10. What are checked vs unchecked exceptions?
✅ Answer:
-
Checked: Caught at compile time (
IOException
) -
Unchecked: Runtime exceptions (
NullPointerException
)
🧠 BDD with Cucumber + Java + Playwright
11. How do you implement BDD in Java with Playwright?
✅ Answer:
-
Use Cucumber for BDD
-
Define
.feature
file -
Use Step Definitions to map Gherkin steps to Playwright Java code
12. Sample Feature File
Feature: Login functionality
Scenario: Successful login
Given I open the login page
When I enter valid credentials
Then I should see the homepage
13. Step Definition Example
@Given("I open the login page")
public void openLoginPage() {
page.navigate("https://example.com/login");
}
14. How do you share Playwright context between steps?
✅ Answer:
Use a TestContext
or Hooks
class to store and share browser/page instances.
⚙️ Real-World Scenarios
15. How do you handle pop-ups or alerts?
page.onDialog(dialog -> {
System.out.println("Alert Text: " + dialog.message());
dialog.accept();
});
16. How do you handle multiple windows or tabs?
Page newPage = context.waitForPage(() -> {
page.click("#open-new-tab");
});
newPage.bringToFront();
17. How do you wait for an element in Playwright?
page.waitForSelector("#element-id", new Page.WaitForSelectorOptions().setTimeout(5000));
18. How do you verify an element is visible and enabled?
boolean isVisible = page.locator("#submit").isVisible();
boolean isEnabled = page.locator("#submit").isEnabled();
19. Playwright’s default timeout? Can it be changed?
✅ Default: 30 seconds
✅ Override:
page.setDefaultTimeout(10000); // 10 seconds
20. Playwright test best practices
-
Use Page Object Model
-
Isolate test data
-
Use assertions wisely
-
Capture screenshots on failure
-
Avoid hard waits, use Playwright's auto-waiting