|
| 1 | +import { Locator, Page, expect } from '@playwright/test' |
| 2 | + |
| 3 | +export class Pagination { |
| 4 | + private page: Page |
| 5 | + private paginationDataTestId: string |
| 6 | + |
| 7 | + private nextButton: Locator |
| 8 | + private previousButton: Locator |
| 9 | + private pagination: Locator |
| 10 | + |
| 11 | + constructor(page: Page, paginationDataTestId: string) { |
| 12 | + this.page = page |
| 13 | + this.paginationDataTestId = paginationDataTestId |
| 14 | + } |
| 15 | + |
| 16 | + private async getPagination(): Promise<Locator> { |
| 17 | + if (!this.pagination) { |
| 18 | + this.pagination = await this.page.getByTestId(this.paginationDataTestId) |
| 19 | + } |
| 20 | + return this.pagination |
| 21 | + } |
| 22 | + |
| 23 | + private async getNextButton(): Promise<Locator> { |
| 24 | + if (!this.nextButton) { |
| 25 | + const pagination = await this.getPagination() |
| 26 | + this.nextButton = await pagination.locator('button').last() |
| 27 | + } |
| 28 | + return this.nextButton |
| 29 | + } |
| 30 | + |
| 31 | + private async getPreviousButton(): Promise<Locator> { |
| 32 | + if (!this.previousButton) { |
| 33 | + const pagination = await this.getPagination() |
| 34 | + this.previousButton = await pagination.locator('button').first() |
| 35 | + } |
| 36 | + return this.previousButton |
| 37 | + } |
| 38 | + |
| 39 | + public async getFrom(): Promise<number> { |
| 40 | + const pagination = await this.getPagination() |
| 41 | + const from = await (await pagination.getByTestId('from')).innerText() |
| 42 | + return Number.parseInt(from) |
| 43 | + } |
| 44 | + |
| 45 | + public async getTo(): Promise<number> { |
| 46 | + const pagination = await this.getPagination() |
| 47 | + const to = await (await pagination.getByTestId('to')).innerText() |
| 48 | + return Number.parseInt(to) |
| 49 | + } |
| 50 | + |
| 51 | + public async getCount(): Promise<number> { |
| 52 | + const pagination = await this.getPagination() |
| 53 | + const count = await (await pagination.getByTestId('count')).innerText() |
| 54 | + return Number.parseInt(count) |
| 55 | + } |
| 56 | + |
| 57 | + public async getRowsPerPage(): Promise<number> { |
| 58 | + const pagination = await this.getPagination() |
| 59 | + const input = await pagination.locator('input') |
| 60 | + |
| 61 | + return Number.parseInt(await input.inputValue()) |
| 62 | + } |
| 63 | + |
| 64 | + public async changeRowsPerPage(rowsCount: 10 | 25 | 50): Promise<void> { |
| 65 | + const pagination = await this.getPagination() |
| 66 | + const input = await pagination.locator('input') |
| 67 | + const dropdown = await pagination.locator('.MuiInputBase-root') |
| 68 | + await dropdown.click() |
| 69 | + |
| 70 | + const ul = await this.page.locator('#menu- .MuiList-root') |
| 71 | + const option = await ul |
| 72 | + .locator('li') |
| 73 | + .filter({ hasText: rowsCount.toString() }) |
| 74 | + |
| 75 | + await option.click() |
| 76 | + await expect(input).toHaveValue(rowsCount.toString()) |
| 77 | + await expect(await this.getTo()).toEqual(rowsCount) |
| 78 | + } |
| 79 | + |
| 80 | + public async goToNextPage(): Promise<boolean> { |
| 81 | + const nextButton = await this.getNextButton() |
| 82 | + if (await nextButton.isDisabled()) { |
| 83 | + return false |
| 84 | + } |
| 85 | + await nextButton.click() |
| 86 | + return true |
| 87 | + } |
| 88 | + |
| 89 | + public async goToPreviousPage(): Promise<boolean> { |
| 90 | + const previousButton = await this.getPreviousButton() |
| 91 | + if (await previousButton.isDisabled()) { |
| 92 | + return false |
| 93 | + } |
| 94 | + await previousButton.click() |
| 95 | + return true |
| 96 | + } |
| 97 | +} |
0 commit comments