Skip to content

Commit

Permalink
Backport PR jupyterlab#14983: Fix filebrowser.open and add ability …
Browse files Browse the repository at this point in the history
…to provide a factory
  • Loading branch information
fcollonval authored and meeseeksmachine committed Aug 21, 2023
1 parent 3805b2c commit e48c1c3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
19 changes: 16 additions & 3 deletions galata/src/helpers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ export class ActivityHelper {
* @param name Activity name
* @returns Handle on the tab or null if the tab is not found
*/
getTab(name?: string): Promise<ElementHandle<Element> | null> {
return this.getTabLocator(name).elementHandle();
async getTab(name?: string): Promise<ElementHandle<Element> | null> {
let handle: ElementHandle<Element> | null = null;
try {
handle = await this.getTabLocator(name).elementHandle({ timeout: 500 });
} catch {
handle = null;
}
return handle;
}

/**
Expand Down Expand Up @@ -104,7 +110,14 @@ export class ActivityHelper {
locator = page.getByRole('main').locator(`[role="tabpanel"][id="${id}"]`);
}

return locator.elementHandle();
let handle: ElementHandle<Element> | null = null;
try {
handle = await locator.elementHandle({ timeout: 500 });
} catch {
handle = null;
}

return handle;
}

/**
Expand Down
31 changes: 25 additions & 6 deletions galata/src/helpers/filebrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,40 @@ export class FileBrowserHelper {
* Note: This will double click on the file;
* an editor needs to be available for the given file type.
*
* @param filePath Notebook path
* @param filePath File path
* @param factory Document factory to use
* @returns Action success status
*/
async open(filePath: string): Promise<boolean> {
async open(filePath: string, factory?: string): Promise<boolean> {
await this.revealFileInBrowser(filePath);
const name = path.basename(filePath);

const fileItem = await this.page.$(
`xpath=${this.xpBuildFileSelector(name)}`
);
if (fileItem) {
await fileItem.click({ clickCount: 2 });
await this.page.getByRole('main').getByRole('tab', { name }).waitFor({
state: 'visible'
});
if (factory) {
await fileItem.click({ button: 'right' });
await this.page
.getByRole('listitem')
.filter({ hasText: 'Open With' })
.click();
await this.page
.getByRole('menuitem', { name: factory, exact: true })
.click();
} else {
await fileItem.dblclick();
}
// Use `last` as if a file is already open, it will simply be activated
// if not it will be opened with optionally another factory (but we don't have a way
// to know that from the DOM).
await this.page
.getByRole('main')
.getByRole('tab', { name })
.last()
.waitFor({
state: 'visible'
});
} else {
return false;
}
Expand Down
48 changes: 48 additions & 0 deletions galata/test/galata/filebrowser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { expect, test } from '@jupyterlab/galata';

const DEFAULT_NAME = 'Untitled.ipynb';

test.describe('filebrowser helper', () => {
test.beforeEach(async ({ page }) => {
await page.menu.clickMenuItem('File>New>Notebook');

await page
.locator('.jp-Dialog')
.getByRole('button', { name: 'Select Kernel', exact: true })
.click();

await page.activity.closeAll();
});

test('should open a file', async ({ page }) => {
await page.filebrowser.open(DEFAULT_NAME);

expect(await page.activity.isTabActive(DEFAULT_NAME)).toEqual(true);
});

test('should activate already opened file', async ({ page }) => {
await page.menu.clickMenuItem('File>New>Console');

await page
.locator('.jp-Dialog')
.getByRole('button', { name: 'Select Kernel', exact: true })
.click();

expect.soft(await page.activity.isTabActive(DEFAULT_NAME)).toEqual(false);
await page.filebrowser.open(DEFAULT_NAME);
expect(await page.activity.isTabActive(DEFAULT_NAME)).toEqual(true);
});

test('should open the file with another factory', async ({ page }) => {
await page.filebrowser.open(DEFAULT_NAME);

await page.filebrowser.open(DEFAULT_NAME, 'Editor');

await expect(
page.getByRole('main').getByRole('tab', { name: DEFAULT_NAME })
).toHaveCount(2);
});
});

0 comments on commit e48c1c3

Please sign in to comment.