|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +import { expect } from '@jupyterlab/galata'; |
| 7 | + |
| 8 | +import type { Locator, Page } from '@playwright/test'; |
| 9 | + |
| 10 | +import { test } from './fixtures'; |
| 11 | + |
| 12 | +const NOTEBOOK = 'simple.ipynb'; |
| 13 | + |
| 14 | +const SUBFOLDER = 'subfolder'; |
| 15 | + |
| 16 | +test.use({ autoGoto: false }); |
| 17 | + |
| 18 | +/** |
| 19 | + * Open a folder in the file browser of the drive, from the root of the drive. |
| 20 | + */ |
| 21 | +async function openFolder(browser: Locator, folder: string): Promise<void> { |
| 22 | + for (const name of folder.split('/')) { |
| 23 | + await browser |
| 24 | + .locator('.jp-DirListing-item') |
| 25 | + .getByText(name, { exact: true }) |
| 26 | + .dblclick(); |
| 27 | + } |
| 28 | + await expect(browser.locator('.jp-BreadCrumbs')).toContainText( |
| 29 | + path.basename(folder) |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Create a notebook from the main menu and return the path of the page that |
| 35 | + * opens it. |
| 36 | + */ |
| 37 | +async function newNotebook(page: Page): Promise<string> { |
| 38 | + const [notebook] = await Promise.all([ |
| 39 | + page.waitForEvent('popup'), |
| 40 | + page.menu.clickMenuItem('File>New>Notebook'), |
| 41 | + ]); |
| 42 | + await notebook.waitForSelector('.jp-NotebookPanel'); |
| 43 | + const pathname = decodeURIComponent(new URL(notebook.url()).pathname); |
| 44 | + await notebook.close(); |
| 45 | + return pathname; |
| 46 | +} |
| 47 | + |
| 48 | +test.describe('Custom drive', () => { |
| 49 | + let drive: Locator; |
| 50 | + let driveTab: Locator; |
| 51 | + |
| 52 | + test.beforeEach(async ({ context, page, tmpPath }) => { |
| 53 | + // The `test-drive` extension only adds its drive when the tests opt in. |
| 54 | + await context.addInitScript(() => { |
| 55 | + window.localStorage.setItem('test-drive', 'true'); |
| 56 | + }); |
| 57 | + await page.contents.uploadFile( |
| 58 | + path.resolve(__dirname, `./notebooks/${NOTEBOOK}`), |
| 59 | + `${tmpPath}/${SUBFOLDER}/${NOTEBOOK}` |
| 60 | + ); |
| 61 | + await page.goto(`tree/${tmpPath}`); |
| 62 | + |
| 63 | + drive = page.locator('#main-panel #test-drive-browser'); |
| 64 | + driveTab = page.locator('.jp-TreePanel .lm-TabBar-tab', { |
| 65 | + hasText: 'Test Drive', |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should show the file browser of the drive in a tab', async ({ |
| 70 | + page, |
| 71 | + }) => { |
| 72 | + await expect(page.locator('#main-panel #filebrowser')).toBeVisible(); |
| 73 | + await expect(drive).toBeHidden(); |
| 74 | + |
| 75 | + await driveTab.click(); |
| 76 | + |
| 77 | + await expect(drive).toBeVisible(); |
| 78 | + await expect(page.locator('#main-panel #filebrowser')).toBeHidden(); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should create a notebook in the file browser of the current tab', async ({ |
| 82 | + page, |
| 83 | + tmpPath, |
| 84 | + }) => { |
| 85 | + // The drive is added last, it must not replace the default file browser. |
| 86 | + expect(await newNotebook(page)).toEqual( |
| 87 | + `/notebooks/${tmpPath}/Untitled.ipynb` |
| 88 | + ); |
| 89 | + |
| 90 | + await driveTab.click(); |
| 91 | + await openFolder(drive, `${tmpPath}/${SUBFOLDER}`); |
| 92 | + expect(await newNotebook(page)).toEqual( |
| 93 | + `/notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/Untitled.ipynb` |
| 94 | + ); |
| 95 | + |
| 96 | + await page.locator('.jp-TreePanel >> text="Files"').click(); |
| 97 | + expect(await newNotebook(page)).toEqual( |
| 98 | + `/notebooks/${tmpPath}/Untitled1.ipynb` |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should open a notebook of the drive', async ({ page, tmpPath }) => { |
| 103 | + await driveTab.click(); |
| 104 | + await openFolder(drive, `${tmpPath}/${SUBFOLDER}`); |
| 105 | + |
| 106 | + const [notebook] = await Promise.all([ |
| 107 | + page.waitForEvent('popup'), |
| 108 | + drive.getByText(NOTEBOOK, { exact: true }).dblclick(), |
| 109 | + ]); |
| 110 | + |
| 111 | + await expect(notebook.locator('.jp-Notebook')).toContainText( |
| 112 | + 'Test Notebook' |
| 113 | + ); |
| 114 | + expect(decodeURIComponent(new URL(notebook.url()).pathname)).toEqual( |
| 115 | + `/notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/${NOTEBOOK}` |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + test('should not show the file browser of the drive on the notebook page', async ({ |
| 120 | + page, |
| 121 | + tmpPath, |
| 122 | + }) => { |
| 123 | + await page.goto(`notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/${NOTEBOOK}`); |
| 124 | + |
| 125 | + // The page uses the drive, but has no file browser for it. |
| 126 | + await expect(page.locator('.jp-Notebook')).toContainText('Test Notebook'); |
| 127 | + await expect(page.locator('#test-drive-browser')).toHaveCount(0); |
| 128 | + }); |
| 129 | +}); |
0 commit comments