2021-11-29 04:09:01 +00:00
|
|
|
import { Page, Locator } from 'playwright'
|
2021-11-24 08:04:14 +00:00
|
|
|
import { expect } from '@playwright/test'
|
2022-04-02 13:37:38 +00:00
|
|
|
import * as process from 'process'
|
2021-11-24 08:04:14 +00:00
|
|
|
|
2021-11-29 04:09:01 +00:00
|
|
|
export const IsMac = process.platform === 'darwin'
|
|
|
|
export const IsLinux = process.platform === 'linux'
|
|
|
|
export const IsWindows = process.platform === 'win32'
|
2021-11-24 08:04:14 +00:00
|
|
|
|
|
|
|
export function randomString(length: number) {
|
2022-01-17 05:30:59 +00:00
|
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2021-11-24 08:04:14 +00:00
|
|
|
|
2022-01-17 05:30:59 +00:00
|
|
|
let result = '';
|
|
|
|
const charactersLength = characters.length;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
|
|
}
|
2021-11-24 08:04:14 +00:00
|
|
|
|
2022-01-17 05:30:59 +00:00
|
|
|
return result;
|
2021-11-24 08:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createRandomPage(page: Page) {
|
2022-01-17 05:30:59 +00:00
|
|
|
const randomTitle = randomString(20)
|
|
|
|
|
|
|
|
// Click #search-button
|
|
|
|
await page.click('#search-button')
|
|
|
|
// Fill [placeholder="Search or create page"]
|
|
|
|
await page.fill('[placeholder="Search or create page"]', randomTitle)
|
|
|
|
// Click text=/.*New page: "new page".*/
|
|
|
|
await page.click('text=/.*New page: ".*/')
|
|
|
|
// wait for textarea of first block
|
2022-03-31 14:52:42 +00:00
|
|
|
await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
|
2022-01-17 05:30:59 +00:00
|
|
|
|
|
|
|
return randomTitle;
|
2021-11-24 08:04:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:54:30 +00:00
|
|
|
export async function createPage(page: Page, page_name: string) {// Click #search-button
|
|
|
|
await page.click('#search-button')
|
|
|
|
// Fill [placeholder="Search or create page"]
|
|
|
|
await page.fill('[placeholder="Search or create page"]', page_name)
|
|
|
|
// Click text=/.*New page: "new page".*/
|
|
|
|
await page.click('text=/.*New page: ".*/')
|
|
|
|
// wait for textarea of first block
|
2022-03-31 14:52:42 +00:00
|
|
|
await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
|
2022-02-17 10:54:30 +00:00
|
|
|
|
|
|
|
return page_name;
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:34:56 +00:00
|
|
|
|
|
|
|
export async function searchAndJumpToPage(page: Page, pageTitle: string) {
|
|
|
|
await page.click('#search-button')
|
|
|
|
await page.fill('[placeholder="Search or create page"]', pageTitle)
|
|
|
|
await page.waitForSelector(`[data-page-ref="${pageTitle}"]`, { state: 'visible' })
|
|
|
|
await page.click(`[data-page-ref="${pageTitle}"]`)
|
|
|
|
return pageTitle;
|
|
|
|
}
|
|
|
|
|
2021-12-20 08:21:07 +00:00
|
|
|
/**
|
2021-12-21 00:29:22 +00:00
|
|
|
* Locate the last block in the inner editor
|
2021-12-20 08:21:07 +00:00
|
|
|
* @param page The Playwright Page object.
|
|
|
|
* @returns The locator of the last block.
|
|
|
|
*/
|
2022-03-31 14:52:42 +00:00
|
|
|
export async function lastBlock(page: Page): Promise<Locator> {
|
2022-01-17 05:30:59 +00:00
|
|
|
// discard any popups
|
|
|
|
await page.keyboard.press('Escape')
|
|
|
|
// click last block
|
2022-03-31 18:56:51 +00:00
|
|
|
if (await page.locator('text="Click here to edit..."').isVisible()) {
|
|
|
|
await page.click('text="Click here to edit..."')
|
|
|
|
} else {
|
|
|
|
await page.click('.page-blocks-inner .ls-block >> nth=-1')
|
|
|
|
}
|
2022-01-17 05:30:59 +00:00
|
|
|
// wait for textarea
|
2022-03-31 14:52:42 +00:00
|
|
|
await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
|
2022-03-31 18:56:51 +00:00
|
|
|
await page.waitForTimeout(100)
|
2022-03-31 14:52:42 +00:00
|
|
|
return page.locator('textarea >> nth=0')
|
2021-12-21 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:52:42 +00:00
|
|
|
/**
|
|
|
|
* Press Enter and create the next block.
|
|
|
|
* @param page The Playwright Page object.
|
|
|
|
*/
|
|
|
|
export async function enterNextBlock(page: Page): Promise<Locator> {
|
|
|
|
let blockCount = await page.locator('.page-blocks-inner .ls-block').count()
|
|
|
|
await page.press('textarea >> nth=0', 'Enter')
|
2022-04-05 00:41:43 +00:00
|
|
|
await page.waitForTimeout(10)
|
2022-03-31 14:52:42 +00:00
|
|
|
await page.waitForSelector(`.ls-block >> nth=${blockCount} >> textarea`, { state: 'visible' })
|
|
|
|
return page.locator('textarea >> nth=0')
|
2021-11-24 08:04:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 08:21:07 +00:00
|
|
|
/**
|
2021-12-21 00:29:22 +00:00
|
|
|
* Create and locate a new block at the end of the inner editor
|
2022-01-07 04:09:27 +00:00
|
|
|
* @param page The Playwright Page object
|
2021-12-20 08:21:07 +00:00
|
|
|
* @returns The locator of the last block
|
|
|
|
*/
|
2021-12-21 00:29:22 +00:00
|
|
|
export async function newInnerBlock(page: Page): Promise<Locator> {
|
2022-03-31 14:52:42 +00:00
|
|
|
await lastBlock(page)
|
|
|
|
await page.press('textarea >> nth=0', 'Enter')
|
2021-12-21 00:29:22 +00:00
|
|
|
|
2022-03-31 14:52:42 +00:00
|
|
|
return page.locator('textarea >> nth=0')
|
2021-12-21 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function newBlock(page: Page): Promise<Locator> {
|
2022-03-31 18:56:51 +00:00
|
|
|
let blockNumber = await page.locator('.page-blocks-inner .ls-block').count()
|
2022-03-31 14:52:42 +00:00
|
|
|
const prev = await lastBlock(page)
|
|
|
|
await page.press('textarea >> nth=0', 'Enter')
|
2022-03-31 18:56:51 +00:00
|
|
|
await page.waitForSelector(`.page-blocks-inner .ls-block >> nth=${blockNumber} >> textarea`, { state: 'visible' })
|
2022-03-31 14:52:42 +00:00
|
|
|
return page.locator('textarea >> nth=0')
|
2021-11-24 08:04:14 +00:00
|
|
|
}
|
2021-12-11 15:50:17 +00:00
|
|
|
|
|
|
|
export async function escapeToCodeEditor(page: Page): Promise<void> {
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.press('.block-editor textarea', 'Escape')
|
|
|
|
await page.waitForSelector('.CodeMirror pre', { state: 'visible' })
|
2021-12-11 15:50:17 +00:00
|
|
|
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.waitForTimeout(300)
|
|
|
|
await page.click('.CodeMirror pre')
|
|
|
|
await page.waitForTimeout(300)
|
2021-12-11 15:50:17 +00:00
|
|
|
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.waitForSelector('.CodeMirror textarea', { state: 'visible' })
|
2021-12-11 15:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function escapeToBlockEditor(page: Page): Promise<void> {
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.waitForTimeout(300)
|
|
|
|
await page.click('.CodeMirror pre')
|
|
|
|
await page.waitForTimeout(300)
|
2021-12-11 15:50:17 +00:00
|
|
|
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.press('.CodeMirror textarea', 'Escape')
|
|
|
|
await page.waitForTimeout(300)
|
2021-12-11 15:50:17 +00:00
|
|
|
}
|
2021-12-30 09:35:20 +00:00
|
|
|
|
2022-01-05 13:05:05 +00:00
|
|
|
export async function setMockedOpenDirPath(
|
|
|
|
page: Page,
|
|
|
|
path?: string
|
|
|
|
): Promise<void> {
|
|
|
|
// set next open directory
|
|
|
|
await page.evaluate(
|
|
|
|
([path]) => {
|
|
|
|
Object.assign(window, {
|
|
|
|
__MOCKED_OPEN_DIR_PATH__: path,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[path]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:52:42 +00:00
|
|
|
export async function openLeftSidebar(page: Page): Promise<void> {
|
|
|
|
let sidebar = page.locator('#left-sidebar')
|
|
|
|
|
|
|
|
// Left sidebar is toggled by `is-open` class
|
|
|
|
if (!/is-open/.test(await sidebar.getAttribute('class'))) {
|
|
|
|
await page.click('#left-menu.button')
|
|
|
|
await page.waitForTimeout(10)
|
|
|
|
await expect(sidebar).toHaveClass(/is-open/)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 13:05:05 +00:00
|
|
|
export async function loadLocalGraph(page: Page, path?: string): Promise<void> {
|
|
|
|
await setMockedOpenDirPath(page, path);
|
2022-01-17 05:30:59 +00:00
|
|
|
|
2022-04-02 13:37:38 +00:00
|
|
|
const onboardingOpenButton = page.locator('strong:has-text("Choose a folder")')
|
2022-01-17 05:30:59 +00:00
|
|
|
|
2022-04-02 13:37:38 +00:00
|
|
|
if (await onboardingOpenButton.isVisible()) {
|
|
|
|
await onboardingOpenButton.click()
|
2022-01-05 13:05:05 +00:00
|
|
|
} else {
|
2022-04-02 13:37:38 +00:00
|
|
|
await page.click('#left-menu.button')
|
2022-01-17 05:30:59 +00:00
|
|
|
let sidebar = page.locator('#left-sidebar')
|
|
|
|
if (!/is-open/.test(await sidebar.getAttribute('class'))) {
|
|
|
|
await page.click('#left-menu.button')
|
2022-03-31 14:52:42 +00:00
|
|
|
await expect(sidebar).toHaveClass(/is-open/)
|
2022-01-05 13:05:05 +00:00
|
|
|
}
|
2022-01-17 05:30:59 +00:00
|
|
|
|
|
|
|
await page.click('#left-sidebar #repo-switch');
|
|
|
|
await page.waitForSelector('#left-sidebar .dropdown-wrapper >> text="Add new graph"', { state: 'visible' })
|
|
|
|
|
2022-01-05 13:05:05 +00:00
|
|
|
await page.click('text=Add new graph')
|
2022-04-02 13:37:38 +00:00
|
|
|
await page.waitForSelector('strong:has-text("Choose a folder")', { state: 'visible' })
|
|
|
|
await page.click('strong:has-text("Choose a folder")')
|
|
|
|
|
|
|
|
const skip = page.locator('a:has-text("Skip")')
|
|
|
|
await skip.click()
|
2022-01-05 13:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setMockedOpenDirPath(page, ''); // reset it
|
|
|
|
|
|
|
|
await page.waitForSelector(':has-text("Parsing files")', {
|
|
|
|
state: 'hidden',
|
|
|
|
timeout: 1000 * 60 * 5,
|
|
|
|
})
|
|
|
|
|
2022-04-02 13:37:38 +00:00
|
|
|
const title = await page.title()
|
|
|
|
if (title === "Import data into Logseq" || title === "Add another repo") {
|
|
|
|
await page.click('a.button >> text=Skip')
|
|
|
|
}
|
|
|
|
|
|
|
|
await page.waitForFunction('window.document.title === "Logseq"')
|
2022-01-05 13:05:05 +00:00
|
|
|
|
|
|
|
console.log('Graph loaded for ' + path)
|
|
|
|
}
|
|
|
|
|
2021-12-30 09:35:20 +00:00
|
|
|
export async function activateNewPage(page: Page) {
|
2022-01-17 05:30:59 +00:00
|
|
|
await page.click('.ls-block >> nth=0')
|
|
|
|
await page.waitForTimeout(500)
|
2022-01-07 04:09:27 +00:00
|
|
|
}
|