forked from laysense/laysense.cn
164 lines
8.2 KiB
JavaScript
164 lines
8.2 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {chromium} = require(process.env.PLAYWRIGHT_MODULE || 'playwright');
|
|
|
|
const baseURL = process.env.LAYSENSE_TEST_URL || 'http://127.0.0.1:8787';
|
|
const email = process.env.LAYSENSE_TEST_EMAIL;
|
|
const password = process.env.LAYSENSE_TEST_PASSWORD;
|
|
if (!email || !password) {
|
|
throw new Error('Set LAYSENSE_TEST_EMAIL and LAYSENSE_TEST_PASSWORD for a disposable test database.');
|
|
}
|
|
|
|
async function run(browser) {
|
|
const context = await browser.newContext({baseURL, viewport: {width: 1480, height: 960}, reducedMotion: 'reduce'});
|
|
const page = await context.newPage();
|
|
const errors = [];
|
|
page.on('pageerror', (error) => errors.push(error.message));
|
|
await page.goto('/admin/login');
|
|
await page.locator('[name=email]').fill(email);
|
|
await page.locator('[name=password]').fill(password);
|
|
await page.locator('form button[type=submit]').click();
|
|
await page.waitForURL('**/admin');
|
|
|
|
const csrf = await page.locator('[name=csrf]').first().inputValue();
|
|
const slug = 'studio-browser-' + Date.now().toString(36);
|
|
async function post(path, values) {
|
|
const response = await context.request.post(path, {
|
|
form: {csrf, ...values},
|
|
headers: {'X-Requested-With': 'XMLHttpRequest'},
|
|
});
|
|
assert.equal(response.status(), 200, await response.text());
|
|
return response.json();
|
|
}
|
|
|
|
await post('/admin/new-page', {slug, name: '画布测试页'});
|
|
await post('/admin/page-blocks/' + slug, {operation: 'create', key: 'fixtures', preset: 'text'});
|
|
await post('/admin/pages/' + slug + '/fixtures', {
|
|
version: '0', title: '媒体和链接', lead: '', style: 'plain',
|
|
body: '<p><img src="/assets/favicon.png" alt="示例图片" width="96"></p>'
|
|
+ '<p><a href="/services">示例链接</a></p>'
|
|
+ '<video controls width="260" preload="none"><source src="/assets/original/drag-in-components.mp4"></video>'
|
|
+ '<audio controls src="/assets/original/drag-in-components.mp4" preload="none"></audio>',
|
|
});
|
|
const editorPath = '/admin/pages/' + slug;
|
|
await page.goto(editorPath);
|
|
const version = await page.locator('#studio-page-settings [name=version]').inputValue();
|
|
await post(editorPath, {
|
|
version, name: '画布测试页', status: 'draft', title: '画布测试页',
|
|
description: '', heading: '画布测试页', accent: '', lead: '',
|
|
image: '/assets/original/home-hero.webp', alt: '首屏示例', image_scale: '0',
|
|
});
|
|
await page.reload();
|
|
const frame = page.frameLocator('iframe[title="可编辑页面画布"]');
|
|
const sizing = page.locator('[data-media-sizing]');
|
|
const properties = page.locator('[data-canvas-properties]');
|
|
const closeProperties = () => properties.locator('[data-property-close]').click();
|
|
|
|
const image = frame.locator('#fixtures img');
|
|
await image.click();
|
|
await sizing.waitFor({state: 'visible'});
|
|
assert.equal(await image.getAttribute('data-studio-selected'), '');
|
|
await sizing.locator('[data-media-width]').selectOption('50');
|
|
assert.match(await image.getAttribute('class'), /studio-media-50/);
|
|
await image.dblclick();
|
|
await properties.waitFor({state: 'visible'});
|
|
assert.equal(await properties.locator('h2').textContent(), '正文图片');
|
|
await properties.locator('[name=url]').fill('/assets/original/home-hero.webp');
|
|
await properties.locator('button[type=submit]').click();
|
|
assert.equal(await image.getAttribute('src'), '/assets/original/home-hero.webp');
|
|
|
|
const hero = frame.locator('[data-studio-hero-image]');
|
|
await page.locator('iframe[title="可编辑页面画布"]').evaluate((iframe) => iframe.contentWindow.scrollTo(0, 0));
|
|
await hero.click();
|
|
await sizing.waitFor({state: 'visible'});
|
|
assert.equal(await hero.getAttribute('data-studio-selected'), '');
|
|
await sizing.locator('[data-replace-media]').click();
|
|
assert.equal(await properties.locator('h2').textContent(), '首屏图片');
|
|
await closeProperties();
|
|
|
|
await frame.locator('#fixtures a').click();
|
|
await properties.waitFor({state: 'visible'});
|
|
assert.equal(await properties.locator('h2').textContent(), '正文链接');
|
|
await properties.locator('[name=url]').fill('/articles');
|
|
await properties.locator('button[type=submit]').click();
|
|
assert.equal(await frame.locator('#fixtures a').getAttribute('href'), '/articles');
|
|
|
|
for (const [tag, label] of [['video', '视频'], ['audio', '音频']]) {
|
|
const media = frame.locator('#fixtures ' + tag);
|
|
await media.dispatchEvent('dblclick');
|
|
await properties.waitFor({state: 'visible'});
|
|
assert.equal(await properties.locator('h2').textContent(), label);
|
|
await closeProperties();
|
|
}
|
|
|
|
const gallery = page.locator('[data-canvas-presets]');
|
|
assert.equal(await gallery.locator('[data-template-group]').count(), 6);
|
|
assert.equal(await gallery.locator('[data-template-card]').count(), 13);
|
|
const brokenPreviews = await gallery.locator('[data-template-card] img').evaluateAll(async (images) => {
|
|
for (const image of images) image.loading = 'eager';
|
|
await Promise.all(images.map((image) => image.decode()));
|
|
return images.filter((image) => image.naturalWidth !== 768).length;
|
|
});
|
|
assert.equal(brokenPreviews, 0);
|
|
await gallery.locator('[data-template-filter=cover]').click();
|
|
assert.equal(await gallery.locator('[data-template-card]:visible').count(), 2);
|
|
const cover = gallery.locator('[data-template-preview=cover]');
|
|
await cover.hover();
|
|
const peek = gallery.locator('[data-template-peek]');
|
|
await peek.waitFor({state: 'visible'});
|
|
assert((await peek.boundingBox()).width > (await cover.boundingBox()).width);
|
|
await cover.focus();
|
|
await page.keyboard.press('Enter');
|
|
const dialog = gallery.locator('[data-template-dialog]');
|
|
await dialog.waitFor({state: 'visible'});
|
|
assert.equal(await dialog.locator('h2').textContent(), '图文封面');
|
|
await dialog.locator('[data-template-use]').click();
|
|
await frame.locator('section[id^=block-] h2').filter({hasText: '图文封面'}).waitFor();
|
|
|
|
await gallery.locator('[data-template-filter=""]').click();
|
|
const search = gallery.locator('[data-template-search]');
|
|
await search.fill('does-not-exist');
|
|
await gallery.locator('[data-template-empty]').waitFor({state: 'visible'});
|
|
await search.fill('双栏');
|
|
assert.equal(await gallery.locator('[data-template-card]:visible').count(), 1);
|
|
|
|
await Promise.all([
|
|
page.waitForResponse((response) => response.url().endsWith(editorPath) && response.request().method() === 'POST' && response.ok()),
|
|
page.getByRole('button', {name: '保存页面', exact: true}).first().click(),
|
|
]);
|
|
await page.locator('[data-save-confirmation]').waitFor({state: 'visible'});
|
|
await frame.locator('section[id^=block-] .studio-cover').waitFor();
|
|
assert.equal(await frame.locator('#fixtures a').getAttribute('href'), '/articles');
|
|
assert.equal(await frame.locator('#fixtures img').getAttribute('src'), '/assets/original/home-hero.webp');
|
|
|
|
const phone = await browser.newContext({
|
|
baseURL, viewport: {width: 390, height: 844}, isMobile: true, hasTouch: true,
|
|
storageState: await context.storageState(),
|
|
});
|
|
const phonePage = await phone.newPage();
|
|
phonePage.on('pageerror', (error) => errors.push(error.message));
|
|
await phonePage.goto(editorPath);
|
|
const phoneGallery = phonePage.locator('[data-canvas-presets]');
|
|
await phoneGallery.locator('[data-template-filter=cover]').tap();
|
|
await phoneGallery.locator('[data-template-preview=banner]').tap();
|
|
await phoneGallery.locator('[data-template-dialog]').waitFor({state: 'visible'});
|
|
assert.equal(await phoneGallery.locator('[data-template-dialog] h2').textContent(), '横幅');
|
|
assert.deepEqual(errors, []);
|
|
await phone.close();
|
|
await context.close();
|
|
}
|
|
|
|
(async () => {
|
|
const browser = await chromium.launch({executablePath: process.env.CHROMIUM_EXECUTABLE || undefined});
|
|
try {
|
|
await run(browser);
|
|
console.log('Passed Chromium canvas media/link interactions, template gallery, and persistence.');
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
})().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|