Quick revision
The short answer
A login page object holds the driver and private By locators for the username, password and submit button, and exposes one intention, loginAs(user, password), which fills the form, submits it, waits as needed and returns the next page object, a DashboardPage. The test then reads as plain steps and makes the assertions itself. Locators, waits and navigation belong in the page object; assertions do not, although the page object may expose state such as errorMessage() for the test to check.
What to remember
In this answer, we wrote a LoginPage that keeps private By locators, waits for the page in its constructor, and exposes intentions, loginAs returning a DashboardPage and loginExpectingError returning itself, plus read-only state like errorMessage(). The test made the assertions and contained no locators or waits. We saw that locators, waits and navigation belong in page objects, assertions and test data setup do not, and that plain By fields are simpler and safer than PageFactory.
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.
A login page object holds the driver and private By locators for the username, password and submit button, and exposes one intention, loginAs(user, password), which fills the form, submits it, waits as needed and returns the next page object, a DashboardPage. The test then reads as plain steps and makes the assertions itself. Locators, waits and navigation belong in the page object; assertions do not, although the page object may expose state such as errorMessage() for the test to check.
Why this question is asked
The page object pattern is the first design idea most people learn in UI testing, and it is also the one most often done badly: page objects full of assertions, tests full of locators, or classes that expose every element as a public field. Interviewers ask this coding question because a short page object shows whether we know what the pattern is for, which is keeping tests readable and putting each change in one place. In this answer, we will recap the pattern, write the LoginPage and a test, and then go through what belongs in a page object and what does not, including why plain By fields are simpler than PageFactory.
1A Quick Recap of the Page Object Pattern
A page object is a class that represents one page, or one part of a page, of the application under test. Its job is to hide how the page is built, the locators and the waits, behind methods that describe what a user does on it. Tests call those methods and never touch the driver's find calls directly.
The benefit is about change. If the login button's test id changes, we update one line in LoginPage, and every test that logs in keeps working. Without page objects, the same locator would be copied into dozens of tests, and each would need editing.
2Writing the LoginPage
Here is a complete page object. The locators are private fields of type By, which describes how to find an element without finding it yet. The public method loginAs expresses the user's intention and returns the page the user lands on.
Notice three choices. The constructor waits until the page is really displayed, so a test cannot use a LoginPage before it exists. There are two public methods, because logging in successfully and failing to log in lead to different pages, and the return type makes that explicit. And all finding happens inside methods, at the moment the element is needed, so nothing goes stale after a re-render.
3Writing the Test That Uses It
The test now reads almost like a description of the scenario. It creates the page, performs the intention, and asserts on what it gets back.
- LoginTestthe scenario and the assertions
- LoginPage, DashboardPageintentions, locators, waits, navigation
- WebDriverfinding elements and sending commands
- Browserthe real login form
4 layers, from the top down. Step through them one at a time.
Notice there is not a single locator or wait in the test. If the login form is redesigned, the test does not change; only LoginPage does.
4What Belongs in a Page Object
A page object should contain the locators for its page, kept private; methods that express user intentions, such as loginAs, searchFor or addToCart, rather than low-level actions like typeIntoField3; the waits that make those intentions reliable; and navigation, by returning the page object for the page the user arrives at. It may also expose read-only state the test needs to check, such as errorMessage(), greeting() or isRememberMeChecked().
Keeping waits inside the methods matters more than it looks. Every test that logs in now gets the same correct waiting, and tests stay free of synchronization code, which is the code most likely to be written wrongly when copied.
5What Should Not Be in a Page Object
Assertions do not belong in page objects. A page object that calls assertEquals decides what a test is checking, which makes it impossible to reuse the same method in a test with different expectations, and it hides the test's intent inside a helper. The page object reports state; the test judges it.
We also keep out test data creation, such as making a user through the API, which belongs in fixtures or builders, and any knowledge of other pages beyond the page object it returns. And we avoid exposing WebElement fields publicly, because then tests start calling click() on raw elements and the abstraction leaks.
6Why Plain By Fields Instead of PageFactory
Selenium includes PageFactory, which fills fields annotated with @FindBy with proxy elements. It looks tidy, but it is optional and has pitfalls: @CacheLookup fields go stale after a re-render, lists of elements contain ordinary references that go stale, and the proxy hides when the lookup actually happens, which makes timing problems harder to understand. The Selenium project itself recommends against relying on it.
Plain By fields and findElement calls inside methods are more explicit, need no initialization step, and make it obvious that each action looks the element up fresh. For new code they are the simpler choice.
7Follow-up questions
How do you model a component that appears on many pages, such as a header?
As a component object rather than as part of each page. A HeaderComponent class takes the driver, or better a root element or root locator for the header, and exposes intentions such as openProfileMenu(), signOut() or searchFor(text). Each page object that has a header holds a HeaderComponent and exposes it through a method like header(). Tests then write dashboard.header().signOut(), and if the header changes, only HeaderComponent changes. Scoping the component to its root means its locators search only inside the header, so they cannot accidentally match a similar element elsewhere on the page.
Should loginAs return void and let the test create the next page?
Returning the next page object is better, because it encodes the navigation in one place and lets the next page's constructor wait for that page to load. Tests then cannot accidentally use a page they are not on, and they read as a chain of user steps.
Is it fine to have a fluent method for each field, like enterUsername?
It is fine for tests that need fine control, such as checking validation on a single field. But the main methods should be intentions, because most tests only care that the user logged in, not how many fields there were.
8Conclusion
In this answer, we wrote a LoginPage that keeps private By locators, waits for the page in its constructor, and exposes intentions, loginAs returning a DashboardPage and loginExpectingError returning itself, plus read-only state like errorMessage(). The test made the assertions and contained no locators or waits. We saw that locators, waits and navigation belong in page objects, assertions and test data setup do not, and that plain By fields are simpler and safer than PageFactory.
Open with
I would write LoginPage with the WebDriver, a WebDriverWait, and private By fields for the username field, the password field, the submit button and the error message, ideally using ids or data-testid attributes.
Then, if they want more
The constructor waits until the username field is visible, so we know we are really on the page. The main public method is loginAs(user, password): it types both values, waits for the submit button to be clickable, clicks it, and returns new DashboardPage(driver), whose constructor waits for the dashboard heading.
For the failure case I would add loginExpectingError, which waits for the error message and returns the same LoginPage, and a read-only errorMessage() method. The test then has no locators or waits at all: new LoginPage(driver).loginAs("ana", "secret") and then an assertEquals on the dashboard's greeting, or loginExpectingError and an assertion on errorMessage().
What belongs in the page object is the locators, kept private, the waits, intention-level methods, and navigation by returning the next page object. What does not belong is assertions, because the test should decide what is checked; test data setup, which goes in fixtures; and public WebElement fields, which leak the abstraction.
I would use plain By fields rather than PageFactory, because PageFactory proxies hide when lookups happen and cached or list elements can still go stale. For something shared like a header, I would write a HeaderComponent scoped to its root element and compose it into each page object.
About this answer
- Last updated
- 12 November 2026
- 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 page object for a login page with username, password and a submit button, and a...": https://scrivio-interview-library.netlify.app/java/ui-testing/9d58ad0386/