Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): add copy paste e2e tests #6935

Merged
merged 29 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2f8e5ba
test(form): add e2e tests for copy paste
sjelfull Jun 13, 2024
facc156
feat(form): add test ids to field actions
sjelfull Jun 17, 2024
a585b56
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 17, 2024
24216d9
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 17, 2024
926e0e1
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 17, 2024
7df89a5
fix(form): remove blur handling from object
sjelfull Jun 18, 2024
5f54d71
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 18, 2024
d543a65
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 18, 2024
aba5f5c
fixup! test(form): add e2e tests for copy paste
sjelfull Jun 18, 2024
4f3e607
fix(form): trigger keypress on object wrapper
sjelfull Jun 19, 2024
389ef1f
test(form): attempt to stabilise e2e tests for copy paste
sjelfull Jun 19, 2024
ae47529
chore: upgrade playwright deps
sjelfull Jun 20, 2024
11a7032
fixup! chore: upgrade playwright deps
sjelfull Jun 20, 2024
ff93ca5
fixup! chore: upgrade playwright deps
sjelfull Jun 20, 2024
69b1188
test(form): add component tests for copy paste
sjelfull Jun 20, 2024
52f4248
fix(test): make sure fixure awaits setup
sjelfull Jun 21, 2024
60a84b9
fixup! test(form): add component tests for copy paste
sjelfull Jun 21, 2024
99157c2
fix(test): allow. copy pasting permissions in playwright-ct config
sjelfull Jun 21, 2024
54c6e82
fixup! test(form): add component tests for copy paste
sjelfull Jun 21, 2024
2929d4a
test(form): stabilise copy paste e2e tests
sjelfull Jun 21, 2024
82a635f
fixup! test(form): stabilise copy paste e2e tests
sjelfull Jun 21, 2024
b01f5c2
fix(test): fix clipboard.writeText mock
sjelfull Jun 21, 2024
7d52000
refactor(test): remove e2e copypaste test in favour of component test
sjelfull Jun 21, 2024
0209983
fix(test): fix assertion texts
sjelfull Jun 21, 2024
78aab54
refactor(form): align updated toast msg with the copy toast
sjelfull Jun 21, 2024
1352ebb
fix(test): await filling out string inputs
sjelfull Jun 24, 2024
08c6805
chore(test): enable trace/video on retries in CI
sjelfull Jun 24, 2024
0da43bd
feat(playwright-ct): add debug fixture
sjelfull Jun 25, 2024
ca010d7
fix(test): more work to stabilise object tests
sjelfull Jun 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"@bjoerge/mutiny": "^0.5.3",
"@google-cloud/storage": "^7.11.0",
"@jest/globals": "^29.7.0",
"@playwright/test": "1.41.2",
"@playwright/test": "1.44.1",
"@repo/package.config": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@sanity/client": "^6.20.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/portable-text-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@playwright/test": "1.41.2",
"@playwright/test": "1.44.1",
"@portabletext/toolkit": "^2.0.15",
"@repo/package.config": "workspace:*",
"@sanity/diff-match-patch": "^3.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@
"devDependencies": {
"@jest/expect": "^29.7.0",
"@jest/globals": "^29.7.0",
"@playwright/experimental-ct-react": "1.41.2",
"@playwright/test": "1.41.2",
"@playwright/experimental-ct-react": "1.44.1",
"@playwright/test": "1.44.1",
"@repo/package.config": "workspace:*",
"@sanity/codegen": "3.46.1",
"@sanity/generate-help-url": "^3.0.0",
Expand Down
29 changes: 26 additions & 3 deletions packages/sanity/playwright-ct.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {defineConfig, devices} from '@playwright/experimental-ct-react'
const TESTS_PATH = path.join(__dirname, 'playwright-ct', 'tests')
const HTML_REPORT_PATH = path.join(__dirname, 'playwright-ct', 'report')
const ARTIFACT_OUTPUT_PATH = path.join(__dirname, 'playwright-ct', 'results')
const isCI = !!process.env.CI

/**
* See https://playwright.dev/docs/test-configuration.
Expand Down Expand Up @@ -47,7 +48,8 @@ export default defineConfig({
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 40 * 1000,
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
trace: isCI ? 'on-all-retries' : 'retain-on-failure',
video: isCI ? 'on-first-retry' : 'retain-on-failure',
/* Port to use for Playwright component endpoint. */
ctPort: 3100,
/* Configure Playwright vite config */
Expand All @@ -69,8 +71,29 @@ export default defineConfig({

/* Configure projects for major browsers */
projects: [
{name: 'chromium', use: {...devices['Desktop Chrome']}},
{name: 'firefox', use: {...devices['Desktop Firefox']}},
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
permissions: ['clipboard-read', 'clipboard-write'],
contextOptions: {
// chromium-specific permissions
permissions: ['clipboard-read', 'clipboard-write'],
},
},
},
{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
launchOptions: {
firefoxUserPrefs: {
'dom.events.asyncClipboard.readText': true,
'dom.events.testing.asyncClipboard': true,
},
},
},
},
{name: 'webkit', use: {...devices['Desktop Safari']}},
],
})
89 changes: 89 additions & 0 deletions packages/sanity/playwright-ct/tests/fixtures/copyPasteFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {test as base} from '@playwright/experimental-ct-react'

