forked from logseq/logseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.ts
229 lines (206 loc) · 8.09 KB
/
fixtures.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as fs from 'fs'
import * as path from 'path'
import { test as base, expect, ConsoleMessage, Locator } from '@playwright/test';
import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
import { loadLocalGraph, openLeftSidebar, randomString } from './utils';
import { LogseqFixtures } from './types';
let electronApp: ElectronApplication
let context: BrowserContext
let page: Page
let repoName = randomString(10)
let testTmpDir = path.resolve(__dirname, '../tmp')
if (fs.existsSync(testTmpDir)) {
fs.rmSync(testTmpDir, { recursive: true })
}
export let graphDir = path.resolve(testTmpDir, "e2e-test", repoName)
// NOTE: This following is a console log watcher for error logs.
// Save and print all logs when error happens.
let logs: string
const consoleLogWatcher = (msg: ConsoleMessage) => {
// console.log(msg.text())
const text = msg.text()
logs += text + '\n'
expect(text, logs).not.toMatch(/^(Failed to|Uncaught)/)
// youtube video
if (!text.match(/^Error with Permissions-Policy header: Unrecognized feature/)) {
expect(text, logs).not.toMatch(/^Error/)
}
// NOTE: React warnings will be logged as error.
// expect(msg.type()).not.toBe('error')
}
base.beforeAll(async () => {
if (electronApp) {
return
}
console.log(`Creating test graph directory: ${graphDir}`)
fs.mkdirSync(graphDir, {
recursive: true,
});
electronApp = await electron.launch({
cwd: "./static",
args: ["electron.js"],
locale: 'en',
timeout: 10_000, // should be enough for the app to start
})
context = electronApp.context()
await context.tracing.start({ screenshots: true, snapshots: true });
// NOTE: The following ensures App first start with the correct path.
const info = await electronApp.evaluate(async ({ app }) => {
return {
"appPath": app.getAppPath(),
"appData": app.getPath("appData"),
"userData": app.getPath("userData"),
"appName": app.getName(),
"electronVersion": app.getVersion(),
}
})
console.log("Test start with:", info)
page = await electronApp.firstWindow()
// Direct Electron console to watcher
page.on('console', consoleLogWatcher)
page.on('crash', () => {
expect(false, "Page must not crash").toBeTruthy()
})
page.on('pageerror', (err) => {
console.log(err)
// expect(false, 'Page must not have errors!').toBeTruthy()
})
await page.waitForLoadState('domcontentloaded')
// NOTE: The following ensures first start.
// await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
await page.waitForSelector(':has-text("Loading")', {
state: "hidden",
timeout: 1000 * 15,
});
page.once('load', async () => {
console.log('Page loaded!')
await page.screenshot({ path: 'startup.png' })
})
await loadLocalGraph(page, graphDir);
// render app
await page.waitForFunction('window.document.title !== "Loading"')
expect(await page.title()).toMatch(/^Logseq.*?/)
await openLeftSidebar(page)
})
base.beforeEach(async () => {
// discard any dialog by ESC
if (page) {
await page.keyboard.press('Escape')
await page.keyboard.press('Escape')
}
})
base.afterAll(async () => {
// if (electronApp) {
// await electronApp.close()
//}
})
// hijack electron app into the test context
// FIXME: add type to `block`
export const test = base.extend<LogseqFixtures>({
page: async ({ }, use) => {
await use(page);
},
// Timeout is used to avoid global timeout, local timeout will have a meaningful error report.
// 1s timeout is enough for most of the test cases.
// Timeout won't introduce additional sleeps.
block: async ({ page }, use) => {
const block = {
mustFill: async (value: string) => {
const locator: Locator = page.locator('textarea >> nth=0')
await locator.waitFor({ timeout: 1000 })
await locator.fill(value)
await expect(locator).toHaveText(value, { timeout: 1000 })
},
mustType: async (value: string, options?: { delay?: number, toBe?: string }) => {
const locator: Locator = page.locator('textarea >> nth=0')
await locator.waitFor({ timeout: 1000 })
const { delay = 50 } = options || {};
const { toBe = value } = options || {};
await locator.type(value, { delay })
await expect(locator).toHaveText(toBe, { timeout: 1000 })
},
enterNext: async (): Promise<Locator> => {
let blockCount = await page.locator('.page-blocks-inner .ls-block').count()
await page.press('textarea >> nth=0', 'Enter')
await page.waitForSelector(`.ls-block >> nth=${blockCount} >> textarea`, { state: 'visible', timeout: 1000 })
return page.locator('textarea >> nth=0')
},
clickNext: async (): Promise<Locator> => {
await page.$eval('.add-button-link-wrap', (element) => {
element.scrollIntoView();
});
let blockCount = await page.locator('.page-blocks-inner .ls-block').count()
// the next element after all blocks.
await page.click('.add-button-link-wrap', { delay: 100 })
await page.waitForSelector(`.ls-block >> nth=${blockCount} >> textarea`, { state: 'visible', timeout: 1000 })
return page.locator('textarea >> nth=0')
},
indent: async (): Promise<boolean> => {
const locator = page.locator('textarea >> nth=0')
const before = await locator.boundingBox()
await locator.press('Tab', { delay: 100 })
return (await locator.boundingBox()).x > before.x
},
unindent: async (): Promise<boolean> => {
const locator = page.locator('textarea >> nth=0')
const before = await locator.boundingBox()
await locator.press('Shift+Tab', { delay: 100 })
return (await locator.boundingBox()).x < before.x
},
waitForBlocks: async (total: number): Promise<void> => {
// NOTE: `nth=` counts from 0.
await page.waitForSelector(`.ls-block >> nth=${total - 1}`, { state: 'attached', timeout: 50000 })
await page.waitForSelector(`.ls-block >> nth=${total}`, { state: 'detached', timeout: 50000 })
},
waitForSelectedBlocks: async (total: number): Promise<void> => {
// NOTE: `nth=` counts from 0.
await page.waitForSelector(`.ls-block.selected >> nth=${total - 1}`, { timeout: 1000 })
},
escapeEditing: async (): Promise<void> => {
await page.keyboard.press('Escape')
await page.keyboard.press('Escape')
},
activeEditing: async (nth: number): Promise<void> => {
await page.waitForSelector(`.ls-block >> nth=${nth}`, { timeout: 1000 })
// scroll, for isVisble test
await page.$eval(`.ls-block >> nth=${nth}`, (element) => {
element.scrollIntoView();
});
// when blocks are nested, the first block(the parent) is selected.
if (
(await page.isVisible(`.ls-block >> nth=${nth} >> .editor-wrapper >> textarea`)) &&
!(await page.isVisible(`.ls-block >> nth=${nth} >> .block-children-container >> textarea`))) {
return;
}
await page.click(`.ls-block >> nth=${nth} >> .block-content`, { delay: 10, timeout: 100000 })
await page.waitForSelector(`.ls-block >> nth=${nth} >> .editor-wrapper >> textarea`, { timeout: 1000, state: 'visible' })
},
isEditing: async (): Promise<boolean> => {
const locator = page.locator('.ls-block textarea >> nth=0')
return await locator.isVisible()
},
selectionStart: async (): Promise<number> => {
return await page.locator('textarea >> nth=0').evaluate(node => {
const elem = <HTMLTextAreaElement>node
return elem.selectionStart
})
},
selectionEnd: async (): Promise<number> => {
return await page.locator('textarea >> nth=0').evaluate(node => {
const elem = <HTMLTextAreaElement>node
return elem.selectionEnd
})
}
}
use(block)
},
context: async ({ }, use) => {
await use(context);
},
app: async ({ }, use) => {
await use(electronApp);
},
graphDir: async ({ }, use) => {
await use(graphDir);
},
});