Last active
February 3, 2023 19:00
-
-
Save nadvolod/671879f622181219478ff86a1104daf7 to your computer and use it in GitHub Desktop.
Is this a good page object using playwright and typescript?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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