export const test = base.extend<{
getClipboardItemByMimeTypeAsText: (mimeType: string) => Promise<string | null>
setClipboardItems: (items: ClipboardItem[]) => Promise<void>
getClipboardItems: () => Promise<ClipboardItem[]>
getClipboardItemsAsText: () => Promise<string>
}>({
page: async ({page}, use) => {
const setupClipboardMocks = async () => {
await page.addInitScript(() => {
const mockClipboard = {
read: () => {
return Promise.resolve((window as any).__clipboardItems)
},
write: (newItems: ClipboardItem[]) => {
;(window as any).__clipboardItems = newItems

return Promise.resolve()
},
readText: () => {
const items = (window as any).__clipboardItems as ClipboardItem[]
const textItem = items.find((item) => item.types.includes('text/plain'))
return textItem
? textItem.getType('text/plain').then((blob: Blob) => blob.text())
: Promise.resolve('')
},
writeText: (text: string) => {
const textBlob = new Blob([text], {type: 'text/plain'})
;(window as any).__clipboardItems = [new ClipboardItem({'text/plain': textBlob})]
return Promise.resolve()
},
}
Object.defineProperty(Object.getPrototypeOf(navigator), 'clipboard', {
value: mockClipboard,
writable: false,
})
;(window as any).__clipboardItems = []
})
}

await setupClipboardMocks()

page.on('framenavigated', async () => {
await setupClipboardMocks()
})

await use(page)
},

setClipboardItems: async ({page}, use) => {
await use(async (items: ClipboardItem[]) => {
;(window as any).__clipboardItems = items
})
},

getClipboardItems: async ({page}, use) => {
await use(() => {
return page.evaluate(() => navigator.clipboard.read())
})
},

getClipboardItemsAsText: async ({page}, use) => {
await use(async () => {
return page.evaluate(async () => {
const items = await navigator.clipboard.read()
const textItem = items.find((item) => item.types.includes('text/plain'))

return textItem
? textItem.getType('text/plain').then((blob: Blob) => blob.text())
: Promise.resolve('')
})
})
},

getClipboardItemByMimeTypeAsText: async ({page}, use) => {
await use(async (mimeType: string) => {
return page.evaluate(async (mime) => {
const items = await navigator.clipboard.read()
const textItem = items.find((item) => item.types.includes(mime))
const content = textItem ? textItem.getType(mime).then((blob: Blob) => blob.text()) : null

return content
}, mimeType)
})
},
})

