You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
3.8 KiB

const assert = require('node:assert/strict');
const { chromium } = require('playwright');
const base = 'http://127.0.0.1:4173/pages';
const PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScL/nwAAAABJRU5ErkJggg==';
(async () => {
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, locale: 'zh-CN' });
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
await page.goto(`${base}/login.html`);
await page.evaluate(() => window.YouthomeLocal.reset());
await page.goto(`${base}/login.html`);
await page.locator('#panel-password input[type="tel"]').fill('13800000000');
await page.locator('#panel-password input[type="password"]').fill('qinghe123');
await page.locator('#panel-password .btn-primary').click();
await page.waitForURL('**/feed.html');
await page.evaluate(async () => {
await window.YouthomeLocal.request('/campus/moments', {
method: 'POST',
body: JSON.stringify({ content: '资料编辑回归测试动态', mediaUrls: [], tags: [], category: '校园生活', scope: 'CAMPUS' })
});
});
// 1. 打开资料编辑,应带出当前账号资料
await page.goto(`${base}/me-edit.html`);
await page.waitForTimeout(600);
const nickname = page.locator('.edit-form input[type="text"]:not([disabled])').first();
assert.equal(await nickname.inputValue(), '林小夏', '资料编辑应带出当前账号昵称');
// 2. 修改并保存
await nickname.fill('林小夏同学');
await page.locator('.form-textarea').fill('改过的个人简介:喜欢在图书馆靠窗的位置写代码。');
await page.locator('.form-footer .btn', { hasText: '保存' }).first().click();
await page.waitForURL('**/me.html', { timeout: 8000 });
await page.waitForTimeout(1200);
// 3. 个人中心显示新资料
assert.equal((await page.locator('.profile-name').innerText()).trim(), '林小夏同学');
assert.ok((await page.locator('.profile-bio').innerText()).includes('喜欢在图书馆靠窗的位置'), '个人中心应显示新的简介');
// 4. 已发布动态的作者名同步更新
const authorName = await page.evaluate(async () => {
const data = await window.YouthomeLocal.request('/campus/moments?scope=CAMPUS&page=1&size=50');
const mine = (data.items || []).find((item) => item.content === '资料编辑回归测试动态');
return mine ? mine.userName : null;
});
assert.equal(authorName, '林小夏同学', '已发布动态的作者名应同步更新');
// 4.5 头像上传:本地压缩后写入账号,并同步到侧栏与个人中心
await page.goto(`${base}/me-edit.html`);
await page.waitForTimeout(700);
await page.locator('input[type="file"]').setInputFiles({ name: 'avatar.png', mimeType: 'image/png', buffer: Buffer.from(PNG, 'base64') });
await page.locator('.web-toast', { hasText: '头像已更新' }).waitFor({ timeout: 10000 });
const storedAvatar = await page.evaluate(() => JSON.parse(localStorage.getItem('youthome:web:authUser') || '{}').avatar || '');
assert.ok(storedAvatar.startsWith('data:image'), '头像应压缩后存入账号');
assert.equal(await page.locator('.web-user-avatar img').count(), 1, '侧栏应显示新头像');
await page.goto(`${base}/me.html`);
await page.waitForTimeout(1200);
assert.equal(await page.locator('.profile-avatar img').count(), 1, '个人中心应显示新头像');
// 5. 刷新后仍是新资料
await page.reload();
await page.waitForTimeout(1200);
assert.equal((await page.locator('.profile-name').innerText()).trim(), '林小夏同学');
assert.deepEqual(errors, []);
await browser.close();
console.log('profile edit and save passed in Edge');
})().catch((error) => { console.error(error); process.exitCode = 1; });