|
| 1 | +import * as playwright from '@playwright/test' |
| 2 | +import * as path from 'path' |
| 3 | +import { |
| 4 | + locatorFixtures as fixtures, |
| 5 | + LocatorFixtures as TestingLibraryFixtures, |
| 6 | +} from '../../lib/fixture' |
| 7 | + |
| 8 | +const test = playwright.test.extend<TestingLibraryFixtures>(fixtures) |
| 9 | + |
| 10 | +const {expect} = test |
| 11 | + |
| 12 | +test.describe('lib/fixture.ts (locators)', () => { |
| 13 | + test.beforeEach(async ({page}) => { |
| 14 | + await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`) |
| 15 | + }) |
| 16 | + |
| 17 | + test('should handle the query* methods', async ({queries: {queryByText}}) => { |
| 18 | + const element = queryByText('Hello h1') |
| 19 | + |
| 20 | + expect(element).toBeTruthy() |
| 21 | + expect(await element.textContent()).toEqual('Hello h1') |
| 22 | + }) |
| 23 | + |
| 24 | + test('should use the new v3 methods', async ({queries: {queryByRole}}) => { |
| 25 | + const element = queryByRole('presentation') |
| 26 | + |
| 27 | + expect(element).toBeTruthy() |
| 28 | + expect(await element.textContent()).toContain('Layout table') |
| 29 | + }) |
| 30 | + |
| 31 | + test('should handle regex matching', async ({queries: {queryByText}}) => { |
| 32 | + const element = queryByText(/HeLlO h(1|7)/i) |
| 33 | + |
| 34 | + expect(element).toBeTruthy() |
| 35 | + expect(await element.textContent()).toEqual('Hello h1') |
| 36 | + }) |
| 37 | + |
| 38 | + test('should handle the get* methods', async ({queries: {getByTestId}}) => { |
| 39 | + const element = getByTestId('testid-text-input') |
| 40 | + |
| 41 | + expect(await element.evaluate(el => el.outerHTML)).toMatch( |
| 42 | + `<input type="text" data-testid="testid-text-input">`, |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + test('handles page navigations', async ({queries: {getByText}, page}) => { |
| 47 | + await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`) |
| 48 | + |
| 49 | + const element = getByText('Hello h1') |
| 50 | + |
| 51 | + expect(await element.textContent()).toEqual('Hello h1') |
| 52 | + }) |
| 53 | + |
| 54 | + test('should handle the get* method failures', async ({queries}) => { |
| 55 | + const {getByTitle} = queries |
| 56 | + // Use the scoped element so the pretty HTML snapshot is smaller |
| 57 | + |
| 58 | + await expect(async () => getByTitle('missing').textContent()).rejects.toThrow() |
| 59 | + }) |
| 60 | + |
| 61 | + test('should handle the LabelText methods', async ({queries}) => { |
| 62 | + const {getByLabelText} = queries |
| 63 | + const element = getByLabelText('Label A') |
| 64 | + |
| 65 | + /* istanbul ignore next */ |
| 66 | + expect(await element.evaluate(el => el.outerHTML)).toMatch( |
| 67 | + `<input id="label-text-input" type="text">`, |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + test('should handle the queryAll* methods', async ({queries}) => { |
| 72 | + const {queryAllByText} = queries |
| 73 | + const elements = queryAllByText(/Hello/) |
| 74 | + |
| 75 | + expect(await elements.count()).toEqual(3) |
| 76 | + |
| 77 | + const text = await Promise.all([ |
| 78 | + elements.nth(0).textContent(), |
| 79 | + elements.nth(1).textContent(), |
| 80 | + elements.nth(2).textContent(), |
| 81 | + ]) |
| 82 | + |
| 83 | + expect(text).toEqual(['Hello h1', 'Hello h2', 'Hello h3']) |
| 84 | + }) |
| 85 | + |
| 86 | + test('should handle the queryAll* methods with a selector', async ({queries}) => { |
| 87 | + const {queryAllByText} = queries |
| 88 | + const elements = queryAllByText(/Hello/, {selector: 'h2'}) |
| 89 | + |
| 90 | + expect(await elements.count()).toEqual(1) |
| 91 | + |
| 92 | + expect(await elements.textContent()).toEqual('Hello h2') |
| 93 | + }) |
| 94 | + |
| 95 | + test('should handle the getBy* methods with a selector', async ({queries}) => { |
| 96 | + const {getByText} = queries |
| 97 | + const element = getByText(/Hello/, {selector: 'h2'}) |
| 98 | + |
| 99 | + expect(await element.textContent()).toEqual('Hello h2') |
| 100 | + }) |
| 101 | + |
| 102 | + test('should handle the getBy* methods with a regex name', async ({queries}) => { |
| 103 | + const {getByRole} = queries |
| 104 | + const element = getByRole('button', {name: /getBy.*Test/}) |
| 105 | + |
| 106 | + expect(await element.textContent()).toEqual('getByRole Test') |
| 107 | + }) |
| 108 | + |
| 109 | + test('supports `hidden` option when querying by role', async ({queries: {queryAllByRole}}) => { |
| 110 | + const elements = queryAllByRole('img') |
| 111 | + const hiddenElements = queryAllByRole('img', {hidden: true}) |
| 112 | + |
| 113 | + expect(await elements.count()).toEqual(1) |
| 114 | + expect(await hiddenElements.count()).toEqual(2) |
| 115 | + }) |
| 116 | + |
| 117 | + test.describe('querying by role with `level` option', () => { |
| 118 | + test('retrieves the correct elements when querying all by role', async ({ |
| 119 | + queries: {queryAllByRole}, |
| 120 | + }) => { |
| 121 | + const elements = queryAllByRole('heading') |
| 122 | + const levelOneElements = queryAllByRole('heading', {level: 3}) |
| 123 | + |
| 124 | + expect(await elements.count()).toEqual(3) |
| 125 | + expect(await levelOneElements.count()).toEqual(1) |
| 126 | + }) |
| 127 | + |
| 128 | + test('does not throw when querying for a specific element', async ({queries: {getByRole}}) => { |
| 129 | + expect.assertions(1) |
| 130 | + |
| 131 | + await expect(getByRole('heading', {level: 3}).textContent()).resolves.not.toThrow() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + // TODO: scoped queries with `within` |
| 136 | + // TODO: configuration |
| 137 | + // TDOO: deferred page (do we need some alternative to `findBy*`?) |
| 138 | +}) |
0 commit comments