basic.spec.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { test, expect } from '@playwright/test'
  2. import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
  3. let electronApp: ElectronApplication
  4. let context: BrowserContext
  5. let page: Page
  6. function randomString(length: number) {
  7. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  8. let result = '';
  9. const charactersLength = characters.length;
  10. for (let i = 0; i < length; i++) {
  11. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  12. }
  13. return result;
  14. }
  15. test.beforeAll(async () => {
  16. electronApp = await electron.launch({
  17. cwd: "./static",
  18. args: ["electron.js"],
  19. // NOTE: video recording for Electron is not supported yet
  20. // recordVideo: {
  21. // dir: "./videos",
  22. // }
  23. })
  24. context = electronApp.context()
  25. await context.tracing.start({ screenshots: true, snapshots: true });
  26. // Evaluation expression in the Electron context.
  27. const appPath = await electronApp.evaluate(async ({ app }) => {
  28. // This runs in the main Electron process, parameter here is always
  29. // the result of the require('electron') in the main app script.
  30. return app.getAppPath()
  31. })
  32. console.log("Test start with AppPath:", appPath)
  33. })
  34. test.beforeEach(async () => {
  35. // discard any dialog by ESC
  36. if (page) {
  37. await page.keyboard.press('Escape')
  38. await page.keyboard.press('Escape')
  39. } else {
  40. page = await electronApp.firstWindow()
  41. }
  42. })
  43. test.afterAll(async () => {
  44. // await context.close();
  45. await context.tracing.stop({ path: 'artifacts.zip' });
  46. await electronApp.close()
  47. })
  48. test('render app', async () => {
  49. // Direct Electron console to Node terminal.
  50. // page.on('console', console.log)
  51. // Wait for the app to load
  52. await page.waitForLoadState('domcontentloaded')
  53. await page.waitForFunction('window.document.title != "Loading"')
  54. const title = await page.title()
  55. // Logseq: "A privacy-first platform for knowledge management and collaboration."
  56. // or Logseq
  57. expect(title).toMatch(/^Logseq.*?/)
  58. page.once('load', async () => {
  59. console.log('Page loaded!')
  60. await page.screenshot({ path: 'startup.png' })
  61. })
  62. })
  63. test('first start', async () => {
  64. await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
  65. })
  66. test('open sidebar', async () => {
  67. let sidebarVisible = await page.isVisible('#sidebar-nav-wrapper .left-sidebar-inner')
  68. if (!sidebarVisible) {
  69. await page.click('#left-menu.button')
  70. await page.waitForTimeout(1000)
  71. sidebarVisible = await page.isVisible('#sidebar-nav-wrapper .left-sidebar-inner')
  72. }
  73. expect(sidebarVisible).toBe(true)
  74. const button = await page.$('#sidebar-nav-wrapper a:has-text("New page")')
  75. expect(button).not.toBeNull()
  76. })
  77. test('search', async () => {
  78. await page.click('#search-button')
  79. await page.waitForSelector('[placeholder="Search or create page"]')
  80. await page.fill('[placeholder="Search or create page"]', 'welcome')
  81. await page.waitForTimeout(500)
  82. const results = await page.$$('#ui__ac-inner .block')
  83. expect(results.length).toBeGreaterThanOrEqual(1)
  84. })
  85. test('create page and blocks', async () => {
  86. const randomTitle = randomString(20)
  87. // Click #sidebar-nav-wrapper a:has-text("New page")
  88. await page.click('#sidebar-nav-wrapper a:has-text("New page")')
  89. // Fill [placeholder="Search or create page"]
  90. await page.fill('[placeholder="Search or create page"]', randomTitle)
  91. // Click text=/.*New page: "new page".*/
  92. await page.click('text=/.*New page: ".*/')
  93. // do editing
  94. await page.fill(':nth-match(textarea, 1)', 'this is my first bullet')
  95. await page.press(':nth-match(textarea, 1)', 'Enter')
  96. // first block
  97. expect(await page.$$('.block-content')).toHaveLength(1)
  98. await page.fill(':nth-match(textarea, 1)', 'this is my second bullet')
  99. await page.press(':nth-match(textarea, 1)', 'Enter')
  100. await page.fill(':nth-match(textarea, 1)', 'this is my third bullet')
  101. await page.press(':nth-match(textarea, 1)', 'Tab')
  102. await page.press(':nth-match(textarea, 1)', 'Enter')
  103. await page.keyboard.type('continue editing test')
  104. await page.keyboard.press('Shift+Enter')
  105. await page.keyboard.type('continue')
  106. await page.keyboard.press('Enter')
  107. await page.keyboard.press('Shift+Tab')
  108. await page.keyboard.press('Shift+Tab')
  109. await page.keyboard.type('test ok')
  110. await page.keyboard.press('Escape')
  111. const blocks = await page.$$('.ls-block')
  112. expect(blocks).toHaveLength(5)
  113. // active edit,
  114. await page.click('.ls-block >> nth=-1')
  115. await page.press('textarea >> nth=0', 'Enter')
  116. await page.fill('textarea >> nth=0', 'test')
  117. for (let i = 0; i < 5; i++) {
  118. await page.keyboard.press('Backspace')
  119. }
  120. await page.keyboard.press('Escape')
  121. await page.waitForTimeout(1000)
  122. expect(await page.$$('.ls-block')).toHaveLength(5)
  123. })
  124. test('delete and backspace', async () => {
  125. // active edit by click last block
  126. await page.dblclick('.ls-block >> nth=-1')
  127. await page.waitForSelector(':nth-match(textarea, 1)')
  128. await page.click(':nth-match(textarea, 1)')
  129. await page.fill(':nth-match(textarea, 1)', 'test')
  130. const input = await page.inputValue(':nth-match(textarea, 1)')
  131. expect(input).toBe('test')
  132. // backspace
  133. await page.keyboard.press('Backspace')
  134. await page.keyboard.press('Backspace')
  135. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  136. // refill
  137. await page.fill(':nth-match(textarea, 1)', 'test')
  138. await page.keyboard.press('ArrowLeft')
  139. await page.keyboard.press('ArrowLeft')
  140. // delete
  141. await page.keyboard.press('Delete')
  142. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('tet')
  143. await page.keyboard.press('Delete')
  144. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  145. await page.keyboard.press('Delete')
  146. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  147. // TODO: test delete & backspace across blocks
  148. })
  149. test('selection', async () => {
  150. await page.dblclick('.ls-block >> nth=-1')
  151. await page.waitForSelector(':nth-match(textarea, 1)')
  152. await page.click(':nth-match(textarea, 1)')
  153. await page.keyboard.down('Shift')
  154. await page.keyboard.press('ArrowUp')
  155. await page.keyboard.press('ArrowUp')
  156. await page.keyboard.press('ArrowUp')
  157. await page.keyboard.up('Shift')
  158. await page.waitForTimeout(500)
  159. await page.keyboard.press('Backspace')
  160. expect(await page.$$('.ls-block')).toHaveLength(2)
  161. })
  162. test('template', async () => {
  163. const randomTitle = randomString(20)
  164. const randomTemplate = randomString(10)
  165. await page.click('#sidebar-nav-wrapper a:has-text("New page")')
  166. await page.fill('[placeholder="Search or create page"]', "template" + randomTitle)
  167. await page.click('text=/.*New page: ".*/')
  168. await page.fill(':nth-match(textarea, 1)', 'template')
  169. await page.press(':nth-match(textarea, 1)', 'Shift+Enter')
  170. await page.type(':nth-match(textarea, 1)', 'template:: ' + randomTemplate)
  171. await page.press(':nth-match(textarea, 1)', 'Enter')
  172. await page.press(':nth-match(textarea, 1)', 'Enter')
  173. await page.press(':nth-match(textarea, 1)', 'Tab')
  174. await page.fill(':nth-match(textarea, 1)', 'line1')
  175. await page.press(':nth-match(textarea, 1)', 'Enter')
  176. await page.fill(':nth-match(textarea, 1)', 'line2')
  177. await page.press(':nth-match(textarea, 1)', 'Enter')
  178. await page.press(':nth-match(textarea, 1)', 'Tab')
  179. await page.fill(':nth-match(textarea, 1)', 'line3')
  180. await page.press(':nth-match(textarea, 1)', 'Enter')
  181. await page.press(':nth-match(textarea, 1)', 'Enter')
  182. await page.press(':nth-match(textarea, 1)', 'Enter')
  183. expect(await page.$$('.ls-block')).toHaveLength(5)
  184. await page.type(':nth-match(textarea, 1)', '/template')
  185. await page.click('[title="Insert a created template here"]')
  186. // type to search template name
  187. await page.keyboard.type(randomTemplate.substring(0, 3))
  188. await page.click('.absolute >> text=' + randomTemplate)
  189. await page.waitForTimeout(500)
  190. expect(await page.$$('.ls-block')).toHaveLength(8)
  191. })
  192. // FIXME: Electron with filechooser is not working
  193. test.skip('open directory', async () => {
  194. page.on('filechooser', async (fileChooser) => {
  195. console.log("file chooser")
  196. await fileChooser.setFiles('./test-graph')
  197. await page.keyboard.press('Enter')
  198. });
  199. await page.click('#sidebar-nav-wrapper >> text=Journals')
  200. await page.click('h1:has-text("Open a local directory")')
  201. // await page.waitForEvent('filechooser')
  202. await page.keyboard.press('Escape')
  203. await page.waitForTimeout(5000)
  204. })