1+ import { expect , Page } from '@playwright/test' ;
2+ import { OTP } from 'otplib' ;
3+
4+ export class LoginFixture {
5+ constructor ( public readonly page : Page ) { }
6+
7+ async run ( username : string , password : string ) {
8+ await expect ( this . page . getByText ( 'Welcome back, please login' ) ) . toBeVisible ( ) ;
9+ await this . page . getByLabel ( 'Username' ) . fill ( username ) ;
10+ await this . page . getByLabel ( 'Password' ) . fill ( password ) ;
11+ await this . page . getByRole ( 'button' , { name : 'Login' } ) . click ( ) ;
12+ }
13+
14+ async expectSuccess ( username : string ) {
15+ await expect ( this . page . getByText ( `You are currently logged in as ${ username } .` ) ) . toBeVisible ( )
16+ }
17+ }
18+
19+ export class LogoutFixture {
20+ constructor ( public readonly page : Page ) { }
21+
22+ async run ( ) {
23+ await expect ( this . page . getByText ( 'Click the button below to logout.' ) ) . toBeVisible ( ) ;
24+ await this . page . getByRole ( 'button' , { name : 'Logout' } ) . click ( ) ;
25+ }
26+
27+ async expectSuccess ( ) {
28+ await expect ( this . page . getByText ( 'Welcome back, please login' ) ) . toBeVisible ( ) ;
29+ }
30+ }
31+
32+ export class TOTPFixture {
33+ constructor ( public readonly page : Page ) { }
34+
35+ async run ( secret : string ) {
36+ await expect ( this . page . getByText ( 'Enter your TOTP code' ) ) . toBeVisible ( ) ;
37+ const otp = new OTP ( ) ;
38+ const token = await otp . generate ( { secret } ) ;
39+ await this . page . getByPlaceholder ( 'XXXXXX' ) . fill ( token ) ;
40+ // we shouldn't need to click continue, it will auto submit
41+ // await this.page.getByRole('button', { name: 'Continue' }).click();
42+ }
43+
44+ async expectSuccess ( username : string ) {
45+ await expect ( this . page . getByText ( `You are currently logged in as ${ username } .` ) ) . toBeVisible ( )
46+ }
47+ }
0 commit comments