Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active February 3, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadvolod/671879f622181219478ff86a1104daf7 to your computer and use it in GitHub Desktop.
Save nadvolod/671879f622181219478ff86a1104daf7 to your computer and use it in GitHub Desktop.
Is this a good page object using playwright and typescript?
export class LoginPage {
readonly page: Page;
readonly userNameTextbox: Locator
readonly passwordTextbox: Locator
readonly loginBtn: Locator
// Clarity over duplication removal
// Don't start with the abstraction
constructor(page: Page) {
this.page = page;
this.userNameLocator = { name: 'Username' };
// this.userNameTextbox = page.getByRole('textbox', { name: 'Username' })
this.passwordTextbox = page.getByRole('textbox', { name: 'Password' })
this.loginBtn = page.getByRole('button', { name: 'Log In' })
}
async setUsername(txt: string): Promise<void> {
await page.getByRole('textbox', this.userNameLocator).click()
await this.userNameTextbox.type(txt)
}
// prefer not to do this as it allows a low-level interaction in the tests
async inputPasswordTextbox(txt: string): Promise<void> {
await this.passwordTextbox.click()
await this.passwordTextbox.type(txt)
}
// don't mix with API
async login(username: string, password: string): Promise<void> {
await page.getByRole('textbox', this.userNameLocator).click()
await this.userNameTextbox.type(username)
await this.passwordTextbox.click()
await this.passwordTextbox.type(password)
}
async loginSuccessfully(username: string, password: string): Promise<void> {
await login(username, password);
// can now throw a custom exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment