utils.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { Page, Locator } from 'playwright'
  2. import { expect, ConsoleMessage } from '@playwright/test'
  3. import * as pathlib from 'path'
  4. import { modKey } from './util/basic'
  5. // TODO: The file should be a facade of utils in the /util folder
  6. // No more additional functions should be added to this file
  7. // Move the functions to the corresponding files in the /util folder
  8. // Criteria: If the same selector is shared in multiple functions, they should be in the same file
  9. export * from './util/basic'
  10. export * from './util/search-modal'
  11. /**
  12. * Locate the last block in the inner editor
  13. * @param page The Playwright Page object.
  14. * @returns The locator of the last block.
  15. */
  16. export async function lastBlock(page: Page): Promise<Locator> {
  17. // discard any popups
  18. await page.keyboard.press('Escape')
  19. // click last block
  20. if (await page.locator('text="Click here to edit..."').isVisible()) {
  21. await page.click('text="Click here to edit..."')
  22. } else {
  23. await page.click('.page-blocks-inner .ls-block >> nth=-1')
  24. }
  25. // wait for textarea
  26. await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
  27. await page.waitForTimeout(100)
  28. return page.locator('textarea >> nth=0')
  29. }
  30. /**
  31. * Press Enter and create the next block.
  32. * @param page The Playwright Page object.
  33. */
  34. export async function enterNextBlock(page: Page): Promise<Locator> {
  35. // Move cursor to the end of the editor
  36. await page.press('textarea >> nth=0', modKey + '+a') // select all
  37. await page.press('textarea >> nth=0', 'ArrowRight')
  38. let blockCount = await page.locator('.page-blocks-inner .ls-block').count()
  39. await page.press('textarea >> nth=0', 'Enter')
  40. await page.waitForTimeout(10)
  41. await page.waitForSelector(`.ls-block >> nth=${blockCount} >> textarea`, { state: 'visible' })
  42. return page.locator('textarea >> nth=0')
  43. }
  44. /**
  45. * Create and locate a new block at the end of the inner editor
  46. * @param page The Playwright Page object
  47. * @returns The locator of the last block
  48. */
  49. export async function newInnerBlock(page: Page): Promise<Locator> {
  50. await lastBlock(page)
  51. await page.press('textarea >> nth=0', 'Enter')
  52. return page.locator('textarea >> nth=0')
  53. }
  54. export async function escapeToCodeEditor(page: Page): Promise<void> {
  55. await page.press('.block-editor textarea', 'Escape')
  56. await page.waitForSelector('.CodeMirror pre', { state: 'visible' })
  57. await page.waitForTimeout(300)
  58. await page.click('.CodeMirror pre')
  59. await page.waitForTimeout(300)
  60. await page.waitForSelector('.CodeMirror textarea', { state: 'visible' })
  61. }
  62. export async function escapeToBlockEditor(page: Page): Promise<void> {
  63. await page.waitForTimeout(300)
  64. await page.click('.CodeMirror pre')
  65. await page.waitForTimeout(300)
  66. await page.press('.CodeMirror textarea', 'Escape')
  67. await page.waitForTimeout(300)
  68. }
  69. export async function setMockedOpenDirPath(
  70. page: Page,
  71. path?: string
  72. ): Promise<void> {
  73. // set next open directory
  74. await page.evaluate(
  75. ([path]) => {
  76. Object.assign(window, {
  77. __MOCKED_OPEN_DIR_PATH__: path,
  78. })
  79. },
  80. [path]
  81. )
  82. }
  83. export async function openLeftSidebar(page: Page): Promise<void> {
  84. let sidebar = page.locator('#left-sidebar')
  85. // Left sidebar is toggled by `is-open` class
  86. if (!/is-open/.test(await sidebar.getAttribute('class') || '')) {
  87. await page.click('#left-menu.button')
  88. await page.waitForTimeout(10)
  89. await expect(sidebar).toHaveClass(/is-open/)
  90. }
  91. }
  92. export async function loadLocalGraph(page: Page, path: string): Promise<void> {
  93. await setMockedOpenDirPath(page, path);
  94. const onboardingOpenButton = page.locator('strong:has-text("Choose a folder")')
  95. if (await onboardingOpenButton.isVisible()) {
  96. await onboardingOpenButton.click()
  97. } else {
  98. console.log("No onboarding button, loading file manually")
  99. let sidebar = page.locator('#left-sidebar')
  100. if (!/is-open/.test(await sidebar.getAttribute('class') || '')) {
  101. await page.click('#left-menu.button')
  102. await expect(sidebar).toHaveClass(/is-open/)
  103. }
  104. await page.click('#left-sidebar #repo-switch');
  105. await page.waitForSelector('#left-sidebar .dropdown-wrapper >> text="Add new graph"',
  106. { state: 'visible', timeout: 5000 })
  107. await page.click('text=Add new graph')
  108. expect(page.locator('#repo-name')).toHaveText(pathlib.basename(path))
  109. }
  110. setMockedOpenDirPath(page, ''); // reset it
  111. await page.waitForSelector(':has-text("Parsing files")', {
  112. state: 'hidden',
  113. timeout: 1000 * 60 * 5,
  114. })
  115. const title = await page.title()
  116. if (title === "Import data into Logseq" || title === "Add another repo") {
  117. await page.click('a.button >> text=Skip')
  118. }
  119. await page.waitForFunction('window.document.title === "Logseq"')
  120. await page.waitForTimeout(500)
  121. // If there is an error notification from a previous test graph being deleted,
  122. // close it first so it doesn't cover up the UI
  123. let n = await page.locator('.notification-close-button').count()
  124. if (n > 1) {
  125. await page.locator('button >> text="Clear all"').click()
  126. } else if (n == 1) {
  127. await page.locator('.notification-close-button').click()
  128. }
  129. await expect(page.locator('.notification-close-button').first()).not.toBeVisible({ timeout: 2000 })
  130. console.log('Graph loaded for ' + path)
  131. }
  132. export async function activateNewPage(page: Page) {
  133. await page.click('.ls-block >> nth=0')
  134. await page.waitForTimeout(500)
  135. }
  136. export async function editFirstBlock(page: Page) {
  137. await page.click('.ls-block .block-content >> nth=0')
  138. }
  139. /**
  140. * Wait for a console message with a given prefix to appear, and return the full text of the message
  141. * Or reject after a timeout
  142. *
  143. * @param page
  144. * @param prefix - the prefix to look for
  145. * @param timeout - the timeout in ms
  146. * @returns the full text of the console message
  147. */
  148. export async function captureConsoleWithPrefix(page: Page, prefix: string, timeout: number = 3000): Promise<string> {
  149. return new Promise((resolve, reject) => {
  150. let console_handler = (msg: ConsoleMessage) => {
  151. let text = msg.text()
  152. if (text.startsWith(prefix)) {
  153. page.removeListener('console', console_handler)
  154. resolve(text.substring(prefix.length))
  155. }
  156. }
  157. page.on('console', console_handler)
  158. setTimeout(reject.bind("timeout"), timeout)
  159. })
  160. }
  161. export async function queryPermission(page: Page, permission: PermissionName): Promise<boolean> {
  162. // Check if WebAPI clipboard supported
  163. return await page.evaluate(async (eval_permission: PermissionName): Promise<boolean> => {
  164. if (typeof navigator.permissions == "undefined")
  165. return Promise.resolve(false);
  166. return navigator.permissions.query({
  167. name: eval_permission
  168. }).then((result: PermissionStatus): boolean => {
  169. return (result.state == "granted" || result.state == "prompt")
  170. })
  171. }, permission)
  172. }
  173. export async function doesClipboardItemExists(page: Page): Promise<boolean> {
  174. // Check if WebAPI clipboard supported
  175. return await page.evaluate((): boolean => {
  176. return typeof ClipboardItem !== "undefined"
  177. })
  178. }
  179. export async function getIsWebAPIClipboardSupported(page: Page): Promise<boolean> {
  180. // @ts-ignore "clipboard-write" is not included in TS's type definition for permissionName
  181. return await queryPermission(page, "clipboard-write") && await doesClipboardItemExists(page)
  182. }