/** * 任务:更新项目 * * 逻辑: * 1. 打开仓库编辑页面 * 2. 在 Monaco 编辑器末尾追加内容 * 3. 点击提交修改按钮 */ const path = require('path'); const config = require(path.join(__dirname, '..', 'config')); const logger = require(path.join(__dirname, '..', 'utils', 'logger')); const { safeClose, safeGoto } = require(path.join(__dirname, '..', 'utils', 'page')); /** * 执行更新项目任务 * @param {import('playwright').Page} page - 主页面 */ async function updateProject(page) { logger.info('👉 执行项目更新'); const context = page.context(); const newPage = await context.newPage(); try { // 打开编辑页面 await safeGoto(newPage, config.urls.testRepo, { label: '仓库编辑页', timeout: config.navigationTimeout, waitFor: ['#codeartside'], }); logger.info('已进入仓库编辑页面'); // 获取编辑器 iframe const frame = newPage.frameLocator('#codeartside'); // 点击编辑器区域 await frame.locator('.monaco-editor').click({ position: { x: 200, y: 100 }, }); // 光标移动到末尾 await newPage.keyboard.press('Control+End'); await newPage.waitForTimeout(300); // 极短等待确保光标到位(键盘事件无法 await) // 追加内容 const content = `\n# 自动更新 - ${new Date().toISOString()}`; await newPage.keyboard.type(content, { delay: 50 }); logger.info('已写入内容'); // 点击"提交修改"按钮(页面上的第一个) await newPage.locator('button:has-text("提交修改")').first().click(); await page.waitForTimeout(1000); // 等待弹框中的"提交修改"按钮出现并点击 const confirmBtn = newPage.locator('button:has-text("提交修改")').nth(1); await confirmBtn.waitFor({ state: 'visible', timeout: 10000 }); await confirmBtn.click(); await page.waitForTimeout(1000); logger.info('项目更新已提交'); } finally { await safeClose(newPage); } } module.exports = updateProject;