|
| 1 | +import { BrowserContext, chromium, expect, test } from '@playwright/test' |
| 2 | +import path from 'path' |
| 3 | +import sinon from 'sinon' |
| 4 | + |
| 5 | +test.describe('Adding an item', () => { |
| 6 | + let context: BrowserContext |
| 7 | + test.beforeAll(async () => { |
| 8 | + const browser = await chromium.launch() // Or 'firefox' or 'webkit'. |
| 9 | + context = await browser.newContext({ |
| 10 | + locale: 'no-NO', |
| 11 | + timezoneId: 'Europe/Berlin', |
| 12 | + }) |
| 13 | + // See https://github.com/microsoft/playwright/issues/6347#issuecomment-965887758 |
| 14 | + await context.addInitScript({ |
| 15 | + path: path.join(process.cwd(), 'node_modules/sinon/pkg/sinon.js'), |
| 16 | + }) |
| 17 | + // Auto-enable sinon right away |
| 18 | + // and enforce our "current" date |
| 19 | + await context.addInitScript(() => { |
| 20 | + const clock = sinon.useFakeTimers() |
| 21 | + clock.setSystemTime(new Date('2022-03-11T12:00:00Z')) |
| 22 | + ;(window as any).__clock = clock |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + test('should allow me to add todo items', async () => { |
| 27 | + const page = await context.newPage() |
| 28 | + await page.goto('http://localhost:8080/') |
| 29 | + |
| 30 | + // Create a new entry |
| 31 | + await page.locator('button[name="edit-schedule"]').click() |
| 32 | + await page.locator('input[name="conference-day"]').fill('2022-03-12') |
| 33 | + await page |
| 34 | + .locator('select[name="conference-timezone"]') |
| 35 | + .selectOption('Asia/Tokyo') |
| 36 | + await page.locator('input[name="session-hour"]').fill('9') |
| 37 | + await page.locator('input[name="session-minute"]').fill('45') |
| 38 | + |
| 39 | + const sessionName = 'Breakfast for Champions' |
| 40 | + const sessionNameInput = page.locator('input[name="session-name"]') |
| 41 | + await sessionNameInput.fill(sessionName) |
| 42 | + await sessionNameInput.press('Enter') |
| 43 | + |
| 44 | + await page.locator('button[name="save-schedule"]').click() |
| 45 | + |
| 46 | + // Table should have the new entry |
| 47 | + await expect(page.locator('table tbody')).toContainText(sessionName) |
| 48 | + |
| 49 | + // Find the row of the new entry |
| 50 | + const tr = page |
| 51 | + .locator(`table tbody td:has-text("${sessionName}")`) // td |
| 52 | + .locator(`xpath=ancestor::tr`) |
| 53 | + const conferenceTime = tr.locator('td:first-child') |
| 54 | + await expect(conferenceTime).toContainText('09:45') |
| 55 | + |
| 56 | + // Time Zone difference between Berlin and Tokyo on 2022-03-12 is 8 hours |
| 57 | + |
| 58 | + const localTime = tr.locator('td:nth-child(2)') |
| 59 | + await expect(localTime).toContainText('01:45') |
| 60 | + }) |
| 61 | + |
| 62 | + test('should display the users time zone', async () => { |
| 63 | + const page = await context.newPage() |
| 64 | + await page.goto('http://localhost:8080/') |
| 65 | + |
| 66 | + await expect( |
| 67 | + page.locator('table thead th:nth-child(2) small'), |
| 68 | + ).toContainText('Berlin') |
| 69 | + }) |
| 70 | +}) |
0 commit comments