export const {expect} = test
31 changes: 31 additions & 0 deletions packages/sanity/playwright-ct/tests/fixtures/debugFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {test as base} from '@playwright/experimental-ct-react'

export const test = base.extend<{
logActiveElement: () => Promise<{
tagName: string
id: string
className: string
name: string
attributes: Record<string, string>
}>
}>({
logActiveElement: async ({page}, use) => {
await use(async () => {
const activeElementInfo = await page.evaluate(() => {
const active = document.activeElement as HTMLElement
return {
tagName: active.tagName,
id: active.id,
className: active.className,
name: active.nodeName,
attributes: Object.fromEntries(
Array.from(active.attributes).map((attr) => [attr.name, attr.value]),
),
}
})
return Promise.resolve(activeElementInfo)
})
},
})

export {expect} from '@playwright/test'
10 changes: 10 additions & 0 deletions packages/sanity/playwright-ct/tests/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {test as base} from '@playwright/experimental-ct-react'
import {mergeTests} from '@playwright/test'

import {test as copyPasteFixture} from './copyPasteFixture'
import {test as debugFixture} from './debugFixture'
import {test as scrollToTopFixture} from './scrollToTopFixture'

export const test = mergeTests(base, copyPasteFixture, scrollToTopFixture, debugFixture)

export {expect} from '@playwright/test'
20 changes: 20 additions & 0 deletions packages/sanity/playwright-ct/tests/fixtures/scrollToTopFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {expect, type Locator, test as baseTest} from '@playwright/test'

type ScrollToTop = (locator: Locator) => Promise<void>

export const test = baseTest.extend<{
scrollToTop: ScrollToTop
}>({
scrollToTop: async ({page}, use) => {
const scrollToTop: ScrollToTop = async (locator: Locator) => {
await locator.evaluate((element) => {
element.scrollIntoView({block: 'start', inline: 'nearest'})
})

const boundingBox = await locator.boundingBox()
await expect(boundingBox?.y).toBeLessThanOrEqual(1)
}

await use(scrollToTop)
},
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {expect, test} from '@playwright/experimental-ct-react'

import {expect, test} from '../../../fixtures'
import {testHelpers} from '../../../utils/testHelpers'
import {ObjectBlockStory} from './ObjectBlockStory'

Expand Down Expand Up @@ -140,19 +139,20 @@ test.describe('Portable Text Input', () => {
await expect($closeButton.or($closeButtonSvg).first()).toBeFocused()

// Tab to the input
await page.keyboard.press('Tab')

const $dialogInput = await page.getByTestId('default-edit-object-dialog').locator('input')
await page.keyboard.press('Tab+Tab')

// Assertion: Dialog should not be closed when you tab to input
await expect($dialog).not.toBeHidden()
await expect(page.getByTestId('default-edit-object-dialog')).not.toBeHidden()

// Check that we have focus on the input
await expect($dialogInput).toBeFocused()
await expect(page.getByTestId('default-edit-object-dialog').locator('input')).toBeFocused()

// Assertion: Focus should be locked
await page.keyboard.press('Tab+Tab')
await expect($dialogInput).toBeFocused()
await page.keyboard.press('Tab+Tab+Tab')

await expect(page.getByTestId('default-edit-object-dialog')).not.toBeHidden()

await expect(page.getByTestId('default-edit-object-dialog').locator('input')).toBeFocused()
})

test('Blocks that appear in the menu bar should always display a title', async ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable max-nested-callbacks */
import path from 'node:path'

import {expect, test} from '@playwright/experimental-ct-react'
// import {expect, test} from '@playwright/experimental-ct-react'
import {type Path, type SanityDocument} from '@sanity/types'

import {expect, test} from '../../../../fixtures'
import {testHelpers} from '../../../../utils/testHelpers'
import CopyPasteStory from './CopyPasteStory'
import {
Expand Down
Loading
Loading