I want to use Playwright's Locators in my page objects. I found an example for Javascript (stripped for brevity):
import { Page } from '@playwright/test';
export class TodoPage {
listItems = this.page.locator('.todo-list li');
constructor(public readonly page: Page) { }
}
Trying to do the same in my Java code:
public class LoginPage {
private Page page;
private Locator loginButton = this.page.locator("#loginButton");
public LoginPage(Page page){
this.page = page;
}
}
throws a null pointer exception, because page
is not yet initiated, when initializing loginButton
.
I could do
private Locator loginButton;
public LoginPage(Page page){
this.page = page;
loginButton = this.page.locator("#loginButton");
}
but this would become kind of lenghty/ messy, for large page object classes.
Any ideas on how to do that in Java?
Thanks!