You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
6.2 KiB
JavaScript
101 lines
6.2 KiB
JavaScript
|
4 weeks ago
|
// Uses an authorized environment and deletes only its uniquely named, empty fixture tree.
|
||
|
|
const assert = require('node:assert/strict')
|
||
|
|
const fs = require('node:fs')
|
||
|
|
const path = require('node:path')
|
||
|
|
const { chromium } = require(process.env.COURSE_RESOURCE_PLAYWRIGHT_PATH || 'playwright')
|
||
|
|
const origin = process.env.QA_ORIGIN
|
||
|
|
const username = process.env.QA_ADMIN_USERNAME
|
||
|
|
const password = process.env.QA_ADMIN_PASSWORD
|
||
|
|
assert(origin && username && password, 'Provide the authorized QA origin and admin credentials')
|
||
|
|
const reports = path.resolve(process.env.QA_REPORT_DIR || 'tests/e2e/reports/course-resource-menu-focus')
|
||
|
|
fs.mkdirSync(reports, { recursive: true })
|
||
|
|
async function main() {
|
||
|
|
const browser = await chromium.launch({ channel: 'chrome', headless: true })
|
||
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } })
|
||
|
|
const page = await context.newPage()
|
||
|
|
const result = { errors: [], warnings: [], passed: [] }
|
||
|
|
let rootId
|
||
|
|
let token
|
||
|
|
let currentStep = 'login'
|
||
|
|
const marker = 'QA_MENU_FOCUS_' + Date.now() + '_'
|
||
|
|
page.on('pageerror', error => result.errors.push(error.message))
|
||
|
|
page.on('console', message => {
|
||
|
|
if (['warning', 'error'].includes(message.type())) result.warnings.push({ step: currentStep, message: message.text() })
|
||
|
|
})
|
||
|
|
async function api(endpoint, method = 'GET', data) {
|
||
|
|
const response = await context.request.fetch(origin + '/api/course-resources' + endpoint, {
|
||
|
|
method, headers: { Authorization: 'Bearer ' + token }, ...(data !== undefined ? { data } : {}),
|
||
|
|
})
|
||
|
|
assert.equal(response.status(), 200, method + ' ' + endpoint)
|
||
|
|
return (await response.json()).data
|
||
|
|
}
|
||
|
|
async function menu(id, command) {
|
||
|
|
currentStep = command
|
||
|
|
await page.locator(`[data-resource-node="${id}"] .el-dropdown`).click()
|
||
|
|
await page.locator('.el-dropdown-menu:visible').getByText(command, { exact: true }).click()
|
||
|
|
}
|
||
|
|
async function triggerFocused(id) {
|
||
|
|
await page.waitForFunction(id => document.activeElement?.closest('[data-resource-node]')?.getAttribute('data-resource-node') === String(id), id, { timeout: 5000 }).catch(async error => {
|
||
|
|
result.focusDiagnostic = await page.evaluate(id => ({ active: document.activeElement?.outerHTML.slice(0, 2000), trigger: document.querySelector(`[data-resource-node="${id}"] .el-dropdown`)?.outerHTML, menus: [...document.querySelectorAll('.el-dropdown-menu')].map(menu => ({ id: menu.id, labelledBy: menu.getAttribute('aria-labelledby') })) }), id)
|
||
|
|
throw error
|
||
|
|
})
|
||
|
|
assert(await page.locator(`[data-resource-node="${id}"] .el-dropdown button`).isVisible())
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
await page.goto(origin + '/#/login')
|
||
|
|
await page.getByPlaceholder('账号', { exact: true }).fill(username)
|
||
|
|
await page.getByPlaceholder('密码', { exact: true }).fill(password)
|
||
|
|
await page.getByRole('button', { name: '登 录', exact: true }).click()
|
||
|
|
await page.waitForURL(url => !url.hash.includes('/login'))
|
||
|
|
token = (await context.cookies()).find(cookie => cookie.name === 'Admin-Token').value
|
||
|
|
rootId = await api('/nodes', 'POST', { name: marker + '章' })
|
||
|
|
await api('/nodes', 'POST', { name: marker + '甲节', parentId: rootId })
|
||
|
|
const childId = await api('/nodes', 'POST', { name: marker + '乙节', parentId: rootId })
|
||
|
|
await page.goto(origin + '/#/platform-course-resources/index')
|
||
|
|
await page.getByRole('heading', { name: '课程资源', exact: true }).waitFor()
|
||
|
|
await page.getByRole('button', { name: '展开全部', exact: true }).click()
|
||
|
|
await page.locator(`[data-resource-node="${childId}"] .el-dropdown`).click()
|
||
|
|
await page.locator('.el-dropdown-menu:visible').waitFor()
|
||
|
|
await page.locator('.el-dropdown-menu:visible').getByText('重命名', { exact: true }).focus()
|
||
|
|
await page.keyboard.press('Escape')
|
||
|
|
await page.locator('.el-dropdown-menu:visible').waitFor({ state: 'hidden' })
|
||
|
|
await triggerFocused(childId)
|
||
|
|
result.passed.push('Escape closes the menu and restores focus')
|
||
|
|
await menu(childId, '重命名')
|
||
|
|
await page.getByRole('textbox', { name: '请输入目录名称', exact: true }).fill(marker + '改名节')
|
||
|
|
await page.locator('.el-message-box').getByRole('button', { name: '确定', exact: true }).click()
|
||
|
|
await page.locator(`[data-resource-node="${childId}"] .node-name`).filter({ hasText: marker + '改名节' }).waitFor()
|
||
|
|
await triggerFocused(childId)
|
||
|
|
result.passed.push('rename dialog restores focus to the visible trigger')
|
||
|
|
for (const command of ['上移', '下移']) {
|
||
|
|
const oldNumber = await page.locator(`[data-resource-node="${childId}"] .node-number`).innerText()
|
||
|
|
await menu(childId, command)
|
||
|
|
await page.waitForFunction(({ id, oldNumber }) => document.querySelector(`[data-resource-node="${id}"] .node-number`)?.textContent !== oldNumber, { id: childId, oldNumber })
|
||
|
|
await triggerFocused(childId)
|
||
|
|
}
|
||
|
|
result.passed.push('move commands preserve focus outside the hidden menu')
|
||
|
|
await menu(childId, '删除')
|
||
|
|
await page.locator('.el-message-box').getByRole('button', { name: '取消', exact: true }).click()
|
||
|
|
await triggerFocused(childId)
|
||
|
|
assert((await api('/management')).nodes.some(node => node.id === childId))
|
||
|
|
result.passed.push('cancel deletion restores focus and preserves the directory')
|
||
|
|
await page.screenshot({ path: path.join(reports, 'menu-focus-restored.png'), fullPage: true, animations: 'disabled' })
|
||
|
|
await menu(childId, '删除')
|
||
|
|
await page.locator('.el-message-box').getByRole('button', { name: '确认删除', exact: true }).click()
|
||
|
|
await page.locator(`[data-resource-node="${childId}"]`).waitFor({ state: 'hidden' })
|
||
|
|
result.passed.push('confirmed deletion still removes the directory')
|
||
|
|
assert.deepEqual(result.errors, [])
|
||
|
|
assert.deepEqual(result.warnings, [])
|
||
|
|
console.log('PASS: rename, move and deletion focus restoration without browser warnings')
|
||
|
|
} catch (error) {
|
||
|
|
result.failure = error.message
|
||
|
|
await page.screenshot({ path: path.join(reports, 'failure.png'), fullPage: true, animations: 'disabled' })
|
||
|
|
throw error
|
||
|
|
} finally {
|
||
|
|
if (rootId) await api('/nodes/' + rootId, 'DELETE')
|
||
|
|
fs.writeFileSync(path.join(reports, 'result.json'), JSON.stringify(result, null, 2))
|
||
|
|
await browser.close()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
main().catch(error => { console.error(error.message); process.exitCode = 1 })
|