Skip to content

Commit bb801c5

Browse files
mejo-max-nextcloud
authored andcommitted
test(playwright): test that all images are loaded in print view
Signed-off-by: Jonas <jonas@freesources.org>
1 parent 6fcbb3d commit bb801c5

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { expect } from '@playwright/test'
7+
import { test } from '../support/fixtures/create-collectives.ts'
8+
9+
test.describe('Collective print view', () => {
10+
test('loads all images before opening print dialog', async ({ user, page, collective }) => {
11+
// Create two pages each with an image. The second page is below initial viewport.
12+
for (let i = 0; i < 2; i++) {
13+
const collectivePage = await collective.createPage({
14+
title: `Page with image ${i}`,
15+
user,
16+
page,
17+
})
18+
const src = await collectivePage.uploadImage({
19+
filename: 'test.png',
20+
user,
21+
page,
22+
})
23+
await collectivePage.setContent({
24+
content: `# Image ${i}\n\n![image ${i}](${src})\n`,
25+
user,
26+
page,
27+
})
28+
}
29+
30+
// Stub window.print so the real print dialog doesn't block the test
31+
await page.addInitScript(() => {
32+
(window as Window & { __printCalls?: number }).__printCalls = 0
33+
window.print = () => {
34+
(window as Window & { __printCalls?: number }).__printCalls! += 1
35+
}
36+
})
37+
38+
await page.goto(`/index.php/apps/collectives/_/print/${collective.getCollectiveUrlPart()}`)
39+
40+
await expect(page.getByText('Preparing collective for exporting or printing'))
41+
.toBeVisible()
42+
await expect(page.getByText('Preparing collective for exporting or printing'))
43+
.toBeHidden({ timeout: 30000 })
44+
45+
const printCalls = await page.evaluate(() => (window as Window & { __printCalls?: number }).__printCalls)
46+
expect(printCalls).toBeGreaterThan(0)
47+
48+
const imgs = page.locator('div.ProseMirror figure.image img.image__main')
49+
const count = await imgs.count()
50+
expect(count).toBe(2)
51+
for (let i = 0; i < count; i++) {
52+
const naturalWidth = await imgs.nth(i).evaluate((el: HTMLImageElement) => el.naturalWidth)
53+
expect(naturalWidth, `image ${i} should have loaded`).toBeGreaterThan(0)
54+
}
55+
})
56+
})

playwright/support/fixtures/CollectivePage.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import type { Page } from '@playwright/test'
77
import type { User } from './User.ts'
88

9+
import { readFileSync } from 'node:fs'
10+
import { resolve } from 'node:path'
911
import { webdavUrl } from '../helpers/urls.ts'
1012

1113
type CollectivePageData = {
@@ -156,4 +158,43 @@ export class CollectivePage {
156158
const content = `## Link\n\n[${linkText}](${linkUrl})`
157159
await this.setContent({ content, user, page })
158160
}
161+
162+
async uploadImage({ filename, mimetype = 'image/png', user, page }: {
163+
filename: string
164+
mimetype?: string
165+
user: User
166+
page: Page
167+
}): Promise<string> {
168+
const attachmentsDir = `.attachments.${this.data.id}`
169+
170+
// MKCOL is idempotent enough: 201 = created, 405 = already exists
171+
const dirUrl = webdavUrl(
172+
user.account.userId,
173+
this.data.collectivePath,
174+
this.data.filePath,
175+
attachmentsDir,
176+
)
177+
const mkcol = await page.request.fetch(dirUrl, { method: 'MKCOL' })
178+
if (![201, 405].includes(mkcol.status())) {
179+
throw new Error(`MKCOL ${dirUrl} failed: ${mkcol.status()}`)
180+
}
181+
182+
const filepath = resolve(import.meta.dirname, 'files', filename)
183+
await page.request.put(
184+
webdavUrl(
185+
user.account.userId,
186+
this.data.collectivePath,
187+
this.data.filePath,
188+
attachmentsDir,
189+
filename,
190+
),
191+
{
192+
headers: { 'Content-Type': mimetype },
193+
data: readFileSync(filepath),
194+
failOnStatusCode: true,
195+
},
196+
)
197+
198+
return `${attachmentsDir}/${filename}`
199+
}
159200
}

0 commit comments

Comments
 (0)