Last Updated On
Welcome to the Playwright Java Quiz! This blog post features 25 multiple-choice questions that explore basic concepts of Playwright Java.
1. What is Playwright primarily used for in Java development?
a) Data processing
b) Web automation and testing
c) GUI design
d) Database management
Answer 1
b) Web automation and testing
2. What programming paradigm does Playwright support?
a) Functional programming
b) Object-oriented programming
c) Procedural programming
d) Reactive programming
Answer 2
b) Object-oriented programming
3. Which types of browsers does Playwright support?
a) Chrome, Firefox, WebKit
b) Opera, Netscape, Lynx
c) IE, Safari, Tor
d) Edge, Brave, Vivaldi
Answer 3
a) Chrome, Firefox, WebKit
4. Playwright APIs can be used with _
a) Java, JavaScript, TypeScript, Python, .Net
b) Cobol, Groovy, Gherkin, Java
c) Python, Cobol, C, C++
d) .Net, Gherkin, PHP
Answer 4
a) Java, JavaScript, TypeScript, Python, .Net
5. How do you initialize Playwright in a Java project?
a) new Playwright()
b) Playwright.create()
c) Playwright.builder()
d) Playwright.createDefault()
Answer 5
b) Playwright.create()
Playwright playwright = Playwright.create()
6. In Playwright Java, which class is used to create a new page?
a) BrowserContext
b) Playwright
c) Page
d) NewPage
Answer 6
c) Page
The Page class is central to interacting with browser pages or tabs. When a new page is created using a BrowserContext, it provides methods to navigate, perform actions like clicks or types, and evaluate expressions in the page’s context.
Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
7. Which statement is true about launching a browser in Playwright Java?
a) Playwright launches browsers via WebDriver
b) Playwright directly controls browser instances
c) Browsers must be manually installed
d) Playwright only supports Google Chrome
Answer 7
b) Playwright directly controls browser instances
Playwright directly controls browser instances using its own library APIs to provide high performance. It supports multiple browsers like Chrome, Firefox, and Safari without relying on WebDriver.
8. How do you launch a browser using Playwright Java?
a) Browser browser = new ChromeBrowser();
b) Browser browser = Playwright.create().chromium().launch();
c) Playwright playwright = new Playwright();
d) Browser browser = Playwright.newContext();
Answer 8
b) Browser browser = Playwright.create().chromium().launch();
To launch a browser, you first create a Playwright object, select the browser engine (e.g., Chromium), and call the launch() method to start a new browser instance.
9. What is the primary purpose of a BrowserContext in Playwright?
a) It represents a running browser process
b) It isolates multiple pages from affecting each other
c) It is used for logging browser activities
d) It manages browser installations
Answer 9
b) It isolates multiple pages from affecting each other
A BrowserContext enables isolation between pages, cookies, and caches. It is useful for creating separate test scenarios without affecting other sessions.
10. Which method is used to navigate to a URL?
a) navigate(url);
b) visit(url);
c) goTo(url);
d) page.navigate(url);
Answer 10
d) page.navigate(url);
The navigate() method is called on a Page object to visit a specific URL.
11. What is the default behavior of Playwright when interacting with elements?
Choose one option
a) It waits for the element to be visible
b) It interacts instantly without waiting
c) It delays every operation by 5 seconds
d) It throws an error if the element is not immediately available
Answer 11
a) It waits for the element to be visible
Playwright automatically waits for elements to be visible and actionable before performing interactions, offering reliable testing.
12. Which statement is true about locators in Playwright Java?
a) Locators are predefined for all elements
b) Locators are generated using XPath only
c) Playwright supports various locator strategies like CSS, XPath, and text-based targeting
d) Locators are dependent on WebDriver
Answer 12
c) Playwright supports various locator strategies like CSS, XPath, and text-based targeting
Playwright provides powerful locator strategies, including CSS selectors, XPath, and text-based targeting, making it flexible for various use cases.
13. How can you close a Playwright browser instance in Java?
Choose one option
a) browser.close()
b) playwright.close()
c) cleanup()
d) browser.quit()
Answer 13
The close() method is used to release resources and close the Playwright browser instance.
browser.close()
14. Which statement is true about headless mode in Playwright?
Choose one option
a) Headless mode disables most of the webpage functionalities
b) It’s slower than headful mode
c) It runs the browser without a GUI
d) Only supports Chrome browser
Answer 14
c) It runs the browser without a GUI
Headless mode means the browser runs without a graphical user interface (GUI), making it faster and more resource-efficient for automation.
15. Which option launches browser in visible mode?
Choose one option
a) visible=true
b) headed=true
c) headless=false
d) showBrowser=true
Answer 15
c) headless=false
Setting headless to false displays the browser.
16. How do you close all pages when using Playwright?
Choose one option
a) playwright.clearAll()
b) Manually close each page using page.close()
c) Automatically close through browser.close()
d) context.closeAllPages()
Answer 16
c) Automatically close through browser.close()
Closing the browser instance using browser.close() automatically closes all associated pages.
17. What is a key advantage of Playwright over Selenium?
Choose one option
a) Built-in auto-waiting
b) Requires browser drivers manually
c) Supports only Chrome
d) No API support
Answer 17
a) Built-in auto-waiting
Auto-waiting reduces flaky tests and simplifies code.
18. Which testing frameworks commonly integrate with Playwright?
a) JUnit
b) TestNG
c) Cucumber
d) All of the above
Answer 18
d) All of the above
Playwright Java works well with JUnit, TestNG, and Cucumber frameworks.
19. How do you emulate a specific viewport size in Playwright Java?
a) Page.resize(width, height)
b) ViewportSize.setSize(width, height)
c) Pass viewportSize in browser launch()
d) Pass viewportSize in newContext()
Answer 19
d) Pass viewportSize in newContext()
In Playwright Java, the viewport size is configured when creating a BrowserContext, using Browser.NewContextOptions().setViewportSize().
BrowserContext context = browser.newContext(
new Browser.NewContextOptions()
.setViewportSize(1280, 720)
);
Page page = context.newPage();
20. How can you launch a browser in headed mode?
Choose one option
a) new BrowserType.LaunchOptions().setHeadless(false)
b) new BrowserType.LaunchOptions().setVisible(true)
c) browser.setHeadless(false)
d) playwright.setGUI(true)
Answer 20
a) new BrowserType.LaunchOptions().setHeadless(false)
Setting headless to false tells Playwright to display the actual browser window.
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
21. Which method can locate an element using a CSS selector?
a) page.locator()
b) page.element()
c) page.findElement()
d) page.search()
Answer 21
a) page.locator()
page.locator() creates a Playwright Locator for finding and interacting with elements.
page.locator("css=button").click();
22. Which method is commonly used to enter text into an input field?
a) locator.fill()
b) locator.write()
c) locator.input()
d) locator.set()
Answer 22
a) locator.fill()
fill() sets the value of an editable field, such as a text box.
page.locator("#username").fill("Vibha");
23. Which method clears an input and sets its value to “Vibha”?
a) locator.type(“Vibha”)
b) locator.fill(“Vibha”)
c) locator.text(“Vibha”)
d) locator.sendKeys(“Vibha”)
Answer 23
b) locator.fill(“Vibha”)
fill() sets the input’s value. When the field already contains text, fill() replaces the existing value rather than simply appending characters.
24. Which method can retrieve the text content of a locator?
a) locator.getText()
b) locator.textContent()
c) locator.read()
d) locator.valueText()
Answer 24
b) locator.textContent()
textContent() retrieves the text content associated with the selected DOM element.
String text = page.locator("#message").textContent();
25. Which locator is generally a good choice for locating an accessible button by its role and name?
a) page.getByRole(…)
b) page.getByCss(…)
c) page.findButton(…)
d) page.getElement(…)
Answer 25
a) page.getByRole(…)
getByRole() locates elements according to their accessible role, such as button, checkbox, heading, or link.
page.getByRole(
AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Login")
).click();
We would love to hear from you! Please leave your comments and share your scores in the section below