|
| 1 | +import { expect, test, type Page } from '@playwright/test' |
| 2 | +import i18next from 'i18next' |
| 3 | +import { getPastDate, loadTranslate, waitForSkeletonsToHide } from './utils' |
| 4 | +import { operatorList } from 'src/pages/operator/data' |
| 5 | + |
| 6 | +const getLabelValue = async (label: string, page: Page) => { |
| 7 | + const row = page.locator('table tr', { hasText: label }) |
| 8 | + return await row.locator('td').nth(1).textContent() |
| 9 | +} |
| 10 | + |
| 11 | +const selectRandomOperator = async (label: string, page: Page) => { |
| 12 | + await page.getByRole('combobox', { name: label }).click() |
| 13 | + const options = page.getByRole('option') |
| 14 | + const optionsCount = await options.count() |
| 15 | + const randomIndex = Math.floor(Math.random() * optionsCount) |
| 16 | + await options.nth(randomIndex).scrollIntoViewIfNeeded() |
| 17 | + await options.nth(randomIndex).click() |
| 18 | + return options.nth(randomIndex) |
| 19 | +} |
| 20 | + |
| 21 | +test.describe('Operator Page Tests', () => { |
| 22 | + test.beforeEach(async ({ page }) => { |
| 23 | + await page.route(/google-analytics\.com|googletagmanager\.com/, (route) => route.abort()) |
| 24 | + await loadTranslate(i18next) |
| 25 | + await page.clock.setFixedTime(getPastDate()) |
| 26 | + await page.goto('/') |
| 27 | + await page |
| 28 | + .getByText(i18next.t('operator_title'), { exact: true }) |
| 29 | + .and(page.getByRole('link')) |
| 30 | + .click() |
| 31 | + await page.getByRole('progressbar').waitFor({ state: 'hidden' }) |
| 32 | + await test.step('Validate that the test is on the correct page', async () => { |
| 33 | + await expect(page).toHaveURL(/operator/) |
| 34 | + await expect(page.locator('h4')).toHaveText(i18next.t('operator_title')) |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + test('Test operator inputs', async ({ page }) => { |
| 39 | + await test.step('Validate inputs are disabled when no operator is selected', async () => { |
| 40 | + await expect(page.getByRole('combobox', { name: i18next.t('choose_operator') })).toBeEmpty() |
| 41 | + await expect(page.getByRole('textbox', { name: i18next.t('choose_date') })).toBeDisabled() |
| 42 | + await expect( |
| 43 | + page.getByRole('button', { name: i18next.t('operator.time_range.day') }), |
| 44 | + ).toBeDisabled() |
| 45 | + await expect( |
| 46 | + page.getByRole('button', { name: i18next.t('operator.time_range.week') }), |
| 47 | + ).toBeDisabled() |
| 48 | + await expect( |
| 49 | + page.getByRole('button', { name: i18next.t('operator.time_range.month') }), |
| 50 | + ).toBeDisabled() |
| 51 | + }) |
| 52 | + |
| 53 | + await test.step('Select a random operator', async () => { |
| 54 | + await selectRandomOperator(i18next.t('choose_operator'), page) |
| 55 | + }) |
| 56 | + |
| 57 | + await test.step('Validate inputs are enabled after selecting an operator', async () => { |
| 58 | + await expect(page.getByRole('textbox', { name: i18next.t('choose_date') })).toBeEnabled() |
| 59 | + await expect( |
| 60 | + page.getByRole('button', { name: i18next.t('operator.time_range.day') }), |
| 61 | + ).toBeEnabled() |
| 62 | + await expect( |
| 63 | + page.getByRole('button', { name: i18next.t('operator.time_range.day') }), |
| 64 | + ).toHaveAttribute('aria-pressed', 'true') |
| 65 | + await expect( |
| 66 | + page.getByRole('button', { name: i18next.t('operator.time_range.week') }), |
| 67 | + ).toBeEnabled() |
| 68 | + await expect( |
| 69 | + page.getByRole('button', { name: i18next.t('operator.time_range.month') }), |
| 70 | + ).toBeEnabled() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test('Test operator info card', async ({ page }) => { |
| 75 | + await test.step('Select a random operator', async () => { |
| 76 | + await selectRandomOperator(i18next.t('choose_operator'), page) |
| 77 | + }) |
| 78 | + await test.step('Validate operator info card details', async () => { |
| 79 | + const id = await getLabelValue(i18next.t('operator.ref'), page) |
| 80 | + if (!id) { |
| 81 | + throw new Error('Operator ID not found in operator info card') |
| 82 | + } |
| 83 | + const operator = operatorList.find((op) => op.ref === id) |
| 84 | + if (!operator) { |
| 85 | + throw new Error(`Operator with ID ${id} not found in operator list`) |
| 86 | + } |
| 87 | + const founded = await getLabelValue(i18next.t('operator.founded'), page) |
| 88 | + if (founded !== '-') { |
| 89 | + expect(Number(founded)).toEqual(operator.founded) |
| 90 | + } |
| 91 | + const area = await getLabelValue(i18next.t('operator.area_title'), page) |
| 92 | + if (area !== '-') { |
| 93 | + expect(area).toEqual(i18next.t(`operator.area.${operator.area}`)) |
| 94 | + } |
| 95 | + const type = await getLabelValue(i18next.t('operator.type_title'), page) |
| 96 | + if (type !== '-') { |
| 97 | + expect(type).toEqual(i18next.t(`operator.type.${operator.type}`)) |
| 98 | + } |
| 99 | + const website = await getLabelValue(i18next.t('operator.website'), page) |
| 100 | + if (website !== '-') { |
| 101 | + expect(website).toEqual(operator.website) |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + test('Test operator gaps card', async ({ page }) => { |
| 107 | + await test.step('Select a random operator', async () => { |
| 108 | + await selectRandomOperator(i18next.t('choose_operator'), page) |
| 109 | + }) |
| 110 | + |
| 111 | + await test.step('Validate operator gaps', async () => { |
| 112 | + await waitForSkeletonsToHide(page) |
| 113 | + const planned = await getLabelValue(i18next.t('rides_planned'), page) |
| 114 | + const actual = await getLabelValue(i18next.t('rides_actual'), page) |
| 115 | + const missing = await getLabelValue(i18next.t('rides_missing'), page) |
| 116 | + if (planned !== '-' && actual !== '-' && missing !== '-') { |
| 117 | + expect(Number(missing)).toEqual(Number(planned) - Number(actual)) |
| 118 | + } else { |
| 119 | + throw new Error('Operator gaps not loaded') |
| 120 | + } |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + test('Test operator routes card', async ({ page }) => { |
| 125 | + await test.step('Select a random operator', async () => { |
| 126 | + await selectRandomOperator(i18next.t('choose_operator'), page) |
| 127 | + }) |
| 128 | + |
| 129 | + await test.step('Validate operator routes', async () => { |
| 130 | + await waitForSkeletonsToHide(page) |
| 131 | + const table = page.locator('table').nth(2) |
| 132 | + const rows = table.locator('tbody tr') |
| 133 | + const totalText = await page.getByText(i18next.t('operator.total')).textContent() |
| 134 | + const total = Number(totalText?.split(' ')[i18next.language === 'en' ? 2 : 3] || 0) |
| 135 | + if (total !== 0) { |
| 136 | + const rowsCount = await rows.count() |
| 137 | + expect(rowsCount).toEqual(total) |
| 138 | + } else { |
| 139 | + throw new Error('Operator routes not loaded') |
| 140 | + } |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments