Last Updated On
Welcome to the Playwright Java Quiz! This blog post features 25 multiple-choice questions that explore the concepts of locators and Actions in Playwright Java.
1. What is a Locator in Playwright?
a) A method used only to open a browser
b) An object used to identify elements on a web page
c) A tool used to install browsers
d) A Java class used only for screenshots
Answer 1
b) An object used to identify elements on a web page
A Locator identifies one or more elements on a page. Playwright resolves the locator when an action or query is performed, which helps it work with dynamic web pages.
Locator loginButton = page.locator("#login");
loginButton.click();
2. Which method is recommended for locating a button by its accessible role and name?
a) page.findButton("Login")
b) page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"))
c) page.locateButton("Login")
d) page.getButton("Login")
Answer 2
b) page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(“Login”))
getByRole() identifies an element by its accessibility role and accessible name. This often produces more reliable tests than selectors based on CSS classes.
3. Which method locates an element by its visible text?
a) page.getByText("Welcome")
b) page.getByVisible("Welcome")
c) page.findText("Welcome")
d) page.textLocator("Welcome")
Answer 3
a) page.getByText(“Welcome”)
getByText() finds elements by text content. By default, matching can be based on partial text, depending on the element and matching rules.
4. What happens if click() is called on a locator that matches multiple elements?
a) Playwright always clicks the first element
b) Playwright always clicks the last element
c) Playwright normally throws a strictness error
d) Playwright clicks every matching element
Answer 4
c) Playwright normally throws a strictness error
Locator actions that require one target are strict. The locator must resolve to a single element.
5. How do you locate a text input using its label?
a) page.getByLabel("Username")
b) page.getByInput("Username")
c) page.findByLabel("Username")
d) page.locatorLabel("Username")
Answer 5
a) page.getByLabel(“Username”)
getByLabel is useful for form fields associated with labels or other supported accessible labeling mechanisms.
page.getByLabel("Username").fill("Vibha");
6. Which method locates an input by its placeholder?
a) page.getByHint("Enter email")
b) page.getByPlaceholder("Enter email")
c) page.getByInputText("Enter email")
d) page.findPlaceholder("Enter email")
Answer 6
b) page.getByPlaceholder(“Enter email”)
getByPlaceholder – Placeholder locators target fields using placeholder text.
page.getByPlaceholder("Enter email").fill("vibha@example.com");
7. What does page.locator(“#username”) mean?
a) Locate an element whose ID is username
b) Locate an element whose class is username
c) Locate an element whose text is username
d) Locate every input on the page
Answer 7
a) Locate an element whose ID is username
The # symbol identifies an element by its CSS ID selector.
#username selects an element by ID.
.username selects elements by class.
input selects elements by HTML tag.
8. What does locator.first() do?
a) Selects the first matching element
b) Selects the last matching element
c) Counts matching elements
d) Selects all matching elements
Answer 8
a) Selects the first matching element
If several elements match a locator, first() returns a locator targeting the first match.
9. Which method returns the number of elements matching a locator?
a) length()
b) size()
c) count()
d) total()
Answer 9
c) count()
The count() method returns the number of matching elements. count() can be used when zero, one, or multiple elements match.
int count = page.locator(".product").count();
10. What is the primary difference between locator.fill(“text”) and locator.pressSequentially(“text”) in Playwright Java?
a) fill triggers individual keyboard events for each character, whereas pressSequentially sets the field value instantly.
b) fill sets the value instantly by clearing and populating the element, while pressSequentially sends granular key events per character.
c) fill only works on non-input elements, while pressSequentially only works on text input tags.
d) There is no difference; they are exact functional aliases.
Answer 10
b) fill sets the value instantly by clearing and populating the element, while pressSequentially sends granular key events per character.
fill() is generally the simplest way to populate an input field) pressSequentially() types characters one by one and can be useful when the application responds to individual keyboard input events.
11. Which code locates a button with the exact accessible name “Save”?
Choose one option
a) page.getByRole(
AriaRole.BUTTON,
new Page.GetByRoleOptions()
.setName("Save")
.setExact(true)
);
b) page.getByRole(AriaRole.BUTTON)
.exact("Save");
c) page.locator("button")
.setExactText("Save");
d) page.getByText("Save")
.setRole(AriaRole.BUTTON);
Answer 11
a) getByRole() can combine an accessible role with an accessible name. Setting exact to true requires an exact name match rather than a partial match.
12. Which method can narrow a locator based on conditions such as text?
a) locator.search()
b) locator.where()
c) locator.filter()
d) locator.refine()
Answer 12
c) locator.filter()
filter() can narrow an existing locator according to additional criteria.
page.locator(".product")
.filter(new Locator.FilterOptions()
.setHasText("Laptop"));
13. Which method is specifically intended for locating an element by its test ID?
Choose one option
a) page.locatorById()
b) page.findTestId()
c) page.getByTestId()
d) page.testId()
Answer 13
c) page.getByTestId()
getByTestId() provides a convenient way to locate elements using a test-id attribute.
14.What does locator.nth(2) select?
Choose one option
a) The second element
b) The third element
c) The fourth element
d) All elements after the second
Answer 14
b) The third element
15. Consider the following HTML:
<div class="user">
<span class="name">John</span>
<button>Edit</button>
</div>
How can you click the Edit button inside the user container?
a) page.locator(".user").locator("button").click();
b) page.locator(".user button").click();
c) Both a and b
d) Neither a nor b
Answer 15
c) Both a and b
Both approaches use valid CSS-based locator strategies. Chaining with .locator("button") scopes the child locator to the parent locator, while .user button uses a CSS descendant selector. If multiple user containers exist, further filtering is needed to target the correct one.
Setting headless to false displays the browser.
16. How do you locate a list item that contains a descendant heading named “Product 2”?
Choose one option
a) page.getByRole(AriaRole.LISTITEM)
.filter(new Locator.FilterOptions()
.setHas(
page.getByRole(
AriaRole.HEADING,
new Page.GetByRoleOptions().setName("Product 2")
)
));
b) page.getByRole(AriaRole.LISTITEM)
.getParent("Product 2");
c) page.getByText("Product 2")
.parent();
d) page.locator("Product 2")
.closest("listitem");
Answer 16
a)
setHas() filters candidate elements based on whether they contain another matching locator.
17. Which code locates an element that is both a button and has the title “Subscribe”?
Choose one option
a) page.getByRole(AriaRole.BUTTON)
.or(page.getByTitle("Subscribe"));
b) page.getByRole(AriaRole.BUTTON)
.and(page.getByTitle("Subscribe"));
c) page.getByRole(AriaRole.BUTTON)
.filter("Subscribe");
d) page.getByTitle("Subscribe")
.contains(AriaRole.BUTTON);
Answer 17
b)
locator.and() creates a locator matching the intersection of two locators. The resulting element must satisfy both conditions.
18. Which Playwright Java method is used to select a checkbox?
a) clickCheckbox()
b) select()
c) check()
d) setCheckedOnly()
Answer 18
c) check()
check() ensures a checkbox is checked. If it is already checked, Playwright leaves it checked rather than toggling it off.
page.locator("#rememberMe").check();
19. Which method is used to clear a checked checkbox?
a) clear()
b) uncheck()
c) deselect()
d) removeCheck()
Answer 19
b) uncheck()
uncheck() ensures that a checkbox becomes unchecked.
page.locator("#rememberMe").uncheck();
20. How can you verify that a checkbox is checked?
Choose one option
a) locator.isChecked()
b) locator.isSelected()
c) locator.hasCheck()
d) locator.getChecked()
Answer 20
a) locator.isChecked()
isChecked() returns a boolean indicating whether a checkbox or radio button is checked.
21. Which Playwright Java method should be used for a standard HTML dropdown?
a) click()
b) choose()
c) selectOption()
d) selectDropdown()
Answer 21
c) selectOption()
selectOption() is specifically designed for standard HTML elements.
page.locator("#country").selectOption("ie");
22. How do you select an option based on its visible label?
a) selectOption(“Ireland”)
b) selectOption(new SelectOption().setLabel(“Ireland”))
c) selectLabel(“Ireland”)
d) chooseLabel(“Ireland”)
Answer 22
b) selectOption(new SelectOption().setLabel(“Ireland”))
SelectOption allows you to specify whether you want to match an option’s value, label, or index.
page.locator("#country").selectOption(
new SelectOption().setLabel("Ireland")
);
23. A allows multiple options to be selected. Which approach can select “Java” and “Python”?
a) selectOption("Java", "Python");
b) selectOptions("Java", "Python");
c) selectOption(new String[]{"java", "python"});
d) multiSelect("java", "python");
Answer 23
c) selectOption(new String[]{“java”, “python”});
selectOption() can accept multiple values for a multi-select .
24. What is the primary purpose of matchesAriaSnapshot() in Playwright Java?
a) Compare screenshots pixel by pixel
b) Check whether an element’s CSS matches expected styles
c) Assert the accessibility tree against a predefined ARIA snapshot template
d) Verify that all elements have an aria-label
Answer 24
c) Assert the accessibility tree against a predefined ARIA snapshot template
matchesAriaSnapshot() is used for snapshot testing of the accessibility tree. Playwright compares the current accessibility structure against a predefined snapshot template.
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page).matchesAriaSnapshot("""
- heading "todos"
- textbox "What needs to be done?"
25. Why can calling locator.all() immediately on a dynamically loading list be unreliable?
a) all() supports only one element
b) all() automatically deletes duplicate elements
c) all() returns the elements currently present without waiting for the dynamic list to finish loading
d) all() works only with hidden elements
Answer 25
c) all() returns the elements currently present without waiting for the dynamic list to finish loading
The Playwright Java API states that Locator.all() doesn’t wait for elements to match. It immediately returns locators for the elements currently present, so a changing dynamic list can produce unpredictable results.
We would love to hear from you! Please leave your comments and share your scores in the section below