enhance(e2e): add a local plugin for the e2e tests

pull/10793/head
charlie 2024-01-11 19:10:28 +08:00
parent af03ed50d5
commit 9ef1d89089
6 changed files with 117 additions and 2 deletions

View File

@ -99,7 +99,33 @@ test('block related apis',
*/
export async function callPageAPI(page, method, ...args) {
return await page.evaluate(([method, args]) => {
const hasNs = method.indexOf('.') !== -1
const ns = hasNs ? method.split('.') : method
// @ts-ignore
return window.logseq.api[method]?.(...args)
const ctx = hasNs ? window.logseq.sdk[ns[0].toLowerCase()] : window.logseq.api
return ctx[hasNs ? ns[1] : method]?.(...args)
}, [method, args])
}
/**
* load local tests plugin
*/
export async function loadLocalE2eTestsPlugin(page) {
const pid = 'a-logseq-plugin-for-e2e-tests'
const hasLoaded = await page.evaluate(([pid]) => {
// @ts-ignore
const p = window.LSPluginCore.registeredPlugins.get(pid)
return p != null
}, [pid])
if (hasLoaded) return true
await callPageAPI(page, 'set_state_from_store',
'ui/developer-mode?', true)
await page.keyboard.press('t+p')
await page.locator('text=Load unpacked plugin')
await callPageAPI(page, 'set_state_from_store',
'plugin/selected-unpacked-pkg', `${__dirname}/plugin`)
await page.keyboard.press('Escape')
await page.keyboard.press('Escape')
}

View File

@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lsplugin.user.js"></script>
</head>
<body>
<div id="root"></div>
<script src="./index.js"></script>
</body>
</html>

49
e2e-tests/plugin/index.js Normal file
View File

@ -0,0 +1,49 @@
async function main () {
logseq.UI.showMsg('Hi, e2e tests from a local plugin!')
// await (new Promise(resolve => setTimeout(resolve, 3000)))
let msg = 0
const logPane = (input) => {
logseq.provideUI({
key: `log-${++msg}`,
path: `#a-plugin-for-e2e-tests > ul`,
template: `<li>${input}</li>`
})
}
// log pane
logseq.provideUI({
key: 'logseq-e2e-tests',
template: `<div id="a-plugin-for-e2e-tests">
<h2>Plugin e2e tests ...</h2>
<ul></ul>
</div>`,
path: 'body',
style: {
width: '300px',
position: 'fixed',
top: '300px',
left: '300px',
zIndex: 99,
},
})
logseq.provideStyle(`
#a-plugin-for-e2e-tests {
padding: 20px;
background-color: red;
color: white;
width: 300px;
}
`)
// test log
setTimeout(() => {
logPane(`DB: hook changed`)
}, 2000)
}
// bootstrap
logseq.ready(main).catch(null)

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,8 @@
{
"name": "e2e-plugin",
"description": "A plugin for e2e tests",
"main": "./index.html",
"logseq": {
"id": "a-logseq-plugin-for-e2e-tests"
}
}

View File

@ -1,6 +1,6 @@
import { expect } from '@playwright/test'
import { test } from './fixtures'
import { callPageAPI } from './logseq-api.spec'
import { callPageAPI, loadLocalE2eTestsPlugin } from './logseq-api.spec'
test.skip('enabled plugin system default', async ({ page }) => {
const callAPI = callPageAPI.bind(null, page)
@ -60,3 +60,18 @@ test.skip('play a plugin<logseq-journals-calendar> from the Marketplace', async
await expect(page.locator('body[data-page="page"]')).toBeVisible()
})
test(`play a plugin from local`, async ({ page }) => {
const callAPI = callPageAPI.bind(null, page)
const _pLoaded = await loadLocalE2eTestsPlugin(page)
await callAPI(`ui.show_msg`, 1)
const loc = page.locator('#a-plugin-for-e2e-tests')
await loc.waitFor({state: 'visible'})
await callAPI(`ui.show_msg`, 1)
await expect(page.locator('text=DB: hook changed')).toBeVisible()
// await page.waitForSelector('#test-pause')
})